<template>
  <!-- 平台来源 -->
  <div class="echarts">
    <ECharts :option="option" :resize="false" />
  </div>
</template>

<script setup>
  import ECharts from '@/components/ECharts/index.vue';
  import { useDict } from '@/hooks/useDict';
  import echarts from '@/components/ECharts/config';
  import { reactive, ref, computed, onBeforeUnmount } from 'vue';
  import { getFourFollowTypeTotal } from '@/api/dataScreen';
  const load = ref(false);
  const salvPro = ref([]);
  const timer1 = ref();
  const { FollowStatus } = useDict('FollowStatus');
  const props = defineProps({
    tenantId: [String, Number, Object],
  });
  onBeforeUnmount(() => {
    if (timer1.value) {
      clearInterval(timer1.value);
      timer1.value = null;
    }
  });
  // 绘制左侧面
  const CubeLeft = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0,
    },
    buildPath: function (ctx, shape) {
      // 会canvas的应该都能看得懂，shape是从custom传入的
      const xAxisPoint = shape.xAxisPoint;
      const c0 = [shape.x + 17, shape.y];
      const c1 = [shape.x - 30, shape.y - 6];
      const c2 = [xAxisPoint[0] - 30, xAxisPoint[1] - 13];
      const c3 = [xAxisPoint[0] + 17, xAxisPoint[1]];
      ctx
        .moveTo(c0[0], c0[1])
        .lineTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .closePath();
    },
  });
  // 绘制右侧面
  const CubeRight = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0,
    },
    buildPath: function (ctx, shape) {
      const xAxisPoint = shape.xAxisPoint;
      const c1 = [shape.x + 17, shape.y];
      const c2 = [xAxisPoint[0] + 17, xAxisPoint[1]];
      const c3 = [xAxisPoint[0] + 35, xAxisPoint[1] - 15];
      const c4 = [shape.x + 35, shape.y - 15];
      ctx
        .moveTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .lineTo(c4[0], c4[1])
        .closePath();
    },
  });
  // 绘制顶面
  const CubeTop = echarts.graphic.extendShape({
    shape: {
      x: 0,
      y: 0,
    },
    buildPath: function (ctx, shape) {
      const c1 = [shape.x + 17, shape.y];
      const c2 = [shape.x + 35, shape.y - 15]; //右点
      const c3 = [shape.x - 12, shape.y - 20];
      const c4 = [shape.x - 30, shape.y - 6];
      ctx
        .moveTo(c1[0], c1[1])
        .lineTo(c2[0], c2[1])
        .lineTo(c3[0], c3[1])
        .lineTo(c4[0], c4[1])
        .closePath();
    },
  });
  // 注册三个面图形
  echarts.graphic.registerShape('CubeLeft', CubeLeft);
  echarts.graphic.registerShape('CubeRight', CubeRight);
  echarts.graphic.registerShape('CubeTop', CubeTop);
  const param = {
    tenant: props.tenantId,
  };
  const query = () => {
    load.value = true;
    getFourFollowTypeTotal(param).then((res) => {
      if (res.result) {
        load.value = false;
        salvPro.value = res.result.map((v) => ({
          name: v.followType,
          value: v.tractNum,
          flowStatusTotalList: v.flowStatusTotalList,
        }));
      }
    });
  };
  query();
  timer1.value = setInterval(() => {
    if (!load.value) {
      query();
    }
  }, 6000);

  const option = computed(() => {
    return {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow',
        },
        formatter: function (params, ticket, callback) {
          const item = params[1];
          let html = item.name + ' : ' + item.value + '<br/>';
          if (item.data && item.data.flowStatusTotalList.length > 0) {
            html += '以下是明细：<br/>';
            item.data.flowStatusTotalList.forEach((item, index) => {
              html +=
                (item.followStatus
                  ? FollowStatus.value?.find((v) => v.value === item.followStatus)?.label
                  : '') +
                item.tractNum +
                '个<br/>';
            });
            html += '</div>';
          }
          return html;
        },
      },
      grid: {
        left: '5%',
        right: '5%',
        top: '15%',
        bottom: '5%',
        containLabel: true,
      },
      xAxis: {
        type: 'category',
        data: salvPro.value.map((v) => v.name),
        axisLine: {
          show: true,
          lineStyle: {
            color: 'white',
          },
        },
        axisTick: {
          show: false,
          length: 9,
          alignWithLabel: true,
          lineStyle: {
            color: '#7DFFFD',
          },
        },
        axisLabel: {
          fontSize: 20,
        },
      },
      yAxis: {
        type: 'value',
        axisLine: {
          show: true,
          lineStyle: {
            color: 'white',
          },
        },
        min: 0,
        splitLine: {
          show: false,
        },
        axisTick: {
          show: false,
        },
        axisLabel: {
          fontSize: 16,
        },
        boundaryGap: ['20%', '20%'],
      },
      series: [
        {
          type: 'custom',
          renderItem: (params, api) => {
            const location = api.coord([api.value(0), api.value(1)]);
            return {
              type: 'group',
              children: [
                {
                  type: 'CubeLeft',
                  shape: {
                    api,
                    xValue: api.value(0),
                    yValue: api.value(1),
                    x: location[0],
                    y: location[1],
                    xAxisPoint: api.coord([api.value(0), 0]),
                  },
                  style: {
                    fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: '#1BC9F1',
                      },
                      {
                        offset: 1,
                        color: '#1C7084',
                      },
                    ]),
                  },
                },
                {
                  type: 'CubeRight',
                  shape: {
                    api,
                    xValue: api.value(0),
                    yValue: api.value(1),
                    x: location[0],
                    y: location[1],
                    xAxisPoint: api.coord([api.value(0), 0]),
                  },
                  style: {
                    fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: '#1C7287',
                      },
                      {
                        offset: 1,
                        color: '#1BC3EB',
                      },
                    ]),
                  },
                },
                {
                  type: 'CubeTop',
                  shape: {
                    api,
                    xValue: api.value(0),
                    yValue: api.value(1),
                    x: location[0],
                    y: location[1],
                    xAxisPoint: api.coord([api.value(0), 0]),
                  },
                  style: {
                    fill: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                      {
                        offset: 0,
                        color: '#06728A',
                      },
                      {
                        offset: 1,
                        color: '#06728A',
                      },
                    ]),
                  },
                },
              ],
            };
          },
          data: salvPro.value,
        },
        {
          type: 'bar',
          label: {
            normal: {
              show: true,
              position: 'top',
              formatter: (e) => {
                return e.value;
              },
              fontSize: 16,
              color: '#fff',
              offset: [2, -25],
            },
          },
          barWidth: 170,
          itemStyle: {
            color: 'transparent',
          },
          tooltip: {},
          data: salvPro.value,
        },
      ],
    };
  });
</script>
<style lang="scss" scoped>
  .echarts {
    width: 100%;
    height: 100%;
  }
</style>
