<template>
  <div>
    <div class="title" @click="openDialog">{{ title }}</div>
    <div class="content">
      <div>
        <div class="left-item">
          <div class="left-tip">装备数 <span>（台）</span></div>
          <div class="number">{{ query.num || 0 }}</div>
        </div>
        <div class="left-item">
          <div class="left-tip">装备价值 <span>（亿元）</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('state-->', 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 option = {
        tooltip: {
          trigger: 'item',
        },
        series: [
          {
            name: '装备年限',
            type: 'pie',
            radius: '39%',
            avoidLabelOverlap: false,
            label: {
              show: true,
              formatter: '{b}\n{d}%',
              color: '#A3D6F8',
            },
            labelLine: {
              show: true,
              length: 8, // 第一段线
              length2: 5, // 第二段线
            },

            data: this.pieData,
          },
        ],
      }

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

<style lang="scss" scoped>
.title {
  padding-left: 10px;
  font-weight: 800;
  background-image: url('../../img/equip-title.png');
  background-size: 100% 100%;
  color: #f0f5f8;
  cursor: pointer;
}
.number {
  font-size: 20px;
  background: linear-gradient(to bottom, #f0f5f8, #5eb6f0);
  -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: 600;
    }
  }

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