<template>
  <custom-echart-column-line :chart-data="state.data" height="100%" :option="state.option" />
</template>
<script setup>
import { reactive, watch } from 'vue';
import { isEmpty, sleep } from '@/utils';
import * as echarts from 'echarts';

const props = defineProps({
  data: {
    type: Array,
    default: () => [],
  },
  query: {
    type: String,
    default: '',
  },
});

const state = reactive({
  data: [],
  option: {
    grid: {
      left: '3%',
      right: '1%',
      bottom: '10%',
      top: '15%',
      containLabel: true,
    },
    tooltip: {
      show: true,
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
      // tooltip样式调整添加这个类名
      className: 'custom-tooltip-container', // 自定义父容器类名
      backgroundColor: 'rgba(0,0,0,0.5)',
      borderColor: '#35d0c0',
      formatter: (data) => {
        console.log('data', data);
        const params = data[0];
        let str = `<div class="custom-echarts-tips">
          <span>${params.name}</span><br/>
          <span>${params.marker} ${params.data} 万吨</span><br />
          <span>${data[1].marker} ${data[1].data} 万吨</span>
        </div>`;
        return str;
      },
    },
    barStyle: {
      barWidth: 15,
      color: {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          { offset: 0, color: '#35D0C0' },
          { offset: 1, color: '#35D0C0' },
        ],
        global: false,
      },
    },
    xAxis: {
      type: 'category',
      axisTick: {
        show: false,
        alignWithLabel: false,
        interval: 'auto',
        inside: false,
        length: 5,
        lineStyle: {
          type: 'solid',
          width: 1,
          color: 'rgba(28, 158, 222, 1)',
        },
      },
    },
    yAxis: [
      {
        type: 'value',
        barWidth: 15,
        nameTextStyle: {
          color: '#fff',
          fontSize: 12,
          align: 'center',
          padding: [0, 20, 5, 0],
        },
        axisLabel: {
          formatter: '{value}',
          color: 'rgba(95, 187, 235, 1)',
          textStyle: {
            fontSize: 14,
            color: '#fff',
            lineHeight: 16,
          },
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          show: true,
          lineStyle: {
            color: 'rgba(28, 130, 197, .3)',
            type: 'dashed',
          },
        },
      },
      {
        splitNumber: 5,
        type: 'value',
        show: false,
        nameTextStyle: {
          color: '#fff',
          fontSize: 12,
          align: 'center',
          padding: [0, 0, 5, 0],
        },
        axisLabel: {
          show: true,
          fontSize: 12,
          color: '#fff',
        },
        axisLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        splitLine: {
          show: false,
        },
      },
    ],
    legend: {
      itemWidth: 16,
      itemHeight: 16,
      itemGap: 20,
      right: '2%',
      top: '3%',
      x: 'center',
      textStyle: {
        fontSize: 16,
        color: '#fff',
      },
      data: ['总产量（吨）', '平均产量（吨）'],
      selectedMode: false,
    },
    series: [
      {
        name: '总产量（吨）',
        type: 'bar',
        data: [],
        barWidth: '15px',
        barGap: '50%',
        itemStyle: {
          normal: {
            borderRadius: [8, 8, 0, 0],
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: 'rgba(1,254,253,1)',
              },

              {
                offset: 1,
                color: 'rgba(1,254,253,0)',
              },
            ]),
          },
        },
        label: {
          show: false,
          position: 'top',
          fontSize: 12,
          color: '#F5F5F5',
          offset: [0, -5],
          formatter: '{c}',
        },
      },
      {
        data: [], // 折线图的数据
        name: '平均产量（吨）',
        type: 'line',
        yAxisIndex: 1,
        showSymbol: false,
        symbolSize: 8,
        smooth: false,
        symbol: 'circle',
        max: 100,
        lineStyle: {
          normal: {
            color: '#FEF906',
          },
        },
        itemStyle: {
          color: '#FEF906',
        },
      },
    ],
  },
});

const loadData = async (code = '') => {
  state.loading = true;
  // GetInputsInfo()
  //   .then((res) => {
  //     if (res.code === 200) {
  //       state.data = res.data;
  //     }
  //   })
  //   .catch((err) => {
  //     app.$message.error(err.msg);
  //   });
  await sleep(500);
  state.data = [
    { value: 103, value1: 102, name: '耿马镇' },
    { value: 72, value1: 157, name: '勐撒镇' },
    { value: 50, value1: 125, name: '勐永镇' },
    { value: 60, value1: 146, name: '孟定镇' },
    { value: 40, value1: 86, name: '勐简乡' },
    { value: 111, value1: 172, name: '贺派乡' },
    { value: 81, value1: 180, name: '四排山乡' },
    { value: 55, value1: 66, name: '芒洪乡' },
    { value: 68, value1: 84, name: '大兴乡' },
  ];
};

watch(
  () => props.data,
  (val) => {
    if (!isEmpty(val)) {
      state.data = val;
    }
  },
  {
    deep: true,
    immediate: true,
  }
);

watch(
  () => props.query,
  (val) => {
    if (!isEmpty(val)) {
      loadData(val);
    }
  },
  {
    deep: true,
    immediate: true,
  }
);
</script>
