<template lang="html">
  <div id="charts">
    <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/logic/BusinessType',
    {data: {startDate: self.startdate, endDate: self.enddate, f_orgid: self.orgid}},
    {resolveMsg: null, rejectMsg: null})
    .then((res) => {
      self.texttile = '用户类型'
      let numdata = []
      let pricedata = []
      numdata[0] = res.data.num
      pricedata[0] = res.data.money
      self.set(numdata, pricedata)
    })
}
export default {
  props: {
    startdate: {
      type: String,
      default: Util.toStandardDateString()
    },
    enddate: {
      type: String,
      default: Util.toStandardDateString()
    },
    orgid: {
      type: String
    }
  },
  data () {
    title: '业务类型'
    return {
      texttile: '',
      xc: ''
    }
  },
  methods: {
    set (numdata, pricedata) {
      this.xc = echarts.init(document.getElementById('sm'))
      this.xc.setOption({
        color: ['#894BE5', '#FFC0CB'],
        title: {
          text: '增值业务散列图',
          subtext: '数据查询来源',
          x: 'left'
        },
        tooltip: {
          trigger: 'item',
          formatter: function (params) {
            let str = ''
            if (params.seriesName.includes('笔数')) {
              str += params.seriesName + ': ' + params.value + '笔'
            } else {
              str += params.seriesName + ': ' + params.value + '元'
            }
            return str
          }
        },
        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: ['增值业务数据']
          }
        ],
        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>
