<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>
          <a-select
            v-model="projectId"
            style="width:500px"
            showSearch
            optionFilterProp="children"
            :filterOption="filterOption"
          >
            <a-select-option :value="item.projectId" v-for="item of dataElse" :key="item.id">
              {{ item.projectName }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{
                item.mediaId == "1" ? '头条' :item.mediaId == "2"? '快手':item.mediaId == "3"?'头条内广':'快手内广'
              }}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ item.advertiserName }}
            </a-select-option>
          </a-select>
          <span style="margin-left:10px">系统类型：</span>
          <a-select
            v-model="systemType"
            style="width:300px"
            showSearch
            optionFilterProp="children"
            :filterOption="filterOption"
          >
            <a-select-option value="1">
              不限
            </a-select-option>
            <a-select-option value="android">
              安卓
            </a-select-option>
            <a-select-option value="ios">
              ios
            </a-select-option>
          </a-select>

          <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" v-if="tabListNoTitle.length > 0">
            <!-- <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
            v-if="tabListNoTitle.length > 0"
            size="middle"
            :columns="columns"
            :dataSource="data"
            bordered
            id="outTable"
            :pagination="ipagination"
            :scroll="{ x: true }"
            :loading="loading"
          >
          </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 { mapGetters } from 'vuex'
import { JeecgListMixin } from '@/mixins/ipagination'
import qs from 'qs'
var length = 0
var active = 1
const columns = [
  {
    title: '日期',
    dataIndex: 'date',
    scopedSlots: { customRender: 'date' },
    align: 'center'
  },
  {
    title: '总花费（元）',
    dataIndex: 'cost',
    align: 'center'
  },
  {
    title: '双出价（元）',
    dataIndex: 'doubleBid',
    align: 'center'
  },
  {
    title: '非双出价（元）',
    dataIndex: 'singleBid',
    align: 'center'
  },
  {
    title: '激活数',
    dataIndex: 'active',
    align: 'center'
  },
  {
    title: '激活成本',
    dataIndex: 'activeCost',
    align: 'center'
  },
  {
    title: '每日计划(组)',
    dataIndex: 'planNum',
    align: 'center'
  },
  {
    title: '每日素材安排',
    dataIndex: 'mat',
    align: 'center'
  }
]

export default {
  name: 'data-display-statistics',
  components: {
    ACol,
    ARow,
    countTo,
    lifting,
    timeCheck,
    Treeselect
  },
  mixins: [JeecgListMixin],
  data: function() {
    return {
      // this.timeRange[1] = moment().add(1, 'months')
      statDate: moment().subtract(1, 'day'),
      title: '账户数据展示',
      show: true,
      data: [],
      dataAll: null,
      columns: columns,
      options: [],
      list: [],
      appId: [],
      tabListNoTitle: [],
      noTitleKey: 'day',
      discount: 1.06,
      date: '',
      loading: false,
      dataElse: [],
      projectId: 285,
      systemType: '1'
    }
  },
  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,
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    ...mapGetters(['nickname', 'avatar', 'userInfo']),
    getParticipateList() {
      getAction('/ctop/projectMember/participateList', {
        userId: this.userInfo().id
      }).then(res => {
        if (res.code == 0) {
          this.dataElse = res.result
        }
      })
    },
    onTabChange(key, type) {
      console.log(key, type)
      this[type] = key
      this.data = []
      var list = this.tabListNoTitle.filter(item => {
        return item.key == key
      })
      console.log(list)
      this.data = list[0].data
      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.projectId = this.projectId
      params.systemType = this.systemType == '1' ? '' : this.systemType
      this.data = []
      this.loading = true
      postAction('/ctop/advertiser/xxlDailyReportList', params).then(res => {
        if (res.success) {
          console.log(res)
          this.loading = false
          //        {
          //       key: 'channel',
          //       tab: '渠道包分析'
          //     }
          //   ],
          //   noTitleKey: 'day',
          this.noTitleKey = res.data[0].sheetName
          this.tabListNoTitle = res.data.map(item => {
            return {
              key: item.sheetName,
              tab: item.sheetName,
              data: item.details
            }
          })

          this.data = this.tabListNoTitle[0].data.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.projectId = this.projectId
      params.systemType = this.systemType == '1' ? '' : this.systemType
      var param = qs.stringify(params)
      var name = this.dataElse.filter(item => {
        return item.projectId == this.projectId
      })[0].projectName
      downFilePost('/ctop/advertiser/exportXxlDailyReport', param).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 = name + '-' + this.statDate.format('YYYY-MM-DD') + '-日报' //下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() //点击下载
        document.body.removeChild(downloadElement) //下载完成移除元素
        window.URL.revokeObjectURL(href) //释放掉blob对象
      })
    }
  },
  watch: {},
  distoryed() {},
  mounted: function() {
    this.getParticipateList()
    this.statDate = moment().subtract(1, 'day')
    // moment().format('YYYY-MM-DD_HH:mm')
  }
}
</script>
