<template>
  <div class="right-list-toobar">
    <el-toolbar>
      <el-button @click="refresh"><i class="fa fa-refresh"></i>刷新</el-button>
      <el-col>当前报表：是否支持Applet</el-col>
    </el-toolbar>
    <div v-loading="dataLoading">
      <echarts :options="chartOptions" height="300px"></echarts>
      <div style="margin:10px">

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

</script>

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

</style>
