<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="stat-site-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:5px">
      </div>
      <el-table :data="tableSite" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="date" label="时段"></el-table-column>
        <el-table-column prop="rate" label="PV比例"></el-table-column>
        <el-table-column prop="PV" label="PV"></el-table-column>
        <el-table-column prop="UV" label="UV"></el-table-column>
        <el-table-column prop="IP" label="IP"></el-table-column>
        <el-table-column prop="returnVisitor" label="回头客数据"></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,
      site: [], // 全站点击量chart数据
      tableSite: [], // 全站点击量table数据
      dateRangeValue: [],
      startDate: '', // 搜索框日期范围
      endDate: '',
      chartOptions: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['PV', 'UV', 'IP']
        },
        xAxis: {
          boundaryGap: false,
          data: this.site.map(function (obj) { return obj.date })
        },
        yAxis: {

        },
        series: [
          {
            name: 'UV',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.site.map(function (obj) { return obj.UV })
          },
          {
            name: 'PV',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.site.map(function (obj) { return obj.PV })
          },
          {
            name: 'IP',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.site.map(function (obj) { return obj.IP })
          }
        ]
      }
      this.chartOptions = option
    },
    dateRangeChange(val) {
      if (val && val instanceof Array) {
        this.startDate = val[0]
        this.endDate = val[1]
      } else {
        this.dateRangeValue = []
        this.startDate = ''
        this.endDate = ''
      }
    },
    refresh() {
      this.loadSite()
    },
    check() {
      this.loadSite()
    },
    async loadSite() {
      this.dataLoading = true
      const startdate = this.startDate
      const enddate = this.endDate
      let resp
      try {
        resp = await axios.get('/ui/stats/site', {
          params: {
            startdate,
            enddate
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.site = resp.data.data
      this.tableSite = resp.data.tableDT
      this.showChart()
    },
    async initDate() {
      this.dataLoading = true
      let res
      try {
        res = await axios.get('/ui/stats/summary/initdate')
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      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.loadSite()
  }
}

</script>

<style scoped>

</style>
