<template>
    <div>
      <div class="label t1" @click="drillclick">{{title}}</div>
      <div id="NoGas"></div>
    </div>
</template>

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

  export default {
    name: "NoGas",
    data() {
      return {
        title: '长期未用气用户统计',
        xc: '',
        resdata: [],
        nousegasdata: [],
      }
    },
    methods: {
      setBar(id) {
        this.xc = this.$echarts.init(document.getElementById(id))
        var scale=this.$parent.$parent.$parent.scale
        var xAxisData = [];
        var legendData = ['用户数量'];
        var serieData = [];
        var metaDate = []

        var num = 0
        for (var i = this.nousegasdata.length; i > 0; i--) {
          xAxisData.push(this.nousegasdata[i - 1].name)
          metaDate.push(this.nousegasdata[i - 1].value)
          num = num + this.nousegasdata[i - 1].value
        }
        this.$parent.warningnum = this.$parent.warningnum + num
        xAxisData.push("")

        var yname = ' 天'
        for (var v = 0; v < legendData.length; v++) {
          var serie = {
            name: legendData[0],
            type: "bar",
            stack: "总量",
            barWidth: '30%',
            barGap: "20%",
            itemStyle: {
              normal: {
                color: '#6fd9d0',
                label: {
                  show: true,
                  textStyle: {
                    color: "#ffffff",
                    fontSize:16*scale
                  },
                  position: "top",
                  formatter: function (p) {
                    return p.value > 0 ? (p.value) : 0;
                  }
                }
              }
            },
            data: metaDate,
          }
          serieData.push(serie)
        }
        var colors = ["#036BC8", "#4A95FF", "#5EBEFC", "#2EF7F3", "#FFFFFF"]
        this.xc.setOption({
          title: {text: '', textAlign: 'left', textStyle: {color: "#fff", fontSize: "16*scale", fontWeight: "normal"}},
          color: colors,
          grid: {left: '4%', top: "18%", bottom: "5%", right: "5%", containLabel: true},
          dataZoom: [{
            type: 'inside',
            show: true,
            xAxisIndex: [0],
            left: '9%',
            bottom: -5,
            start: 2,
            end: 90 //初始化滚动条
          }],
          tooltip: {
            alwaysShowContent:true,
            backgroundColor:'rgba(2, 59, 63, 0.45)',
            // borderColor :'rgba(13, 109, 180)',
            borderWidth:0,
            hideDelay:100,
              padding:10*scale,
              textStyle:{
                  fontSize:18*scale
              },
            formatter: function (params) {
              if (params[0].axisValue == '') {
                return ''
              }
              var result = ''
              params.forEach(function (item) {
                if (item.seriesName !== 'ser') {
                  result += item.seriesName + " &nbsp;&nbsp;&nbsp;<span style='font-weight: bolder;color:#e1e52a;'> " + item.value + "</span></br>";
                }
              });
              return "<div style='text-align: center;height: 1px;font-weight: normal'>"+params[0].axisValue + '天</div><br/> ' + result
            },
            axisPointer: {type: 'line', lineStyle: {width: 0}},
            trigger: 'axis',
            extraCssText: 'width: auto;height: auto;'
          },
          xAxis: [
            {
              name: yname,
              type: 'category',
              nameTextStyle: {
                fontSize: 14 * scale,
                color: '#15dce2',
                padding: [40 * scale, 0, 0, -70 * scale],
              },
              axisLine: {show: true, lineStyle: {color: '#04669e', width: 1}},
              axisLabel: {interval: 0, textStyle: {color: '#ffffff', fontSize: 14 * scale}},
              axisTick: {show: false},
              data: xAxisData,
            },
          ],
          yAxis: [
            {
              name: '用户数量',
              nameTextStyle: {
                fontSize: 14 * scale,
                padding: [-10 * scale, 35 * scale, 0, 0],
              },
              axisTick: {show: false},
              splitLine: {show: true, lineStyle: {type: 'dashed', color: '#004371'}},
              axisLabel: {interval: 0, textStyle: {color: '#15dce2', fontSize: 14 * scale}},
              axisLine: {show: false, lineStyle: {color: '#15dce2', fontSize: 14 * scale}},
            },
          ],
          series: serieData
        })
      },
      // 查询sql
      sqlsearch() {
        // var date = DataSql.toBeforeDate(365)
        // DataSql.param = date + ' 00:00:00'
        let getData = DataSql.getData(this,  'longnotusegas',{condition:'1=1'}, 'resdata')
        co(getData)
      },
      //整理数据
      processData() {
        let num = [0, 0, 0, 0, 0, 0 ]
        num[0] = this.resdata[0].week
        num[1] = this.resdata[0].month
        num[2] = this.resdata[0].tmonth
        num[3] = this.resdata[0].harfyear
        num[4] = this.resdata[0].year
        num[5] = this.resdata[0].more
        var self = this
        let date = ['0-7', '7-30', '30-90', '90-180', '180-360','>360']
        var i = 5
        num.forEach( (row)=> {
          let item = {}
          item.name = date[i]
          item.value = num[i]
          self.nousegasdata.push(item)
          i--
        })
        console.log(this.nousegasdata)
      },
      // 钻取入口
      drillclick() {
        var total = this.nousegasdata
        var initdatatr = {
          param: '',
          param1: '',
          listparam: '1=1 '
        }
        this.$emit('drill', 'longnotusegas', 'nogas', this.title, initdatatr)
        this.$emit('drilldata', total)
      },
    },
    watch: {
      'resdata': {
        deep: true,
        handler(val) {
          console.log(this.resdata, '长期未用气用户统计')
          this.processData()
          this.setBar('NoGas')
          this.$parent.loaddata++
        }
      },
    },
    ready() {
      this.sqlsearch()
    }
  }
</script>

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

  #NoGas {
    height: 91%;
    width: 99%;
    padding: 1% 2% 3% 2%;

  }
  .t1{
    color: #4bf2ff;
  }
</style>
