<style lang="scss" scoped>
.search-bar {
  display: flex;
  align-items: center;
}
.search-box p {
  display: inline-block;
  border: 1px solid #f2f2f2;
  border-radius: 5px;
  margin-right: 10px;
  padding: 5px 10px;
  cursor: pointer;
}
</style>
<style>
/* .data-display-statistics .ant-card-body {
  padding: 0 15px;
} */
.data-display-statistics .ant-col-6 {
  margin: 10px 0;
}
.data-display-statistics .ant-col-6 .ant-card-body {
  display: flex;
  justify-content: center;
}
.data-display-statistics .ant-col-6 p,
.data-display-statistics .ant-col-6 span {
  text-align: center;
  line-height: 40px;
  font-size: 30px;
  font-weight: 600;
  color: red;
  margin: 0;
}
.search-box .ant-card-body {
  padding: 5px 15px;
}
.data-display-statistics .ant-col-6 .ant-card-body .styleElse {
  line-height: 60px;
  height: 40px;
  font-size: 16px;
  font-weight: 400;
  margin-left: 10px;
}
.ant-modal-wrap .yincang .ant-modal-content .ant-modal-footer {
  display: none !important;
}
.yincang .ant-col-6 p {
  text-align: left;
  color: red;
  font-size: 16px;
  font-weight: 500;
}
.ant-table-thead div {
  font-weight: 700;
}
.tool-tip {
  position: absolute;
  top: 65px;
  right: 10px;
}
/* th div {
  white-space: nowrap;
} */
</style>
<template>
  <div class="data-statistics">
    <a-row class="image-list-heading vm-panel">
      <a-row type="flex" align="middle" justify="space-between" class="panel-body">
        <div class="search-bar" style="width:100%">
          <span>账户选择：</span>
          <treeselect :appId.sync="appId" style="width:400px" />
          <span style="margin-left:10px">返点比例：</span>
          <a-input-number
            :min="1"
            :max="10"
            :step="0.1"
            v-model="discount"
            placeholder="请输入折扣"
            style="width:100px"
          />
          <span style="margin-left:10px">日期选择：</span>
          <a-date-picker @change="getDate" :disabledDate="disabledDate" />
          <a-button type="primary" style="margin:10px" @click="addUser">搜索</a-button>
          <a-button
            type="primary"
            icon="reload"
            @click="getDataReset"
            style="background-color:#67c23a;border-color:#67c23a"
            >刷新</a-button
          >
        </div>
      </a-row>
    </a-row>
    <a-row :gutter="10" style="margin-top:10px">
      <a-col :span="24">
        <a-card
          style="width:100%"
          :tabList="tabListNoTitle"
          :activeTabKey="noTitleKey"
          @tabChange="key => onTabChange(key, 'noTitleKey')"
        >
          <div style="display:flex;margin-bottom:15px">
            <!-- <a-select style="width: 120px" v-model="statHour" @change="getElseData" allowClear>
              <a-select-option v-for="item of 24" :key="item - 1" :value="item - 1">{{ item - 1 }}</a-select-option>
            </a-select> -->
            <a-button type="primary" @click="exportExcel">导出</a-button>
          </div>
          <a-table
            size="middle"
            :columns="columns"
            :dataSource="data"
            bordered
            id="outTable"
            :pagination="ipagination"
            :scroll="{ x: 1900 }"
            :loading="loading"
          >
            <span slot="statDate" slot-scope="statDate">{{ statDate | formatDate }}</span>
            <span slot="disCountCost" slot-scope="disCountCost">{{ disCountCost | toFixtwo }}</span>

            <span slot="clickConversionRate" slot-scope="clickConversionRate">{{ clickConversionRate | getBo }}</span>

            <span slot="cost" slot-scope="cost">{{ cost | toFixtwo }}</span>
            <span slot="photoClickCost" slot-scope="photoClickCost">{{ photoClickCost | toFixtwo }}</span>
            <span slot="impression1kCost" slot-scope="impression1kCost">{{ impression1kCost | toFixtwo }}</span>
            <span slot="conversionPrice" slot-scope="conversionPrice">{{ conversionPrice | toFixtwo }}</span>
            <span slot="discountConversionPrice" slot-scope="discountConversionPrice">{{
              discountConversionPrice | toFixtwo
            }}</span>
            <span slot="contractRebateRealCharged" slot-scope="contractRebateRealCharged">{{
              contractRebateRealCharged | toFixtwo
            }}</span>
            <span slot="actualCost" slot-scope="actualCost">{{ actualCost | toFixtwo }}</span>

            <span slot="photoClickRatio" slot-scope="photoClickRatio">{{ photoClickRatio | getBo }}</span>
            <span slot="actionRatio" slot-scope="actionRatio">{{ actionRatio | getBo }}</span>
            <span slot="conversionRate" slot-scope="conversionRate">{{ conversionRate | getBo }}</span>
            <span slot="photoClickRatio" slot-scope="photoClickRatio">{{ photoClickRatio | getBo }}</span>
          </a-table>
        </a-card>
      </a-col>
    </a-row>
  </div>
