<template>
  <div shadow="never" class="homeBoxCard" v-loading="loading">
    <div style="text-align: center; font-weight: bold">
      {{ curveComList.name }}
    </div>
    <div class="height300" ref="worksChartRef" />
  </div>
</template>
<script>
import { computed, defineComponent, onMounted, ref, watch } from 'vue'
import { useStore } from 'vuex'
import * as echarts from 'echarts'

export default defineComponent({
  name: 'CurveComB',
  props: {
    curveComList: Object,
  },
  setup(props, context) {
    context
    const store = useStore()
    const worksChartRef = ref(null)

    // 总数
    const total = computed(() => store.state.Home.worksChartData.total)
    // num
    const num = computed(() => store.state.Home.worksChartData.num)

    // 读取数据 func
    const loading = ref(true)
    const getData = async () => {
      // loading.value = true
      // await store.dispatch("Home/queryWorksChartData");
      loading.value = false
    }

    function ecahrts() {
      let myChart = echarts.init(worksChartRef.value)

      // 绘制图表
      myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        legend: {
          right: 60,
          itemWidth: 10,
          itemHeight: 10,
          itemGap: 35,
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            data: props.curveComList.dateList,
            axisLine: {
              show: true,
              lineStyle: {
                color: '#444',
                width: 1,
                type: 'solid',
              },
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: true,
              color: '#444',
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
            name: 'A',
            nameTextStyle: {
              color: '#444',
            },
            axisLabel: {
              formatter: '{value} ',
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
              lineStyle: {
                color: '#444',
                width: 1,
                type: 'solid',
              },
            },
            splitLine: {
              lineStyle: {
                color: '#444',
              },
            },
          },
        ],
        series: [
          {
            name: props.curveComList.icontitle,
            type: 'bar',
            data: props.curveComList.data,
            barWidth: 10, //柱子宽度
            barGap: '1', //柱子之间间距
            itemStyle: {
              normal: {
                color: '#5b82ee',
                opacity: 1,
              },
            },
          },
        ],
      })
      window.addEventListener('resize', () => {
        myChart.resize()
      })
    }

    onMounted(() => {
      getData()
      ecahrts()
    })

    // 异步任务 用于给tree传值
    const writeValue = (val) => {
      val
      //   console.log(val)
      ecahrts()
    }
    // 定义 watch 监听
    watch(
      props.curveComList,
      (newCount, old, clear) => {
        // 执行异步任务，并得到关闭异步任务的 id
        let id = writeValue(newCount, old)
        // 如果 watch 监听被重复执行了，则会先清除上次未完成的异步任务
        clear(() => clearTimeout(id))
      },
      // watch 刚被创建的时候不执行
      { lazy: true }
    )

    return {
      worksChartRef,
      loading,
      total,
      num,
    }
  },
})
</script>
<style lang="scss" scoped>
.homeBoxCard {
  margin-bottom: 24px;
  ::v-deep(.el-card__header) {
    padding-left: 12px;
    padding-right: 12px;
  }
  ::v-deep(.el-card__body) {
    padding: 12px;
    font-size: 14px;
    line-height: 1.5715;
  }
  ::v-deep(.el-divider) {
    margin: 8px 0;
  }
  .num {
    font-size: 30px;
    color: #515a6e;
  }
  .height300 {
    height: 300px;
  }
}
</style>