<template>
  <div>
    <div class="label">安检状态统计</div>
    <div id="statestatistics"></div>
  </div>
</template>

<script>
  import * as DataSql from '../../../stores/DataSql'
  import co from 'co'


  export default {
    name: "StateStatistics",
    data() {
      return {
        xc: '',
        resdata: [],
        usernum: [],
        totaluser: '',
      }
    },
    methods: {
      setBar() {
        this.xc = this.$echarts.init(document.getElementById('statestatistics'))
        var scale=this.$parent.$parent.$parent.scale
        let echartData = this.usernum
        var legendname = []
        this.usernum.forEach(function (s) {
          legendname.push(s.name)
        })
        var rich = {
          yellow: {
            color: "#ffc72b",
            fontSize: 10 * scale,
            padding: [5, 4],
            align: 'center'
          },
          total: {
            color: "#ffc72b",
            fontSize: 10 * scale,
            align: 'center'
          },
          white: {
            color: "#fff",
            align: 'center',
            fontSize: 14 * scale,
            padding: [6, 4]
          },
          blue: {
            color: '#e1e52a',
            fontWeight:'bolder',
            fontSize: 16 * scale,
            align: 'center',
            padding: [6, 4]
          },
          hr: {
            borderColor: '#64d8d4',
            width: '100%',
            borderWidth: 0.5,
            height: 0
          },
        }
        var total = 0; //用户总和
        echartData.forEach(function (value) {
          total += value.value;
        });
        this.xc.setOption({
          title: [],
          legend: {
            orient: 'horizontal',
            icon: 'circle',
            x: 'center',
            itemGap: 20,
            y: '80%',
            itemWidth: 20 * scale,
            itemHeight: 10 * scale,
            textStyle: {
              fontSize: 12 * scale,
              // padding: [0, 0, 0, 4 * scale],
              color: '#ffffff'
            },
            data: legendname
          },
          grid: {
            left: '35%',
            right: '35%',
            top: '0',
            bottom: '0'
          },
          series: [
            {
              name: 'Line 1',
              type: 'pie',
              animation: false,
              clockWise: false,
              radius: ['25%', '26%'],
              center: ['50%', '45%'],
              itemStyle: {
                normal: {
                  label: {
                    show: false
                  },
                  labelLine: {
                    show: false
                  },
                }
              },
              hoverAnimation: false,
              tooltip: {
                show: false
              },
              data: [{
                value: 100,
                name: '02',
                itemStyle: {
                  normal: {
                    color: '#29b7ff',//未完成的圆环的颜色
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  }
                }
              }]
            },
            {
              name: '总用户数',
              type: 'pie',
              radius: ['30%', '45%'],
              center: ['50%', '45%'],
              minAngle: 15,//最小角度
              startAngle: 300, //起始角度
              hoverAnimation: false,
              color: ['#6fd9d0', '#ede61f', '#2d44f8'],
              label: {
                normal: {
                  borderWidth: 1,
                  borderRadius: 4,
                  backgroundColor: '#004b5c',
                  borderColor: '#64d8d4',
                  formatter: function (params, ticket, callback) {
                    var total = 0; //用户总数量
                    var percent = 0;
                    echartData.forEach(function (value, index, array) {
                      total += value.value;
                    });
                    percent = ((params.value / total) * 100).toFixed(1);
                    return '{white|' + params.name + '}\n{hr|}\n{blue|  ' + params.value + '}{blue|   ' + percent + '%}';
                  },
                  rich: rich,
                }
              },
              labelLine: {
                normal: {
                  length: 20,
                  length2: 30,
                }
              },
              data: echartData
            },
            {
              name: 'Line 2',
              type: 'pie',
              animation: false,
              clockWise: false,
              radius: ['50%', '51%'],
              center: ['50%', '45%'],
              itemStyle: {
                normal: {
                  label: {
                    show: false
                  },
                  labelLine: {
                    show: false
                  },
                }
              },
              hoverAnimation: false,
              tooltip: {
                show: false
              },
              data: [{
                value: 100,
                name: '02',
                itemStyle: {
                  normal: {
                    color: '#29b7ff',//未完成的圆环的颜色
                    label: {
                      show: false
                    },
                    labelLine: {
                      show: false
                    }
                  }
                }
              }
              ]
            }
          ]
        }, true)
      },
    },
    watch: {
      'usernum'(val) {
        var value = 0
        this.usernum.forEach(function (s) {
          value = value + s.value
        })
        this.totaluser = DataSql.dataunit(value)
        this.setBar()
        this.$parent.loaddata++
      },
    },
    ready() {

      let getData = DataSql.getData(this, 'safestatus',{f_filiale:this.$parent.$parent.f_filiale}, 'usernum')
      co(getData)
      // this.setBar()
    }
  }
</script>

<style scoped>
  .label {
    font-size: 0.85em;
    color: #fff;
    height: 8%;
    width: 100%;
    margin-left: 6%;
  }

  #statestatistics {
    height: 90%;
    width: 100%;
  }
</style>
