<!--  -->
<template>
  <div class="chart-template chart-template-withperiod">
    <p>
      <span>安防设施建设达标率</span>
    </p>
    <div>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane
          v-for="t in types"
          :key="t.value"
          :label="t.text"
          :name="t.value"
        ></el-tab-pane>
      </el-tabs>
    </div>
    <div ref="chart"></div>
  </div>
</template>

<script>
import "./../../board.scss";
import * as echarts from "echarts";
import {
  findMaxIndex,
  getChartDOMSize,
  getNextPeriod,
  getPeriodText,
} from "../../utils.js";
import { orgGA38 } from "@/api/board/cockpit.js";
const types = [
  {
    text: "今天",
    value: "1",
  },
  {
    text: "近7天",
    value: "2",
  },
  {
    text: "近30天",
    value: "3",
  },
  {
    text: "近90天",
    value: "4",
  },
  {
    text: "本年",
    value: "5",
  },
];
export default {
  props: {
    orgId: {
      type: String,
      isRequired: true,
    },
    maxDiagram: {
      type: Number,
      isRequired: false,
    },
    orgName: {
      type: String,
      isRequired: false,
    },
    // title: {
    //   type: String,
    //   isRequired: true,
    // },
  },
  data() {
    return {
      data: {
        all: [],
        completed: [],
        name: [],
      },
      activeName: types[0].value,
    };
  },

  components: {},

  computed: {},

  watch: {
    orgId: {
      deep: true,
      handler(val) {
        this.resetTimer();
        this.getData();
      },
    },
    data: {
      deep: true,
      handler() {
        this.initMap();
      },
    },
  },

  created() {
    this.types = types;
    this.maxDisplay = 16;
    this.refreshTime = 1 * 10 * 1000;
    this.isMouseOver = false;
  },

  async mounted() {
    window.addEventListener("resize", this.windowResize);
  },
  beforeDestroy() {
    this.timer && clearInterval(this.timer);
    this.timer = null;

    window.removeEventListener("resize", this.windowResize);
  },

  methods: {
    handleClick() {
      this.resetTimer();
      this.getData();
    },
    handleMouseEnter() {
      this.isMouseOver = true;
    },
    handleMouseLeave() {
      this.isMouseOver = false;
    },
    async getData() {
      let r = (await orgGA38({ orgId: this.orgId, period: this.activeName }))
        .data;
      let data = {
        ga2021: [],
        ga2015: [],
        rate: [],
        name: [],
      };

      for (let p in r) {
        let item = r[p];
        if (this.maxDiagram && data.name.length >= this.maxDiagram) {
          break;
        }
        data.ga2021.push(item.ga382021);
        data.ga2015.push(item.ga382015);
        data.rate.push((item.reachRate * 100).toFixed(0));
        if (this.orgName && item.orgName.startsWith(this.orgName)) {
          data.name.push(item.orgName.substr(this.orgName.length));
        } else {
          data.name.push(item.orgName);
        }
      }
      this.data = data;

      // console.info(this.data);
    },

    windowResize() {
      this.myChart && this.myChart.resize();
    },
    initMap() {
      this.myChart && this.myChart.dispose();
      let c = this.$refs["chart"];

      // 基于准备好的dom，初始化echarts实例
      this.myChart = echarts.init(
        c
        // document.getElementById("commAlarmEvent_Chart")
      );

      let t = this;
      // 指定图表的配置项和数据
      var option = {
        //  color: ["#82D5AE"],
        tooltip: {
          trigger: "axis",
          confine: true,
          axisPointer: {
            type: "shadow",
          },

          // formatter: "完成率：{c}%",
        },
        legend: {
          show: true,
          textStyle: {
            color: "rgb(245, 245, 245)",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "2%",
          top: "20px",
          containLabel: true,
        },

        xAxis: [
          {
            type: "category",
            data: this.data.name,
            // data: this.mapdata.totalDataVo.xlist,
            axisTick: {
              alignWithLabel: true,
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: "solid",
                color: "rgb(245, 245, 245)", //左边线的颜⾊
              },
            },
            axisLabel: {
              interval: 0,
              rotate: -45,
              fontSize: 12,
              color: "#fff",
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            minInterval: 1,
            // max: function (value) {
            //   let m = value.max == Math.ceil(value.max) ? Math.ceil(value.max * 1.0001) : Math.ceil(value.max);
            //   return m
            // },
          },
        ],
        series: [
          {
            name: "GA38-2021",
            type: "bar",
            stack: "ga38",
            barWidth: "14",
            // label: {
            //   show: true,
            //   position: 'top',
            // },
            data: this.data.ga2021,
            // itemStyle: {
            //   normal: {
            //     label: {
            //       formatter: function (param) {
            //         // if (t.data.all[param.dataIndex]) {
            //         //   let rate = param.value / t.data.all[param.dataIndex];
            //         //   return (Math.floor(rate * 1000) / 10).toFixed(1) + "%";
            //         // } else {
            //         //   return "";
            //         // }
            //         return parseFloat(param.value).toFixed(0) + "%";
            //       },
            //       show: true,
            //       position: "top",
            //       textStyle: {
            //         fontWeight: "bolder",
            //         fontSize: "10",
            //         color: "rgb(245, 245, 245)",
            //       },
            //     },
            //   },
            // },
          },
          {
            name: "GA38-2015",
            type: "bar",
            stack: "ga38",
            barWidth: "14",
            // label: {
            //   show: true,
            //   position: 'top',
            // },
            data: this.data.ga2015,
          },
          {
            name: "达标率",
            type: "bar",
            stack: "ga38",
            barWidth: "14",
            tooltip: {
              valueFormatter: (v) => `${v}%`,
            },
            // label: {
            //   show: true,
            //   formatter:'{c}%'
            // },
            data: this.data.rate,
          },
        ],
      };
      if (option && typeof option === "object") {
        this.myChart.setOption(option);
      }
    },

    resetTimer() {
      this.timer && clearInterval(this.timer);
      this.timer = setInterval(() => {
        if (this.isMouseOver) {
          return;
        }
        this.activeName = getNextPeriod(this.types, this.activeName);
        this.getData();
      }, this.refreshTime);
    },
  },
};
</script>
<style lang="less" scoped></style>
