<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-col align="right">
        <el-date-picker id="advertise-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 @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:10px">

      </div>
      <el-table :data="advertise" tooltip-effect="dark" style="width: 100%">
        <el-table-column prop="itemName" label="广告名称"></el-table-column>
        <el-table-column prop="show" label="呈现"></el-table-column>
        <el-table-column prop="click" label="点击"></el-table-column>
        <el-table-column prop="clickRate" label="点击率"></el-table-column>
        <el-table-column label="趋势">
          <template slot-scope="scope">
            <a href="javascript:void(0);" @click="showTrend(scope.row)">时间趋势</a>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 趋势查看框 -->
    <el-dialog title="时间趋势" :visible.sync="trendDialog">
      <div>
        日期范围:&nbsp;
        <el-date-picker id="advertise-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 @click="trendCheck">查看</el-button>
      </div>
      <div style="margin-top:20px;">
        <echarts :options="trendOption" height="300px"></echarts>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Echarts from '@src/components/Echarts.vue'
const MAX_CHART_ITEM_COUNT = 10 // 饼图最多显示数
export default {
  components: {
    echarts: Echarts
  },
  data() {
    return {
      dataLoading: false,
      advertise: [], // 广告点击率
      advertiseDataArray: [], // 广告点击率图表配置数据
      trend: [], // 趋势
      dateRangeValue: [],
      startDate: '', // 搜索框日期范围
      endDate: '',
      trendDialog: false, // 趋势查看框
      tdateRangeValue: [],
      tStartDate: '', // 趋势查看框日期范围
      tEndDate: '',
      item: '', // 当前被访主机名称
      itemName: '',
      xData: [], // 趋势图表数据
      yData: [],
      chartOptions: {},
      trendOption: {}

    }
  },
  methods: {
    async showChart() {
      // 广告点击率图表
      // 指定图表的配置项和数据
      var advertiseOption = {
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c}'
        },
        legend: {
          orient: 'vertical',
          top: 'center',
          right: '50px',
          data: this.advertiseDataArray.map(function (obj) { return obj.name })
        },
        series: [
          {
            name: '广告呈现',
            type: 'pie',
            center: ['45%', '50%'],
            radius: [0, '55%'],
            data: this.advertiseDataArray.sort(function (a, b) { return a.value - b.value }),
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200
            }
          }
        ]
      }
      this.chartOptions = advertiseOption
    },
    // 趋势图表
    async showTrendChart() {
      // 趋势图表
      var trendOption = {
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: [this.itemName]
        },
        xAxis: {
          boundaryGap: false,
          data: this.xData
        },
        yAxis: {

        },
        series: [
          {
            name: this.itemName,
            type: 'line',
            symbol: 'none',
            smooth: true,
            data: this.yData
          }
        ]
      }
      this.trendOption = trendOption
    },
    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.loadAdvertise()
    },
    check() {
      this.loadAdvertise()
    },
    showTrend(row) {
      this.item = row.item
      this.itemName = row.itemName
      this.tdateRangeValue = this.dateRangeValue
      this.tStartDate = this.startDate
      this.tEndDate = this.endDate
      this.trendDialog = true
      this.loadTrend()
    },
    trendCheck() {
      this.loadTrend()
    },
    async loadTrend() {
      let resp = await axios.get('/ui/stats/trend', {
        params: {
          startdate: this.tStartDate,
          enddate: this.tEndDate,
          item: this.item,
          type: 'ADAdv',
          subType: 'Click'
        }
      })
      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.showTrendChart()
    },
    async loadAdvertise() {
      this.dataLoading = true
      let resp
      try {
        resp = await axios.get('/ui/stats/adsadvertisereport', {
          params: {
            startdate: this.startDate,
            enddate: this.endDate
          }
        })
        this.dataLoading = false
      } catch (e) {
        this.dataLoading = false
        return
      }
      if (resp.data.status !== 1) {
        return resp.data
      }
      this.advertise = resp.data.data
      let chartData = this.advertise.map(function (obj) {
        return {
          name: obj.itemName,
          value: obj.show
        }
      })
      if (chartData.length > MAX_CHART_ITEM_COUNT) {
        let chartDataTemp = chartData.splice(MAX_CHART_ITEM_COUNT)
        let otherValue = _.sum(_.map(chartDataTemp, 'value'))
        chartData.push({
          name: '其他',
          value: otherValue
        })
      }
      this.advertiseDataArray = chartData

      this.showChart()
    },
    async initDate() {
      let res = await axios.get('/ui/stats/summary/initdate')
      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.loadAdvertise()
  }
}

</script>

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

</style>
