<template>
  <div>
    <div class="label">报建用户分布</div>
    <div id="applyuser"></div>
  </div>
</template>

<script>
  import co from 'co'
  import {HttpResetClass} from 'vue-client'

  let getData = function* (self) {
    let load = new HttpResetClass()
    load.load("POST", 'rs/sql/singleTableParam',
      {data: self.sqlcondition},
      {resolveMsg: null, rejectMsg: null})
      .then((res) => {
        let result = {}
        result.name = '民用'
        result.value = res.data[0]['民用']
        self.usernum.push(result)
        let result1 = {}
        result1.name = '非民用'
        result1.value = res.data[0]['非民用']
        self.usernum.push(result1)
        let result2 = {}
        result2.name = '民用锅炉'
        result2.value = res.data[0]['民用锅炉']
        self.usernum.push(result2)
      })
  }

  export default {
    name: "inspec-distri",
    data() {
      return {
        xc: '',
        usernum: [
          {value: 2000, name: '民用'},
          {value: 1215, name: '非民用'},
        ],
        totaluser: '',
      }
    },
    methods: {
      setBar() {
        this.xc = this.$echarts.init(document.getElementById('applyuser'))
        var scale = 0.8;
        let echartData = this.usernum
        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: 10 * scale,
            padding: [11, 0, 11, 0]
          },
          blue: {
            color: '#15dce2',
            fontSize: 14 * scale,
            align: 'center',
            padding: [10, 0]
          }
        }
        var total = 0; //用户总和
        echartData.forEach(function (value) {
          total += value.value;
        });
        this.xc.setOption({
          title: [
            {
              text: '总用户数',
              left: '38%',
              top: '38%',
              textStyle: {
                color: '#ffffff',
                fontSize: 14 * scale,

              }
            },
            {
              text: total,
              left: '38%',
              top: '47%',
              padding: [10 * scale, 10 * scale],
              textStyle: {
                color: '#00e2ea',
                fontSize: 14 * scale,
              }
            }],
          legend: {
            orient: 'vertical',
            icon: 'circle',
            x: '76%',
            bottom: '6%',
            right: '1%',
            itemWidth: 20 * scale,
            itemHeight: 10 * scale,
            textStyle: {
              fontSize: 12 * scale,
              padding: [0, 0, 0, 4 * scale],
              color: '#ffffff'
            },
            data: ['民用', '非民用']
          },
          series: [
            {
              name: 'Line 1',
              type: 'pie',
              animation: false,
              clockWise: false,
              radius: ['52%', '53%'],
              center: ['46%', '49%'],
              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: ['60%', '85%'],
              center: ['46%', '49%'],
              minAngle: 15,//最小角度
              startAngle: 300, //起始角度
              hoverAnimation: false,
              color: ['#6fd9d0', '#ede61f'],
              label: {
                normal: {
                  borderWidth: 20 * scale,
                  borderRadius: 4 * scale,
                  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 '{blue|' + params.name + '}{blue|' + params.value + '}\n\n';
                  },
                  padding: [0, -106 * scale],
                  rich: rich,
                }
              },

              labelLine: {
                normal: {
                  length: 15 * scale,
                  length2: 100 * scale,
                  lineStyle: {
                    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{yellow|     ' + params.value + '}\n{blue|    ' + percent + '%}';
                    },
                  }
                }
              },
              data: echartData
            }, {
              name: 'Line 2',
              type: 'pie',
              animation: false,
              clockWise: false,
              radius: ['93%', '94%'],
              center: ['46%', '49%'],
              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)
      },
      searchdata() {
        var self = this
        let sqldata = getData(this)
        co(sqldata)
      },
      dataunit(value) {
        var index = value.toString().length
        if (index > 5) {
          var i = value.toString().substring(0, index - 4)
          var s = value.toString().substring(index - 4, index - 3)
          this.totaluser = i + '.' + s + '万'
        }
      },
    },
    watch: {
      'usernum'(val) {
        var value = this.usernum[0].value + this.usernum[1].value + this.usernum[2].value
        this.dataunit(value)
        this.setBar()
      }
    },
    ready() {
      var items = '(aa.qq-cc.ee) 民用, bb.ww 非民用, cc.ee 民用锅炉'
      var tablename = '(select count(f_userinfo_id) qq from t_userinfo where f_user_type=\'民用\') aa,\n' +
        '\n' +
        '(select count(f_user_type) ww from t_userinfo where f_user_type=\'非民用\') bb,\n' +
        '\n' +
        '(select count(b.f_userinfo_id) ee from T_USERINFO a, T_DEVICES b where a.F_USERINFO_ID=b.f_userinfo_id and F_DEVICES_TYPE=\'壁挂炉\' and a.f_user_type=\'民用\') cc'
      var condition = "1=1"
      this.sqlcondition = {items: items, tablename: tablename, condition: condition}
      // this.searchdata()
      this.setBar()
    }
  }
</script>

<style scoped>

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


#applyuser{
    height: 68.3%;
    width: 100%;
  }



</style>
