<template>
  <div>
    <div class="f1">
      <div class="label">客户</div>
      <div class="f2">
        <table>
          <tr>
            <td class="td-1"></td>
            <td><span v-model="totaluser">{{totaluser}}</span><br/>现有总客户</td>
            <td class="td-1"></td>
            <td><span v-model="apply">{{apply}}</span><br/>开发客户数</td>
            <td class="td-1"></td>
            <td><span v-model="hiddenuser">{{hiddenuser}}</span><br/>潜在客户数</td>
          </tr>
        </table>
      </div>
    </div>
    <div id="userdata" class="f3"></div>
  </div>
</template>

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

  export default {
    name: "user-module",
    props: {
      listenheight: String,
    },
    data() {
      return {
        xc: '',
        usernum: [],     //用户数据
        usernumstring: [],
        totaluser: '',   //总用户数
        hiddenuser: '', //潜在用户数
        unit: [],        //用户数单位
        resdata: [],    //用户数据
        resdatapie: [],  //饼图数据
        resdataapply: 0,  //报建用户数
        apply: '',  //报建用户数
      }
    },
    methods: {
      //绘图
      setBar() {
        this.xc = this.$echarts.init(document.getElementById('userdata'))
        var scale = 0.8;
        DataSql.dataunitarray(this, 'usernum', 'usernumstring')
        var echartData = this.usernum
        var echartData1 = this.usernumstring
        var rich = {
          blue: {
            color: '#15dce2',
            fontSize: 12 * scale,
            align: 'center',
            padding: [15, 0, 15, 0]
          }
        }
        var total = 0; //用户总和
        var result = ''
        echartData.forEach(function (value) {
          total += value.value;
        });
        var index = total.toString().length
        if (index > 4) {
          var t = total.toString().substring(0, index - 4)
          var s = total.toString().substring(index - 4, index - 3)
          var l = total.toString().substring(index - 3, index - 2)
          if (l > 4) {
            s = eval(s + '+1')
          }
          if (s > 9) {
            s = 0
            t = eval(t + '+1')
          }
          result = t + '.' + s + '万'
        } else {
          result = value
        }
        this.xc.setOption({
          title: [
            {
              text: '总客户数',
              left: '41.5%',
              top: '38%',
              textStyle: {
                color: '#ffffff',
                fontSize: 12 * scale,

              }
            },
            {
              text: result,
              left: '43%',
              top: '47%',
              padding: [1000 * scale, 1000 * scale],
              textStyle: {
                color: '#00e2ea',
                fontSize: 120 * scale,

              }
            }],
          legend: {
            orient: 'horizontal',
            icon: 'circle',
            x: 'center',
            bottom: '6%',
            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: ['35%', '36%'],
              center: ['50%', '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: ['40%', '50%'],
              center: ['50%', '49%'],
              minAngle: 15,//最小角度
              startAngle: 300, //起始角度
              hoverAnimation: false,
              color: ['#6fd9d0', '#ede61f', '#6d9bdc'],
              label: {
                normal: {
                  borderWidth: 20 * scale,
                  borderRadius: 4 * scale,
                  formatter: function (params, ticket, callback) {
                    for (var i = 0; i < echartData1.length; i++) {
                      if (echartData1[i].name == params.name) {
                        return '{blue|' + params.name + '}{blue|' + echartData1[i].value + '}\n';
                      }
                    }
                  },
                  padding: [100, -100 * scale],
                  rich: rich,
                }
              },
              labelLine: {
                normal: {
                  length: 25 * scale,
                  length2: 90 * scale,
                }
              },
              data: echartData
            }, {
              name: 'Line 2',
              type: 'pie',
              animation: false,
              clockWise: false,
              radius: ['54%', '55%'],
              center: ['50%', '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() {
        let sqldata = DataSql.getData(this, 'user',{}, 'resdata')
        co(sqldata)
        let sqldatapie = DataSql.getData(this,'useri',{}, 'resdatapie')
        co(sqldatapie)
        let sqldataone = DataSql.getData(this,'userapply',{}, 'resdataapply')
        co(sqldataone)
      },
    },
    watch: {
      // 监听用户数据并进行绘图
      'usernum': {
        deep: true,
        handler(val) {
          var value = this.usernum[0].value + this.usernum[1].value + this.usernum[2].value
          this.totaluser = DataSql.dataunit(value)
          this.setBar()
          this.$parent.loaddata++
        }
      },
      //监听浏览器大小变化
      'listenheight': {
        deep: true,
        handler(val) {
          if (this.listenheight !== '') {
            // this.$parent.$parent.changeresize(this.xc)
          }
        },
      },
      'resdata': {
        deep: true,
        handler(val) {
          this.usernum = []
          let result = {}
          result.name = '民用'
          result.value = this.resdata[0]['民用']
          this.usernum.push(result)
          let result1 = {}
          result1.name = '非民用'
          result1.value = this.resdata[0]['非民用']
          this.usernum.push(result1)
          let result2 = {}
          result2.name = '民用锅炉'
          result2.value = this.resdata[0]['民用锅炉']
          this.usernum.push(result2)
        }
      },
      'resdatapie': {
        deep: true,
        handler(val) {
          var totalnum = 0
          for (var i = 1; i < 6; i++) {
            let item0 = {}
            totalnum = totalnum + this.resdatapie[i].totalnum
          }
          this.hiddenuser = DataSql.dataunit(totalnum / 5,)
        }
      },
      'resdataapply': {
        deep: true,
        handler(val) {

          this.apply = DataSql.dataunit(this.resdataapply[0].totalnum)
        }
      }
    },

    ready() {
      // 拼接sql，请求后台
      this.searchdata()
    }
  }
</script>

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

  /*标题及用户数据表格样式*/
  .f1 {
    height: 31%;
    width: 100%;
  }

  /*用户数据表格样式*/
  .f2 {
    /*height: 72px;*/
    /*width: 450px;*/
    /*margin-left: 20px*/
    padding: 2% 1%;
    height: 92%;
    width: 100%;
  }

  /*echarts绘制区域样式*/
  .f3 {
    height: 68.3%;
    width: 100%;
    padding-top: 3%;
  }

  table {
    width: 100%;
    height: 100%;
  }

  td {
    /*height: 72px;*/
    /*width: 140px;*/
    height: 100%;
    width: 20%;
    font-size: 12px;
    text-align: center;
    color: #ffffff;
  }

  /*td加载图片*/
  .td-1 {
    /*width: 40px;*/
    width: 10%;
    /*<img src="../../../static/ManageHomePic/椭圆4.png">*/
    background-image: url("../../../../static/ManageHomePic/椭圆4.png");
    background-repeat: no-repeat;
    background-size: 80% 40%;
    background-position: center;
  }

  span {
    font-size: 12px;
    color: #69d8d2;
  }
</style>
