<template>
  <!-- 支出分类统计 -->
  <div
    class="common-card"
    style="padding-bottom: 10px"
  >
    <div class="card-header">
      <h3 class="card-title">
        支出分类
      </h3>
      <van-tag
        type="primary"
        size="medium"
      >
        {{ expenseCategoriesView.length }}类
      </van-tag>
    </div>
    <!-- 环形图区域 -->
    <div
      v-if="expenseCategoriesView.length > 0"
      class="chart-container"
    >
      <div class="ring-chart">
        <BaseChart
          type="doughnut"
          :data="chartData"
          :options="chartOptions"
          :loading="false"
          @chart:render="onChartRender"
        />
      </div>
    </div>
    <!-- 分类列表 -->
    <div class="category-list">
      <div
        v-for="category in expenseCategoriesSimpView"
        :key="category.classify"
        class="category-item clickable"
        @click="$emit('category-click', category.classify, 0)"
      >
        <div class="category-info">
          <div
            class="category-color"
            :style="{ backgroundColor: category.color }"
          />
          <div class="category-name-with-count">
            <span class="category-name">{{ category.classify || '未分类' }}</span>
            <span class="category-count">{{ category.count }}笔</span>
          </div>
        </div>
        <div class="category-stats">
          <div class="category-amount">
            ¥{{ formatMoney(category.amount) }}
          </div>
          <div class="category-percent">
            {{ category.percent }}%
          </div>
        </div>
        <van-icon
          name="arrow"
          class="category-arrow"
        />
      </div>
      <!-- 展开/收起按钮 -->
      <div
        v-if="expenseCategoriesView.length > 1"
        class="expand-toggle"
        @click="showAllExpense = !showAllExpense"
      >
        <van-icon :name="showAllExpense ? 'arrow-up' : 'arrow-down'" />
      </div>
    </div>
    <ModernEmpty
      v-if="!expenseCategoriesView || !expenseCategoriesView.length"
      type="chart"
      theme="blue"
      title="暂无支出"
      description="本期还没有支出记录"
      size="small"
    />
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { getCssVar } from '@/utils/theme'
import ModernEmpty from '@/components/ModernEmpty.vue'
import BaseChart from '@/components/Charts/BaseChart.vue'
import { useChartTheme } from '@/composables/useChartTheme'

const props = defineProps({
  categories: {
    type: Array,
    default: () => []
  },
  totalExpense: {
    type: Number,
    default: 0
  },
  colors: {
    type: Array,
    default: () => []
  }
})

defineEmits(['category-click'])

const showAllExpense = ref(false)

// Chart.js 相关
const { getChartOptions } = useChartTheme()
let _chartJSInstance = null

// 格式化金额
const formatMoney = (value) => {
  if (!value && value !== 0) {
    return '0'
  }
  return Number(value)
    .toFixed(0)
    .replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

// 计算属性
const expenseCategoriesView = computed(() => {
  const list = [...props.categories]
  const unclassifiedIndex = list.findIndex((c) => !c.classify)
  if (unclassifiedIndex !== -1) {
    const [unclassified] = list.splice(unclassifiedIndex, 1)
    list.unshift(unclassified)
  }
  return list
})

const expenseCategoriesSimpView = computed(() => {
  const list = expenseCategoriesView.value

  if (showAllExpense.value) {
    return list
  }

  const unclassified = list.filter((c) => c.classify === '未分类' || !c.classify)
  if (unclassified.length > 0) {
    return [...unclassified]
  }
  return []
})

// 准备图表数据（通用）
const prepareChartData = () => {
  const list = [...expenseCategoriesView.value]
  list.sort((a, b) => b.amount - a.amount)

  const MAX_SLICES = 8

  if (list.length > MAX_SLICES) {
    const topList = list.slice(0, MAX_SLICES - 1)
    const otherList = list.slice(MAX_SLICES - 1)
    const otherAmount = otherList.reduce((sum, item) => sum + item.amount, 0)

    const chartData = topList.map((item, index) => ({
      label: item.classify || '未分类',
      value: item.amount,
      color: props.colors[index % props.colors.length]
    }))

    chartData.push({
      label: '其他',
      value: otherAmount,
      color: getCssVar('--van-gray-6')
    })

    return chartData
  } else {
    return list.map((item, index) => ({
      label: item.classify || '未分类',
      value: item.amount,
      color: props.colors[index % props.colors.length]
    }))
  }
}

// Chart.js 数据
const chartData = computed(() => {
  const data = prepareChartData()

  return {
    labels: data.map((item) => item.label),
    datasets: [
      {
        data: data.map((item) => item.value),
        backgroundColor: data.map((item) => item.color),
        borderWidth: 2,
        borderColor: getCssVar('--van-background-2') || '#fff',
        hoverOffset: 4
      }
    ]
  }
})

// Chart.js 配置
const chartOptions = computed(() => {
  return getChartOptions({
    cutout: '50%',
    plugins: {
      legend: {
        display: false
      },
      tooltip: {
        callbacks: {
          label: (context) => {
            const label = context.label || ''
            const value = context.parsed || 0
            const total = context.dataset.data.reduce((a, b) => a + b, 0)
            const percentage = total > 0 ? ((value / total) * 100).toFixed(1) : 0
            return `${label}: ¥${formatMoney(value)} (${percentage}%)`
          }
        }
      }
    },
    onClick: (_event, _elements) => {
      // 点击饼图扇区时，触发跳转到分类详情
      // 注意：这个功能在 BaseChart 中不会自动触发，需要后续完善
    }
  })
})

// Chart.js 渲染完成回调
const onChartRender = (chart) => {
  _chartJSInstance = chart
}
</script>

<style scoped lang="scss">
@import '@/assets/theme.css';

.common-card {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: var(--spacing-xl);
  margin-bottom: var(--spacing-xl);
  box-shadow: var(--shadow-sm);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--spacing-lg);
}

.card-title {
  font-size: var(--font-lg);
  font-weight: var(--font-semibold);
  color: var(--text-primary);
  margin: 0;
}

.chart-container {
  padding: 0;
}

.ring-chart {
  position: relative;
  width: 100%;
  height: 200px;
  margin: 0 auto;
}

.category-list {
  padding: 0;
}

.category-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--van-border-color);
  transition: background-color 0.2s;
  gap: 12px;
}

.category-item:last-child {
  border-bottom: none;
}

.category-item.clickable {
  cursor: pointer;
}

.category-item.clickable:active {
  background-color: var(--van-background);
}

.category-info {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.category-name-with-count {
  display: flex;
  align-items: center;
  gap: 8px;
}

.category-color {
  width: 12px;
  height: 12px;
  border-radius: 50%;
}

.category-name {
  font-size: 14px;
  color: var(--van-text-color);
}

.category-count {
  font-size: 12px;
  color: var(--van-text-color-3);
}

.category-stats {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.category-arrow {
  margin-left: 8px;
  color: var(--van-text-color-3);
  font-size: 16px;
  flex-shrink: 0;
}

.expand-toggle {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 0;
  color: var(--van-text-color-3);
  font-size: 20px;
  cursor: pointer;
}

.expand-toggle:active {
  opacity: 0.7;
}

.category-amount {
  font-size: 15px;
  font-weight: 600;
  color: var(--van-text-color);
}

.category-percent {
  font-size: 12px;
  color: var(--van-text-color-3);
  background: var(--van-background);
  padding: 2px 8px;
  border-radius: 10px;
}
</style>
