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

<script>
import * as echarts from 'echarts';
import { getInspectionData } from '@/api/index';
import { getDate, getPastTime } from '@/common/times';
export default {
  props: {
    time: {
      type: Array,
    },
    all: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      chartPie: null,
      yData: [],
      xData: [],
      maintenanceTime: '',
      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 = [];
      data.byUser.forEach(function (v, k) {
        if (k < 10) {
          xData.push(v.key);
          yData.push(v.data);
        }
      });
      this.xData = xData;
      this.yData = yData;
    },
    drawPieChart() {
      this.chartPie = echarts.init(document.getElementById('personnel'));
      this.chartPie.setOption({
        tooltip: {
          trigger: 'item',
          axisPointer: {
            type: 'shadow',
          },
        },
        title: {
          left: 'center',
          text: 'Statistics by personnel',
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            data: this.xData,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
          },
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.yData,
          },
        ],
      });
    },
    drawCharts() {
      this.drawPieChart();
    },
  },
};
</script>

<style scoped></style>
