<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="entrance-date-picker1" 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>
      <el-table ref="entranceDataTable" resizable tooltip-effect="dark" style="width: 100%" v-loading="dataLoading"  :page-size="20" :data-read-url="getDataURL" :auto-get-data="false">
        <el-table-column type="index" label=" " width="55"></el-table-column>
        <el-table-column label="入口URL" min-width="70%">
          <template slot-scope="scope">
            <a href="javascript:void(0);" @click="linkto(scope.row.item)">{{scope.row.item}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="entrance" label="入口次数" min-width="10%"></el-table-column>
        <el-table-column prop="rate" label="比例" min-width="10%"></el-table-column>
        <el-table-column label="趋势" min-width="10%">
          <template slot-scope="scope">
            <a href="javascript:void(0);" @click="showTrend(scope.row.item)">时间趋势</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="时间趋势" :visible.sync="trendDialog">
      <div>
        <el-date-picker id="entrance-date-picker2" v-model="tdateRangeValue" type="daterange" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="请选择日期范围" @change="tdateRangeChange"></el-date-picker>&nbsp;
        <el-button :plain="true" @click="trendCheck">查看</el-button>
      </div>
      <div class="chart-trend" style="margin-top:20px" v-loading="chartLoading">
        <echarts :options="trendOption" height="300px"></echarts>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Echarts from '@src/components/Echarts.vue'
export default {
  components: {
    echarts: Echarts
  },
  data() {
    return {
      chartLoading: false,
      dataLoading: false,
      entrance: [], // 入口URL点击量数据
      dateRangeValue: [],
      startDate: '', // 搜索框日期范围
      endDate: '',
      trend: [], // 入口URL访问趋势
      xData: [], // 趋势图表配置
      yData: [],
      item: '', // 当前URL
      trendDialog: false, // 访问趋势框
      tdateRangeValue: [],
      tStartDate: '', // 趋势日期范围
      tEndDate: '',
      trendOption: {}

    }
  },
  methods: {
    dateRangeChange(val) {
      if (val && val instanceof Array) {
        this.startDate = val[0]
        this.endDate = val[1]
      } else {
        this.dateRangeValue = []
        this.startDate = ''
        this.endDate = ''
      }
    },
    tdateRangeChange(val) {
      if (val && val instanceof Array) {
        this.tStartDate = val[0]
        this.tEndDate = val[1]
      } else {
        this.tdateRangeValue = []
        this.startDate = ''
        this.endDate = ''
      }
    },
    refresh() {
      this.loadEntrance()
    },
    check() {
      this.loadEntrance()
    },
    async showChart() {
      // 指定图表的配置项和数据
      var option = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: [this.item]
        },
        xAxis: {
          boundaryGap: false,
          data: this.xData
        },
        yAxis: {

        },
        series: [
          {
            name: this.item,
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.yData
          }
        ]
      }
      this.trendOption = option
    },
    showTrend(item) {
      this.item = item
      this.tdateRangeValue = this.dateRangeValue
      this.tStartDate = this.startDate
      this.tEndDate = this.endDate
      this.trendDialog = true
      this.loadTrend()
    },
    trendCheck() {
      this.loadTrend()
    },
    linkto(url) {
      window.open(url)
    },
    async loadEntrance() {
      this.$refs.entranceDataTable.getData()
    },
    async loadTrend() {
      this.chartLoading = true
      let startdate = this.tStartDate
      let enddate = this.tEndDate
      let item = this.item
      let type = 'URL'
      let subType = 'Entrance'
      let resp
      try {
        resp = await axios.get('/ui/stats/trend', {
          params: {
            startdate,
            enddate,
            item,
            type,
            subType
          }
        })
        this.chartLoading = false
      } catch (e) {
        this.chartLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.trend = resp.data.data
      this.xData = this.trend.map(function (obj) { return obj.date })
      let yName = this.item
      this.yData = this.trend.map(function (obj) { return obj[yName] })
      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)
      }
    },
    getDataURL(){
      let params = {
        startdate: this.startDate,
        enddate: this.endDate
      }
      return ['/ui/stats/entrance', {params}]
    }
  },
  async created() {
    await this.initDate()
    this.loadEntrance()
  }
}

</script>

<style scoped>

</style>
