<template>
  <view class="chart-wrap" :style="{ height: heightPx }">
    <EChartsView :option="chartOption" class="chart" />
  </view>
</template>

<script lang="uts">
import EChartsView from '@/uni_modules/charts/EChartsView.vue'

export default {
  components: {
    EChartsView
  },
  props: {
    items: { type: Array, default: () => [] },
    height: { type: Number, default: 300 }
  },
  data() {
    return {
      heightPx: '300px',
      chartOption: {} as any
    }
  },
  watch: {
    items: { handler() { this.updateOption() }, deep: true },
    height: { 
      handler() { 
        this.heightPx = `${this.height}px`
      } 
    }
  },
  mounted() {
    this.heightPx = `${this.height}px`
    setTimeout(() => {
      this.updateOption()
    }, 200)
  },
  methods: {
    toPlainObject(obj: any): any {
      if (obj == null) return null
      if (typeof obj !== 'object') return obj
      if (Array.isArray(obj)) {
        return obj.map((item) => this.toPlainObject(item))
      }
      const plain: any = {}
      for (const key in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, key)) {
          const value = obj[key]
          if (typeof value === 'function' || key.startsWith('_') || key === 'toJSON') {
            continue
          }
          if (value != null && typeof value === 'object' && !Array.isArray(value)) {
            let isSimple = true
            for (const k in value) {
              if (typeof value[k] === 'object' && value[k] !== null) {
                isSimple = false
                break
              }
            }
            plain[key] = isSimple ? { ...value } : this.toPlainObject(value)
          } else {
            plain[key] = value
          }
        }
      }
      return plain
    },
    updateOption() {
      if (!this.items || this.items.length === 0) return

      const data = (this.items as Array<any>).map((it) => {
        const itemStyle = it.itemStyle ? this.toPlainObject(it.itemStyle) : {} as any
        if (it.color && !itemStyle.color) {
          itemStyle.color = it.color
        }
        return {
          name: String(it.name || it.label),
          value: Number(it.value),
          itemStyle: itemStyle
        }
      })

      const option = {
        color: ['#1890ff', '#2ec7c9', '#b6a2de', '#5ab1ef', '#ffb980', '#d87a80'],
        tooltip: { 
          show: true,
          trigger: 'item', 
          formatter: '{b}: {c} ({d}%)',
          backgroundColor: 'rgba(255, 255, 255, 0.9)',
          textStyle: { color: '#666', fontSize: 12 }
        },
        legend: {
          orient: 'vertical',
          right: 20,
          top: 'center',
          itemWidth: 10,
          itemHeight: 10,
          textStyle: { fontSize: 11, color: '#666' }
        },
        series: [{
          type: 'pie',
          radius: ['45%', '70%'],
          center: ['40%', '50%'],
          data: data,
          avoidLabelOverlap: true,
          label: {
            show: false
          },
          labelLine: { show: false },
          itemStyle: {
            borderRadius: 4,
            borderWidth: 2,
            borderColor: '#fff'
          }
        }]
      }
      this.chartOption = this.toPlainObject(option)
    }
  }
}
</script>

<style scoped>
.chart-wrap { width: 100%; position: relative; overflow: hidden; }
.chart { width: 100%; height: 100%; position: absolute; top: 0; left: 0; }
</style>

