<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: '20%',
            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: "3%", 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',{f_filiale:this.$parent.$parent.f_filiale}, 'resdata')
        co(getData)
      },
      //整理数据
      processData() {
       var self=this
        this.resdata.forEach(function (s) {
          let item = {}
          item.name = s.dates
          item.value = s.num
          self.nousegasdata.push(item)

        })
        console.log(this.nousegasdata)
      },
      // 钻取入口
      drillclick() {
        console.log(this.title)
        let total = []
        for (var i = this.nousegasdata.length; i > 0; i--) {
          total.push(this.nousegasdata[i - 1])
        }
        var date=eval(Util.toStandardDateString().toString().substring(0, 4) + '-2') + '-01-01 00:00:00'
        var initdatatr = {
          startDate: date,
          endDate: Util.toStandardDateString() + ' 00:00:00',
          param: '',
          param1: '',
          listparam: ' al.f_hand_date>=to_date(\''+date+'\',\'yyyy-mm-dd hh24:mi:ss\') and  '
        }
        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>
