<template>
  <div style="width: 100%; text-align: center; height: 700px">
    <div id="codRate" ref="echarts" class="echarts"></div>
    <div id="anRate" ref="echarts" class="echarts"></div>
    <div id="tpRate" ref="echarts" class="echarts"></div>
    <div id="tnRate" ref="echarts" class="echarts"></div>
    <div id="phRate" ref="echarts" class="echarts"></div>
  </div>
</template>

<script>
import { getWaterRate } from '@/api/monitor';
import * as echarts from 'echarts';
export default {
  props: {
    deviceCode: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      background: true,
      listLoading: true,
      lazy: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        deviceCode: '',
      },
      type: 'pie',
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  watch: {
    deviceCode: function (newVal, oldVal) {
      this.queryForm.deviceCode = newVal;
      this.fetchData();
    },
  },
  created() {},
  methods: {
    setOption(opinion, opinionData, id, name) {
      this.charts = echarts.init(document.getElementById(id));
      this.charts.setOption({
        color: ['#1165bf', '#a26d1b'] /*饼状图的颜色*/,
        tooltip: {
          trigger: 'item',
          formatter: '{b}({d}%)' /*饼状图触碰之后显示的注释文字*/,
        },
        legend: {
          left: 'center' /*标签文字排成一行*/,
          y: 'bottom' /*在垂直方向上的底部*/,
          data: opinion,
        },
        series: [
          {
            type: this.type, //饼状图,
            center: ['50%', '40%'], //显示位置,
            name: name,
            radius: ['0%', '50%'] /*空心圆的占比*/,
            itemStyle: {
              emphasis: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)',
              },
              normal: {
                borderWidth: 5 /*隔开的白色边界*/,
                borderColor: '#fff' /*border*/,
                // labelLine :{show:true}
              },
            },

            avoidLabelOverlap: false,
            label: {
              normal: {
                show: true,
                position: 'inner',
                textStyle: {
                  fontWeight: 200,
                  fontSize: 15, //文字的字体大小
                },
                formatter: '{d}%' /*饼状图内显示百分比*/,
                // data:['40%','60%'],
              },

              emphasis: {
                show: false /*空心文字出现*/,
              },
            },
            labelLine: {
              normal: {
                show: false,
              },
            },
            data: opinionData,
          },
        ],
      });
    },
    showData(row) {
      this.queryForm.deviceCode = row.deviceCode;
      this.fetchData();
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getWaterRate(this.queryForm);
      this.setOption(
        ['COD compliance rate', 'COD non-compliance rate'],
        [
          { value: data.codRate, name: 'COD compliance rate' },
          { value: 100 - data.codRate, name: 'COD non-compliance rate' },
        ],
        'codRate',
        'COD'
      );
      this.setOption(
        ['NH3-N compliance rate', 'NH3-N non-compliance rate'],
        [
          { value: data.anRate, name: 'NH3-N compliance rate' },
          { value: 100 - data.anRate, name: 'NH3-N non-compliance rate' },
        ],
        'anRate',
        'NH3-N'
      );
      this.setOption(
        ['TP compliance rate', 'TP non-compliance rate'],
        [
          { value: data.tpRate, name: 'TP compliance rate' },
          { value: 100 - data.tpRate, name: 'TP non-compliance rate' },
        ],
        'tpRate',
        'TP'
      );
      this.setOption(
        ['TN compliance rate', 'TN non-compliance rate'],
        [
          { value: data.tnRate, name: 'TN compliance rate' },
          { value: 100 - data.tnRate, name: 'TN non-compliance rate' },
        ],
        'tnRate',
        'TN'
      );
      this.setOption(
        ['PH compliance rate', 'PH non-compliance rate'],
        [
          { value: data.phRate, name: 'PH compliance rate' },
          { value: 100 - data.phRate, name: 'PH non-compliance rate' },
        ],
        'phRate',
        'PH'
      );
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-container {
  width: 100%;
  .vab-query-form {
    margin-bottom: 30px;
    .el-input {
      width: 180px;
      margin-right: 20px;
    }
  }
}
.echarts {
  width: 240px;
  height: 240px;
  float: left;
}
</style>
