<template>
  <el-card>
    <div :id="id" :class="className" :style="{height:height,width:width}" />
  </el-card>
</template>

<script>
import { index } from "@/api/system/baseForm";
import * as echarts from 'echarts'
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null,
      data: [],
      landMap: new Map()
    }
  },
  mounted() {
    this.initChart()
    this.getData()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    async getData () {
      const res = await index({
        table_name: "asset_safety_inspections",
        page: 1,
        page_size: 999,
        sort_name: "land_id"
      })
      console.log(res)
      this.data = { };
      res.data.forEach(item => {
        if (item.land_id) {
          if (this.data.hasOwnProperty(item.land_id)) {
            this.data[item.land_id] += 1;
          } else {
            this.data[item.land_id] = 1;
            this.landMap.set(item.land_id, item.land_id_lands_id_relation)
          }
        }
      })
      this.setOption();
    },

    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))

    },
    setOption () {

      // prettier-ignore
      let dataAxis = Object.keys(this.data).map(i => this.landMap.get(Number(i))?.name);
      let data = Object.values(this.data);
      let yMax = 500;
      let dataShadow = [];
      for (let i = 0; i < data.length; i++) {
        dataShadow.push(yMax);
      }
      this.chart.setOption({
        title: {
          text: '安全巡检',
        },
        xAxis: {
          data: dataAxis,
          axisLabel: {
            inside: true,
            color: '#fff'
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          z: 10
        },
        yAxis: {
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#999'
          }
        },
        dataZoom: [
          {
            type: 'inside'
          }
        ],
        series: [
          {
            type: 'bar',
            showBackground: true,
            itemStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' }
              ])
            },
            emphasis: {
              itemStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#2378f7' },
                  { offset: 0.7, color: '#2378f7' },
                  { offset: 1, color: '#83bff6' }
                ])
              }
            },
            data: data
          }
        ]
      })
    }
  },
  watch: {

  }
}
</script>
