<template>
  <custom-echart-bar :chart-data="state.data" height="100%" :option="state.option" :show-mark-point="true" />
</template>
<script setup>
import { reactive, watch } from 'vue';
import { isEmpty, sleep } from '@/utils';

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

const state = reactive({
  option: {
    grid: {
      left: '5%',
      right: '5%',
      bottom: '5%',
      top: '10%',
      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) => {
        const params = data[0];
        let str = `<div class="custom-echarts-tips">
          <span>${params.name}</span><br/>
          <span>${params.marker} ${params.data} 家</span>
        </div>`;
        return str;
      },
    },
    barStyle: {
      barWidth: 15,
      itemStyle: {
        borderRadius: [8, 8, 0, 0],
      },
      color: {
        type: 'linear',
        x: 0,
        y: 0,
        x2: 0,
        y2: 1,
        colorStops: [
          { offset: 0, color: '#35D0C0' },
          { offset: 1, color: 'rgba(53,208,192,0.2)' },
        ],
        global: false,
      },
    },
    xAxis: {
      type: 'category',
      // name: '面积',
      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',
      // name: '面积(万亩)',
    },
  },
  data: [],
});

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 = [
    { name: '耿马镇', value: 70 },
    { name: '勐撒镇', value: 203 },
    { name: '勐永镇', value: 54 },
    { name: '孟定镇', value: 35 },
    { name: '勐简乡', value: 95 },
    { name: '贺派乡', value: 62 },
    { name: '四排山乡', value: 84 },
    { name: '芒洪乡', value: 82 },
    { name: '大兴乡', value: 64 },
  ];
};

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>
