<template>
  <div class="right-list-toobar">
    <el-toolbar>
      <el-button @click="refresh"><i class="fa fa-refresh"></i>刷新</el-button>
      <el-col>当前报表：来源组成</el-col>
      <el-col align="right">
        <el-date-picker id="source-date-picker" v-model="dateRangeValue" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="请选择日期范围" @change="dateRangeChange"></el-date-picker>&nbsp;
        <el-button @click="check" class="button" type="primary"><slot name="button">查看</slot></el-button>
      </el-col>
    </el-toolbar>
    <div v-loading="dataLoading">
      <echarts :options="chartOptions" height="300px"></echarts>
      <div style="margin:10px">

      </div>
      <el-table :data="source" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="date" label="日期"></el-table-column>
        <el-table-column prop="direct" label="直接输入(UV)"></el-table-column>
        <el-table-column prop="searchEngine" label="搜索引擎(UV)"></el-table-column>
        <el-table-column prop="qrcode" label="二维码扫描(UV)"></el-table-column>
        <el-table-column prop="referer" label="来源网站(UV)"></el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import Echarts from '@src/components/Echarts.vue'
export default {
  components: {
    echarts: Echarts
  },
  data() {
    return {
      dataLoading: false,
      source: [], // 来源组成
      dateRangeValue: [],
      startDate: '', // 日期范围
      endDate: '',
      chartOptions: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var sourceOption = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c}'
        },
        legend: {
          orient: 'vertical',
          top: 'center',
          right: '50px',
          data: ['直接输入', '搜索引擎', '二维码扫描', '来源网站']
        },
        series: [
          {
            name: '来源组成',
            type: 'pie',
            center: ['45%', '50%'],
            radius: [0, '55%'],
            data: [
              { value: this.source[0].direct, name: '直接输入' },
              { value: this.source[0].searchEngine, name: '搜索引擎' },
              { value: this.source[0].qrcode, name: '二维码扫描' },
              { value: this.source[0].referer, name: '来源网站' }
            ],
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      }
      this.chartOptions = sourceOption
    },
    dateRangeChange(val) {
      if (val && val instanceof Array) {
        this.startDate = val[0]
        this.endDate = val[1]
      } else {
        this.dateRangeValue = []
        this.startDate = ''
        this.endDate = ''
      }
    },
    refresh() {
      this.loadSource()
    },
    check() {
      this.loadSource()
    },
    async loadSource() {
      this.dataLoading = true
      let startdate = this.startDate
      let enddate = this.endDate
      let resp
      try {
        resp = await axios.get('/ui/stats/source', {
          params: {
            startdate,
            enddate
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.source = resp.data.data
      this.showChart()
    },
    async initDate() {
      let res = await axios.get('/ui/stats/summary/initdate')
      if (res.data.status === 1) {
        this.startDate = res.data.data.startDate
        this.endDate = res.data.data.endDate
        this.dateRangeValue = []
        this.dateRangeValue.push(this.startDate)
        this.dateRangeValue.push(this.endDate)
      }
    }
  },
  async created() {
    await this.initDate()
    this.loadSource()
  }
}

</script>

<style scoped>

</style>