</template>

<script>
import ARow from 'ant-design-vue/es/grid/Row'
import ACol from 'ant-design-vue/es/grid/Col'
import countTo from 'vue-count-to'
import moment from 'moment'
import { getDailyReport } from '@api/statistics'
import { getAction, postAction, downFile, downFilePost } from '@/api/manage'
import axios from 'axios'
import lifting from './components/lifting'
import timeCheck from './components/timeCheck'
import Treeselect from './components/Treeselect.vue'

import { JeecgListMixin } from '@/mixins/ipagination'

var length = 0
var active = 1
const columns = [
  {
    title: '消耗(￥)',
    dataIndex: 'cost',
    scopedSlots: { customRender: 'cost' },
    align: 'center'
  },
  {
    title: '展示数',
    dataIndex: 'photoShow',
    align: 'center'
  },
  {
    title: '点击数',
    dataIndex: 'photoClick',
    align: 'center'
  },
  {
    title: '点击率',
    dataIndex: 'photoClickRatio',
    align: 'center',
    scopedSlots: { customRender: 'photoClickRatio' }
  },
  {
    title: '平均点击花费',
    dataIndex: 'photoClickCost',
    align: 'center',
    scopedSlots: { customRender: 'photoClickCost' }
  },
  {
    title: '行为数',
    dataIndex: 'bclick',
    align: 'center'
  },
  {
    title: '行为率',
    dataIndex: 'actionRatio',
    align: 'center',
    scopedSlots: { customRender: 'actionRatio' }
  },
  {
    title: '平均千次展示花费（￥）',
    dataIndex: 'impression1kCost',
    align: 'center',
    scopedSlots: { customRender: 'impression1kCost' }
  },

  {
    title: '转化数',
    dataIndex: 'conversions',
    align: 'center'
  },
  {
    title: '转化单价(￥)',
    dataIndex: 'conversionPrice',
    align: 'center',
    scopedSlots: { customRender: 'conversionPrice' }
  },
  {
    title: '转化率',
    dataIndex: 'conversionRate',
    align: 'center',
    scopedSlots: { customRender: 'conversionRate' }
  },
  {
    title: '折后转化成本（￥）',
    dataIndex: 'discountConversionPrice',
    align: 'center',
    scopedSlots: { customRender: 'discountConversionPrice' }
  },
  {
    title: '激励花费（￥）',
    dataIndex: 'contractRebateRealCharged',
    align: 'center',
    scopedSlots: { customRender: 'contractRebateRealCharged' }
  },
  {
    title: '实际消耗（￥）',
    dataIndex: 'actualCost',
    align: 'center',
    scopedSlots: { customRender: 'actualCost' }
  }
]

const columnsGroup = [
  {
    title: '日期',
    dataIndex: 'statDate',
    scopedSlots: { customRender: 'statDate' },
    align: 'center'
  },
  {
    title: '账号',
    dataIndex: 'accountId',
    align: 'center'
  },
  {
    title: '账号名称',
    dataIndex: 'authName',
    align: 'center'
  },
  {
    title: '计划名称',
    dataIndex: 'campaignName',
    align: 'center'
  },
  {
    title: '广告组名称',
    dataIndex: 'unitName',
    align: 'center'
  },
  {
    title: '消耗',
    dataIndex: 'cost',
    align: 'center',
    scopedSlots: { customRender: 'cost' }
  },
  {
    title: '折后消耗',
    dataIndex: 'disCountCost',
    align: 'center',
    scopedSlots: { customRender: 'cost' }
  },
  {
    title: '展示数',
    dataIndex: 'photoShow',
    align: 'center'
  },
  {
    title: '点击数',
    dataIndex: 'photoClick',
    align: 'center'
  },
  {
    title: '点击率',
    dataIndex: 'photoClickRatio',
    align: 'center',
    scopedSlots: { customRender: 'photoClickRatio' }
  },

  {
    title: '行为数',
    dataIndex: 'bclick',
    align: 'center'
  },
  {
    title: '行为率',
    dataIndex: 'actionRatio',
    align: 'center',
    scopedSlots: { customRender: 'actionRatio' }
  },
  {
    title: '平均千次展示花费（￥）',
    dataIndex: 'impression1kCost',
    align: 'center',
    scopedSlots: { customRender: 'impression1kCost' }
  },
  {
    title: '平均点击花费',
    dataIndex: 'photoClickCost',
    align: 'center',
    scopedSlots: { customRender: 'photoClickCost' }
  },
  {
    title: '转化数',
    dataIndex: 'conversions',
    align: 'center'
  },
  {
    title: '转化单价(￥)',
    dataIndex: 'conversionPrice',
    align: 'center',
    scopedSlots: { customRender: 'conversionPrice' }
  },
  {
    title: '转化率',
    dataIndex: 'conversionRate',
    align: 'center',
    scopedSlots: { customRender: 'conversionRate' }
  },
  {
    title: '折后转化成本（￥）',
    dataIndex: 'discountConversionPrice',
    align: 'center',
    scopedSlots: { customRender: 'discountConversionPrice' }
  },
  {
    title: '点击转化率',
    dataIndex: 'clickConversionRate',
    align: 'center',
    scopedSlots: { customRender: 'clickConversionRate' }
  }
]

