<template>
  <div id="personnel-site" style="width: 100%; height: 300px"></div>
</template>

<script>
import { getInspectionData } from '@/api/index';
import { getDate, getPastTime } from '@/common/times';
import * as echarts from 'echarts';
export default {
  props: {
    time: {
      type: Array,
    },
    all: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chartPie: null,
      yData: [],
      xData: [],
      seriesData: [],
      isAllProject: false,
    };
  },
  watch: {
    time(value) {
      this.maintenanceTime = value;
      this.getInspectionData();
      setTimeout(() => {
        this.drawCharts();
      }, 2000);
    },
    all(value) {
      this.isAllProject = value;
      this.getInspectionData();
      setTimeout(() => {
        this.drawCharts();
      }, 2000);
    },
  },

  mounted: function () {},
  methods: {
    async getInspectionData() {
      var projectId = sessionStorage.getItem('projectId');
      var param = {};
      if (this.isAllProject == false) {
        param = {
          start: getDate(this.maintenanceTime[0]),
          end: getDate(this.maintenanceTime[1]),
          projectId: projectId,
        };
      } else {
        param = {
          start: getDate(this.maintenanceTime[0]),
          end: getDate(this.maintenanceTime[1]),
        };
      }
      const { data } = await getInspectionData(param);
      var xData = [];
      var yData = [];
      var seriesData = [];
      data.byDevice.forEach(function (v, k) {
        if (k < 10) {
          xData.push(v.key);
          yData.push(v.data);
          seriesData.push([v.key, v.data]);
        }
      });
      this.xData = xData;
      this.yData = yData;
      this.seriesData = seriesData;
    },
    drawPieChart() {
      console.log(this.seriesData);
      this.chartPie = echarts.init(document.getElementById('personnel-site'));
      this.chartPie.setOption({
        title: {
          left: 'center',
          text: 'Statistics by site',
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: false,
            },
            saveAsImage: {
              pixelRatio: 2,
            },
          },
        },
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        // dataZoom: [
        //   {
        //     id: 'dataZoomX',
        //     type: 'slider',
        //     xAxisIndex: [0],
        //     filterMode: 'filter'
        //   },
        //   {
        //     id: 'dataZoomY',
        //     type: 'slider',
        //     yAxisIndex: [0],
        //     filterMode: 'empty'
        //   }
        // ],
        xAxis: {
          type: 'category',
          data: this.xData,
          silent: false,
          interval: 0,
          splitLine: {
            show: false,
          },
          splitArea: {
            show: false,
          },
        },
        yAxis: {
          splitArea: {
            show: false,
          },
        },
        series: [
          {
            type: 'bar',
            data: this.yData,
            // Set `large` for large data amount
            large: true,
          },
        ],
      });
    },
    drawCharts() {
      this.drawPieChart();
    },
  },
};
</script>

<style scoped></style>
