<template>
  <div>
    <div class="title" @click="openDialog">{{ title }}</div>
    <div class="content">
      <div>
        <div class="left-item">
          <div class="left-tip">装备数 <span class="unit">（台）</span></div>
          <div class="number">{{ query.num || 0 }}</div>
        </div>
        <div class="left-item">
          <div class="left-tip">装备价值 <span class="unit">（亿元）</span></div>
          <div class="number">{{ query.price ? (query.price / 100000000).toFixed(4) : 0 }}</div>
        </div>
      </div>
      <div>
        <div class="right-tip">装备年限</div>
        <!-- 饼状图 -->
        <div ref="pieChart" class="pie-chart"></div>
      </div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  props: {
    title: {
      type: String,
      default: '',
    },
    state: {
      type: Object,
      default: () => {
        return {
          num: 0,
          price: 0,
        }
      },
    },
    pieValues: {
      type: Array,
      default: () => [], // 只有 value
    },
  },
  data() {
    return {
      query: {},
    }
  },
  mounted() {
    console.log('🚀 ~ mounted ~ this.state:', this.state)
    console.log('statexxxxxxxxxxxxxxx-->', this.pieValues)
    setTimeout(() => {
      this.query = this.state
      this.initPieChart()
    }, 300)
  },
  computed: {
    pieData() {
      // 固定 name，value 从父组件传入
      const names = ['5年以内', '5-10年', '10年以上']
      return names.map((name, index) => ({
        name,
        value: this.pieValues[index] || '',
      }))
    },
  },
  methods: {
    initPieChart() {
      const chartDom = this.$refs.pieChart
      // console.log('🚀 ~ initPieChart ~ chartDom:', chartDom)
      if (!chartDom) return
      const myChart = echarts.init(chartDom)
      // 提取颜色数组为独立变量
      const pieColors = ['#69A6C9', '#7DDBBA', '#ffc300']
      const option = {
          color: pieColors, // 保持原颜色
          tooltip: {
            trigger: 'item',
          },
          // 隐藏图例（图中无图例）
          // legend: {
          //   show: false
          // },
          series: [
            {
              name: '装备年限',
              type: 'pie',
              radius: '39%',
              avoidLabelOverlap: false,
              // 调整标签：拆分成“百分比（大字体+彩色）”和“名称（小字体+白色）”
              label: {
                show: true,
                // 自定义标签格式：百分比单独放大+彩色，名称换行后白色
                formatter: function(params) {
                  // 自定义标签格式：百分比 + 换行 + 名称
                  return `{percent|${params.percent.toFixed(0)}%}\n{name|${params.name}}`;
                },
                // 富文本样式：控制百分比和名称的字体/颜色
                rich: {
                  percent: {
                    fontSize: 16, // 百分比字体放大
                    fontWeight: 'bold',
                    color: 'inherit'
                  },
                  name: {
                    fontSize: 12, // 名称字体缩小
                    color: '#fff', // 名称统一白色
                    marginTop: 2
                  }
                },
                // 标签位置：调整到饼图外部（和示例一致）
                position: 'outside'
              },
              labelLine: {
                show: true,
                length: 8,
                length2: 5,
                lineStyle: {
                  color: '#fff' // 标签线改为白色（适配深色背景）
                }
              },

            data: this.pieData,
          },
        ],
      }

      myChart.setOption(option)
      window.addEventListener('resize', () => myChart.resize())
    },
    openDialog() {
      this.$emit('openDialog')
    },
  },
}
</script>

<style lang="scss" scoped>
.title {
  padding-left: 12px;
  font-size: 16px;
  font-weight: 600;
  background-image: url('../../img/wsScreenTitleSmall.png');
  // background-size: 100% 100%;
  color: #f0f5f8;
  cursor: pointer;
}
.number {
  font-size: 20px;
  background: linear-gradient(180deg, #FFF 19.35%, #5DCBFE 77.42%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}
.content {
  display: flex;
  .left-item {
    margin-top: 16px;
    height: 100px;
    text-align: center;
    background-image: url('../../img/equip-item.png');
    background-size: 100% 100%;

    .left-tip {
      padding-top: 6px;
      padding-bottom: 15px;
      font-size: 14px;
      font-weight: 500;
    }

    .unit {
        font-size: 14px;
        font-weight: 300;
        background: linear-gradient(180deg, #FFF 25.81%, #FFF 77.42%);
        background-clip: text;
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
  }

  .right-tip {
    margin-left: 30px;
    margin-top: 16px;
    padding: 4px 0;
    font-size: 14px;
    font-weight: 500;
    width: 160px;
    text-align: center;
    background-image: url('../../img/equip-title.png');
    background-size: 100% 100%;
  }
  .pie-chart {
    width: 200px;
    height: 200px;
  }
}
</style>
