<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="stickTime-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="stickTime" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="date" label="日期"></el-table-column>
        <el-table-column prop="stickTotalTime" label="会话停留时间"></el-table-column>
        <el-table-column prop="stickTime" 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,
      stickTime: [], // 停留时间数据
      dateRangeValue: [],
      startDate: '', // 搜索框日期范围
      endDate: '',
      echart: [], // 图表数据
      chartOptions: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['会话停留时间', '页均停留时间']
        },
        xAxis: {
          boundaryGap: false,
          data: this.echart.map(function (obj) { return obj.date })
        },
        yAxis: {
        },
        series: [
          {
            name: '会话停留时间',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.echart.map(function (obj) { return obj.stickTime })
          },
          {
            name: '页均停留时间',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.echart.map(function (obj) { return obj.stickTotalTime })
          }
        ]
      }
      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.loadStickTime()
    },
    check() {
      this.loadStickTime()
    },
    async loadStickTime() {
      this.dataLoading = true
      let startdate = this.startDate
      let enddate = this.endDate
      let resp
      try {
        resp = await axios.get('/ui/stats/sticktime', {
          params: {
            startdate,
            enddate
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.stickTime = resp.data.data
      this.echart = resp.data.echart
      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.loadStickTime()
  }
}

</script>

<style scoped>

</style>
