<template lang="html">
  <div id="charts">
    <div id="bm" :style="{width:'100%',height:'100%'}"></div>
  </div>
</template>

<script>
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() //qibiao-shouqi
  load.load('POST', 'api/af-revenue/sql/qibiao-shouqi',
  {data: {startDate: self.startdate, endDate: self.enddate, f_orgid: `f_orgid = ${self.orgid}`,condition: '1=1'}},
  {resolveMsg: null, rejectMsg: null})
  .then((res) => {
    self.texttile = '用户类型'
    // let arrparams = []
    console.log('获取数据。。', res.data)
    let meter = Array.from(new Set(res.data.map(x => x.biaoleixing)))
    let jmeter = []
    let qkmeter = []
    let jkmeter = []
    let webmeter = []
    jmeter.push(res.data.filter(x => x.biaoleixing === '机表').map(item => item.yongqi).reduce((x, y) => x + y, 0).toFixed(2))
    jmeter.push(res.data.filter(x => x.biaoleixing === '机表').map(item => item.jine).reduce((x, y) => x+y, 0).toFixed(2))

    qkmeter.push(res.data.filter(x => x.biaoleixing === '气量卡表').map(item => item.yongqi).reduce((x, y) => x + y, 0).toFixed(2))
    qkmeter.push(res.data.filter(x => x.biaoleixing === '气量卡表').map(item => item.jine).reduce((x, y) => x+y, 0).toFixed(2))

    jkmeter.push(res.data.filter(x => x.biaoleixing === '金额卡表').map(item => item.yongqi).reduce((x, y) => x + y, 0).toFixed(2))
    jkmeter.push(res.data.filter(x => x.biaoleixing === '金额卡表').map(item => item.jine).reduce((x, y) => x+y, 0).toFixed(2))

    webmeter.push(res.data.filter(x => x.biaoleixing === '物联网表').map(item => item.yongqi).reduce((x, y) => x + y, 0).toFixed(2))
    webmeter.push(res.data.filter(x => x.biaoleixing === '物联网表').map(item => item.jine).reduce((x, y) => x+y, 0).toFixed(2))

    self.set(meter, jmeter, qkmeter, jkmeter, webmeter)
  })
}
export default {
  props: {
    startdate: {
      type: String,
      default: Util.toStandardDateString()
    },
    enddate: {
      type: String,
      default: Util.toStandardDateString()
    },
    orgid: {
      type: String
    }
},
 data () {
   title: '气表售气量'
   return {
     texttile: '',
     xc: '',

     minyongvalue: [],
     feiminyongvalue: []
   }
 },
 methods: {
   set (meter, jmeter, qkmeter, jkmeter, webmeter) {
     this.xc = echarts.init(document.getElementById('bm'))
     this.xc.setOption({
       title : {
         text: '气表售气量',
         subtext: '数据查询来源',
         x: 'left'
       },
       tooltip : {
         trigger: 'axis'
       },
       legend: {
         x: 'left',
         y: 'bottom',
         data: meter
       },
       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 : ['气量','金额']
         }
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : [
        {
          name: '机表',
          type: 'bar',
          data: jmeter,
          markLine : {
            data : [
              {type : 'average', name: '平均值'}
            ]
          }
        },
        {
          name:'气量卡表',
          type:'bar',
          data: qkmeter,
          markLine : {
          data : [
              {type : 'average', name : '平均值'}
            ]
          }
        },
        {
          name:'金额卡表',
          type:'bar',
          data: jkmeter,
          markLine : {
          data : [
              {type : 'average', name : '平均值'}
            ]
          }
        },
        {
          name:'物联网表',
          type:'bar',
          data: webmeter,
          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>
