<template>
  <div>
    <div class="label">安检用户类型</div>
    <div id="customertype"></div>
  </div>
</template>

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

  export default {
    name: "CustomerType",
    data() {
      return {
        xc: '',
        resdata: [],
        usernum: [],
        totaluser: '',
      }
    },
    methods: {
      setBar() {
        this.xc = this.$echarts.init(document.getElementById('customertype'))
        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 ,
            padding: [5, 4],
            align: 'center'
          },
          total: {
            color: "#ffc72b",
            fontSize: 10 ,
            align: 'center'
          },
          white: {
            color: "#fff",
            align: 'center',
            fontSize: 14 ,
            padding: [6, 4]
          },
          blue: {
            color: '#e1e52a',
            fontWeight:'bolder',
            fontSize: 16 ,
            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',
            y: '80%',
            itemGap: 20,
            itemWidth: 20 ,
            itemHeight: 10 ,
            textStyle: {
              fontSize: 12 ,
              // 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%', '40%'],
              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%', '40%'],
              minAngle: 15,//最小角度
              startAngle: 300, //起始角度
              hoverAnimation: false,
              color: ['#6fd9d0', '#ede61f', '#4A95FF'],
              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%', '40%'],
              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++
      },
      'resdata': {
        deep: true,
        handler(val) {
          console.log(this.resdata, '测试安检状态据')
          console.log(this.usernum)
          this.usernum = this.resdata
          console.log(this.usernum)
        },
      },
    },
    ready() {

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

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

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