<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="OS" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="item" label="名称"></el-table-column>
        <el-table-column prop="OS" 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="OS-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,
      OS: [], // 操作系统
      OSEchars: [],
      OSDateArray: [], // 操作系统图表配置数据
      OSTrend: [], // 操作系统趋势数据
      trendDialog: false, // 趋势查看框
      trendDateRange: [], // 趋势查看框日期范围
      trendChartLegend: [], // 趋势图表配饰数据
      trendChartSeries: [],
      temTrendItem: '',
      temTrendLangItem: '',
      chartOptions: {},
      trendOption: {}
    }
  },
  methods: {
    async showChart() {
      // 指定图表的配置项和数据
      var OSOption = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c}'
        },
        legend: {
          orient: 'vertical',
          top: 'center',
          right: '50px',
          data: this.OSDataArray.map(function (obj) { return obj.name })
        },
        series: [
          {
            name: '操作系统',
            type: 'pie',
            center: ['45%', '50%'],
            radius: [0, '55%'],
            data: this.OSDataArray.sort(function (a, b) { return a.value - b.value }),
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      }
      this.chartOptions = OSOption
    },
    async showTrendChart() {
      // 趋势图表
      var trendOption = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: this.trendChartLegend
        },
        xAxis: {
          boundaryGap: false,
          data: this.OSTrend.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.getOSEchars()
    },
    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 params = {
        startDate: startDate,
        endDate: endDate,
        type: 'Client',
        subType: 'OS',
        item: row.langItem
      }
      let res = await axios.get('/ui/stats/trendreport', {params: params})
      let data = res.data
      this.OSTrend = data.data
      this.trendChartSeries = this.OSTrend.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 params = {
          startDate: startDate,
          endDate: endDate,
          type: 'Client',
          subType: 'OS',
          item: this.temTrendLangItem
        }
        let res = await axios.get('/ui/stats/trendreport', {params: params}).catch(e => console.log(e))
        let data = res.data
        this.OSTrend = data.data
        this.trendChartSeries = this.OSTrend.map(function (obj) { return obj[temItem] })
        this.showTrendChart()
      } else {
        this.$alert('请选择日期范围', '提示', 'warning')
      }
    },
    async getOSTableData() {
      this.dataLoading = true
      let res
      try {
        res = await axios.get('/ui/stats/clientreport', {
          params: {
            subType: 'OS',
            showType: 'DataTable'
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      let data = res.data
      this.OS = data.data
    },
    async getOSEchars() {
      let params = {
        subType: 'OS',
        showType: 'ECharts'
      }
      let res = await axios.get('/ui/stats/clientreport', {params: params}).catch(e => console.log(e))
      let data = res.data
      this.OSEchars = data.data
      // 初始化操作系统饼图数据
      this.OSDataArray = this.OSEchars.map(function (obj) {
        var rObj = {}
        var key1 = 'value'
        rObj[key1] = obj.OS
        var key2 = 'name'
        rObj[key2] = obj.item
        return rObj
      })
      this.showChart()
      this.getOSTableData()
    }
  },
  created() {
    this.getOSTableData()
  },
  mounted() {
    this.getOSEchars()
  }
}

</script>

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

</style>
