<template lang="html">
  <div id="charts">
    <div id="GasPropertiesManager">
      <button class="button_export button_spacing" @click="change('f_user_type')">一级用气性质</button>
      <button class="button_export button_spacing" @click="change('f_gasproperties')">二级用气性质</button>
    </div>
    <div id="sm" :style="{width:'100%',height:'98%'}"></div>
  </div>
</template>

<script>
/* eslint-disable indent,key-spacing,no-labels */

import * as Util from '../../Util'
import co from 'co'
import echarts from 'echarts'
import {HttpResetClass} from 'vue-client'

let getData = function* (self) {
  let load = new HttpResetClass()
  load.load('POST', 'api/af-revenue/sql/gasPropertiesStatistics',
    {data: {startDate: self.startdate, endDate: self.enddate, f_orgid: self.orgid, groupkey: self.groupkey}},
    {resolveMsg: null, rejectMsg: null})
    .then((res) => {
      self.texttile = '用户类型'
      let arrparams = []
      let numdata = []
      let pricedata = []
      for (var i = 0; i < res.data.length; i++) {
        arrparams[i] = res.data[i].name
        numdata[i] = res.data[i].num
        pricedata[i] = res.data[i].money
      }
      self.set(arrparams, numdata, pricedata)
    })
}
export default {
  props: {
    startdate: {
      type: String,
      default: Util.toStandardDateString()
    },
    enddate: {
      type: String,
      default: Util.toStandardDateString()
    },
    orgid: {
      type: String
    }
  },
  data () {
    return {
      texttile: '',
      xc: '',
      groupkey: 'f_user_type'
    }
  },
  methods: {
    change (type) {
      this.groupkey = type
      this.searchdata()
    },
    set (arrparams, numdata, pricedata) {
      this.xc = echarts.init(document.getElementById('sm'))
      this.xc.setOption({
        title: {
          text: '用气性质散列图',
          subtext: '数据查询来源',
          x: 'left'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          x: 'left',
          y: 'bottom',
          data: ['条数', '用量']
        },
        toolbox: {
          show: true,
          feature: {
            dataView: {show: true, readOnly: false},
            magicType: {show: true, type: ['line', 'bar']},
            restore: {show: true},
            saveAsImage: {show: true}
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: arrparams
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '条数',
            type: 'bar',
            data: numdata,
            markLine: {
              data: [
                {type: 'average', name: '平均值'}
              ]
            }
          },
          {
            name: '用量',
            type: 'bar',
            data: pricedata,
            markLine: {
              data: [
                {type: 'average', name: '平均值'}
              ]
            }
          }
        ]
      })
    },
    searchdata () {
      let getGen = getData(this)
      co(getGen)
    }
  },
  ready () {
    this.searchdata()
  },
  watch: {
    'startdate' (val) {
      this.searchdata()
    },
    'enddate' (val) {
      this.searchdata()
    },
    'orgid' (val) {
      this.searchdata()
    }
  }

}
</script>
<style lang="less">
#GasPropertiesManager {
  display: flex;
  justify-content: center;
}
</style>