export default {
  name: 'data-display-statistics',
  components: {
    ACol,
    ARow,
    countTo,
    lifting,
    timeCheck,
    Treeselect
  },
  mixins: [JeecgListMixin],
  data: function() {
    return {
      title: '账户数据展示',
      show: true,
      data: [],
      dataAll: null,
      columns: columns,
      options: [],
      list: [],
      appId: [],
      tabListNoTitle: [
        {
          key: 'day',
          tab: '分日数据'
        },
        {
          key: 'group',
          tab: '分组数据'
        }
      ],
      noTitleKey: 'day',
      discount: 1,
      date: '',
      loading: false
    }
  },
  filters: {
    formatDate: function(value) {
      let date = new Date(value)
      let y = date.getFullYear()
      let MM = date.getMonth() + 1
      MM = MM < 10 ? '0' + MM : MM
      let d = date.getDate()
      d = d < 10 ? '0' + d : d
      let h = date.getHours()
      h = h < 10 ? '0' + h : h
      let m = date.getMinutes()
      m = m < 10 ? '0' + m : m
      let s = date.getSeconds()
      s = s < 10 ? '0' + s : s
      return y + '-' + MM + '-' + d
    },
    toFixtwo: function(value) {
      if (value) {
        return value.toFixed(2)
      } else {
        return 0.0
      }
    },
    getBo: function(value) {
      if (value) {
        return (value * 100).toFixed(2) + '%'
      } else {
        return '0.00%'
      }
    }
  },
  methods: {
    moment,
    onTabChange(key, type) {
      console.log(key, type)
      this[type] = key
      this.data = []
      if (key == 'day') {
        this.columns = columns
        this.data.push({ ...this.dataAll.dailyAccountReport, key: 1 })
      } else {
        this.columns = columnsGroup
        this.data = this.dataAll.dailyGroupReport.map((item, index) => {
          return {
            ...item,
            key: index
          }
        })
      }
      this.ipagination.total = this.data.length
      this.ipagination.current = 1
    },
    disabledDate(current) {
      return current && current > moment().subtract(1, 'day')
    },
    getDataReset() {
      this.addUser()
    },
    addUser() {
      var that = this
      if (this.appId.length > 0 && this.date != '') {
        that.getDailyReport()
      } else {
        this.$message.error('请选择账户及时间')
      }
    },
    getDailyReport() {
      var params = {}
      params.accountIds = JSON.stringify(this.appId)
      params.date = this.date
      params.discount = this.discount
      this.data = []
      this.loading = true
      getDailyReport(params).then(res => {
        if (res.success) {
          this.loading = false
          this.dataAll = res.result
          if (this.noTitleKey == 'day') {
            this.data.push({ ...res.result.dailyAccountReport, key: 1 })
          } else {
            this.data = res.result.dailyGroupReport.map((item, index) => {
              return {
                ...item,
                key: index
              }
            })
          }
          this.ipagination.total = this.data.length
        }
      })
    },
    getDate(value) {
      if (value) {
        let date = new Date(value)
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? '0' + MM : MM
        let d = date.getDate()
        d = d < 10 ? '0' + d : d
        this.date = y + '-' + MM + '-' + d
      } else {
        let date = new Date()
        let y = date.getFullYear()
        let MM = date.getMonth() + 1
        MM = MM < 10 ? '0' + MM : MM
        let d = date.getDate()
        d = d < 10 ? '0' + d : d
        this.date = y + '-' + MM + '-' + d
      }
    },
    exportExcel() {
      var params = {}
      params.accountIds = JSON.stringify(this.appId)
      params.date = this.date
      params.discount = this.discount
      downFilePost('/ctop/daily/exportDailyReport', params).then(res => {
        let blob = new Blob([res], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
        })
        let downloadElement = document.createElement('a')
        let href = window.URL.createObjectURL(blob) //创建下载的链接
        downloadElement.href = href
        downloadElement.download = this.date + '-日报' //下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() //点击下载
        document.body.removeChild(downloadElement) //下载完成移除元素
        window.URL.revokeObjectURL(href) //释放掉blob对象
      })
    }
  },
  watch: {},
  distoryed() {},
  mounted: function() {
    this.$nextTick(() => {})
  }
}
</script>
