<template>
  <div class="right-list-toobar">
    <el-toolbar>
      <el-button :plain="true" @click="refresh"><i class="fa fa-refresh"></i>刷新</el-button>
      <el-col>当前报表：各时段最高在线人数</el-col>
      <el-col align="right">
        <el-date-picker id="onlineVistor-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 :plain="true" @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="onlineVisitor" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="item" label="时段"></el-table-column>
        <el-table-column prop="_15Min" label="会话15分钟失效"></el-table-column>
        <el-table-column prop="_10Min" label="会话10分钟失效"></el-table-column>
        <el-table-column prop="_5Min" label="会话5分钟失效"></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,
      onlineVisitor: [], // 全站点击量数据
      dateRangeValue: [],
      startDate: '', // 搜索框开始、结束时间
      endDate: '',
      chartOptions: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['15Min', '10Min', '5Min']
        },
        xAxis: {
          boundaryGap: false,
          data: this.onlineVisitor.map(function (obj) { return obj.item.split(' — ')[0] })
        },
        yAxis: {

        },
        series: [
          {
            name: '15Min',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.onlineVisitor.map(function (obj) { return obj._15Min })
          },
          {
            name: '10Min',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.onlineVisitor.map(function (obj) { return obj._10Min })
          },
          {
            name: '5Min',
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.onlineVisitor.map(function (obj) { return obj._5Min })
          }
        ]
      }
      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.loadOnlineVisitor()
    },
    check() {
      this.loadOnlineVisitor()
    },
    async loadOnlineVisitor() {
      this.dataLoading = true
      let startdate = this.startDate
      let enddate = this.endDate
      let resp
      try {
        resp = await axios.get('/ui/stats/onlinevisitor', {
          params: {
            startdate,
            enddate
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.onlineVisitor = resp.data.data
      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.loadOnlineVisitor()
  }
}

</script>

<style scoped>
  .padding-top-bottom .search-bar {
    padding-bottom: 5px;
  }

</style>
