<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-toolbar>
    <div v-loading="dataLoading">
      <echarts :options="chartOptions" height="300px"></echarts>
      <div style="margin:10px">
      </div>
      <el-table :data="browser" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="item" label="名称"></el-table-column>
        <el-table-column prop="browser" label="PV数量"></el-table-column>
        <el-table-column prop="rate" label="比例"></el-table-column>
        <el-table-column label="趋势">
          <template slot-scope="scope">
            <a href="javascript:void(0);" @click="showTrend(scope.row)">{{scope.row.trend}}</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 趋势查看框 -->
    <el-dialog title="时间趋势" :visible.sync="trendDialog">
      <div>
        日期范围:&nbsp;
        <el-date-picker id="browser-date-picker" v-model="trendDateRange" type="daterange" placeholder="选择日期范围"></el-date-picker>&nbsp;&nbsp;
        <el-button @click="trendCheck">查看</el-button>
      </div>
      <div class="chart-trend" style="margin-top:20px">
        <echarts :options="trendOption" height="300px"></echarts>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import util from '@src/common/util.js'
import Echarts from '@src/components/Echarts.vue'
export default {
  components: {
    echarts: Echarts
  },
  data() {
    return {
      dataLoading: false,
      browser: [], // 浏览器
      browserDateArray: [], // 浏览器图表配置数据
      browserTrend: [], // 浏览器趋势数据
      trendDialog: false, // 趋势查看框
      trendDateRange: [], // 趋势查看框日期范围
      trendChartLegend: [], // 趋势图表配饰数据
      trendChartSeries: [],
      temTrendItem: '',
      temTrendLangItem: '',
      chartOptions: {},
      trendOption: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var browserOption = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c}'
        },
        legend: {
          orient: 'vertical',
          top: 'center',
          right: '50px',
          data: this.browserDataArray.map(function (obj) { return obj.name })
        },
        series: [
          {
            name: '浏览器',
            type: 'pie',
            center: ['45%', '50%'],
            radius: [0, '55%'],
            data: this.browserDataArray.sort(function (a, b) { return a.value - b.value }),
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      }
      this.chartOptions = browserOption
    },
    async showTrendChart() {
      // 趋势图表
      var trendOption = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: this.trendChartLegend
        },
        xAxis: {
          boundaryGap: false,
          data: this.browserTrend.map(function (obj) { return obj.date })
        },
        yAxis: {

        },
        series: [
          {
            name: this.temTrendItem,
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.trendChartSeries
          }
        ]
      }
      this.trendOption = trendOption
    },
    refresh() {
      this.getBrowserEchars()
    },
    async showTrend(row) {
      this.trendDateRange = []
      var currentDate = new Date()
      // 时间区段为30天
      currentDate.setDate(currentDate.getDate() - 30)
      let startDate = util.formatDate(currentDate, 'yyyy-MM-dd')
      let endDate = util.formatDate(new Date(), 'yyyy-MM-dd')
      this.trendDateRange.push(currentDate)
      this.trendDateRange.push(new Date())
      this.trendDialog = true
      this.temTrendItem = row.item
      let temItem = row.item.toLowerCase()
      this.temTrendLangItem = row.langItem
      this.trendChartLegend.pop()
      this.trendChartLegend.push(row.item)
      let res = await axios.get('/ui/stats/trendreport', {
        params: {
          startDate: startDate,
          endDate: endDate,
          type: 'Client',
          subType: 'browser',
          item: row.langItem
        }
      })
      this.browserTrend = res.data.data
      this.trendChartSeries = this.browserTrend.map(function (obj) { return obj[temItem] })
      this.showTrendChart()
    },
    async trendCheck() {
      if (this.trendDateRange && this.trendDateRange.length === 2 && this.trendDateRange[0] && this.trendDateRange[1]) {
        let startDate = util.formatDate(this.trendDateRange[0], 'yyyy-MM-dd')
        let endDate = util.formatDate(this.trendDateRange[1], 'yyyy-MM-dd')
        let temItem = this.temTrendItem.toLowerCase()
        let res = await axios.get('/ui/stats/trendreport', {
          params: {
            startDate: startDate,
            endDate: endDate,
            type: 'Client',
            subType: 'browser',
            item: this.temTrendLangItem
          }
        })
        this.browserTrend = res.data.data
        this.trendChartSeries = this.browserTrend.map(function (obj) { return obj[temItem] })
        this.showTrendChart()
      } else {
        this.$alert('请选择日期范围', '提示', 'warning')
      }
    },
    async getBrowserTableData() {
      this.dataLoading = true
      let res
      try {
        res = await axios.get('/ui/stats/clientreport', {
          params: {
            subType: 'Browser',
            showType: 'DataTable'
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      this.browser = res.data.data
    },
    async getBrowserEchars() {
      let res = await axios.get('/ui/stats/clientreport', {
        params: {
          subType: 'Browser',
          showType: 'ECharts'
        }
      })
      this.browser = res.data.data
      // 初始化浏览器饼图数据
      this.browserDataArray = this.browser.map(function (obj) {
        let rObj = {}
        let key1 = 'value'
        rObj[key1] = obj.browser
        let key2 = 'name'
        rObj[key2] = obj.item
        return rObj
      })
      this.showChart()
      this.getBrowserTableData()
    }
  },
  created() {
    this.getBrowserTableData()
  },
  mounted() {
    this.getBrowserEchars()
  }
}
</script>
<style scoped>
  .padding-top-bottom .search-bar {
    padding-bottom: 5px;
  }
</style>
