<!-- 广告报表列表页 -->
<template>
  <div v-loading="loading">
    <el-form :inline="true">
      <el-form-item label="周期">
        <el-date-picker
          v-model="pages.datas"
          type="daterange"
          format="YYYY-MM-DD"
          value-format="YYYY-MM-DD"
          range-separator="到"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        />
      </el-form-item>
      <el-form-item label="区域">
        <el-select filterable clearable v-model="pages.province" style="width: 200px">
          <el-option
            v-for="item in provinces"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告主">
        <el-select
          multiple
          collapse-tags
          filterable
          clearable
          v-model="pages.advertiserIds"
          style="width: 200px"
        >
          <el-option
            v-for="item in advertisers"
            :key="item.advertiserId"
            :label="item.enterpriseName"
            :value="item.advertiserId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创意">
        <el-select
          multiple
          collapse-tags
          filterable
          clearable
          v-model="pages.creativeIds"
          style="width: 200px"
        >
          <el-option
            v-for="item in originalitys"
            :key="item.creativeId"
            :label="item.creativeName"
            :value="item.creativeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="素材">
        <el-select
          multiple
          collapse-tags
          filterable
          clearable
          v-model="pages.stuffIds"
          style="width: 200px"
        >
          <el-option
            v-for="item in SourceMaterials"
            :key="item.stuffId"
            :label="item.stuffName"
            :value="item.stuffId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="() => onSubmit()">搜索</el-button>
        <el-button type="primary" @click="() => onSubmit(true)">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="sdate" label="日期" />
      <el-table-column prop="exposureCount" label="曝光数" />
      <el-table-column prop="clickCount" label="点击数" />
      <el-table-column prop="uvCount" label="用户数" />
    </el-table>
    <br />
    <el-pagination
      background
      layout="prev, pager, next"
      :total="pages.total"
      @current-change="page"
    />
  </div>
</template>

<script setup lang="ts">
import type { OriginalityItem } from '@/types/AdListPage'
import type { Advertiser } from '@/types/Advertiser'
import type { Pages } from '@/types/Tool'
import type { SourceMaterialList } from '@/types/SourceMaterial'
import type { IReportItem } from '@/types/Report'
import {
  getAdvertiserList,
  getOriginalityList,
  getMaterialList,
  getAdvertiserReport,
  getAdvertiserMonthReport,
  getProvinceCity,
} from '@/api/index'
import { formatDate } from '@/tool/index'
import { reactive, ref } from 'vue'
const date_ = formatDate(new Date().getTime() - 86400000)
const pages = reactive<Pages>({
  pageNum: 1,
  pageSize: 10,
  advertiserIds: [],
  creativeIds: [],
  stuffIds: [],
  province: '',
  datas: [
    date_.fmtYear + '-' + date_.fmtMonth + '-' + date_.fmtDay,
    date_.fmtYear + '-' + date_.fmtMonth + '-' + date_.fmtDay,
  ],
  total: 0,
  type: 'day',
})
const advertisers = ref<Advertiser[]>([])
const originalitys = ref<OriginalityItem[]>([])
const SourceMaterials = ref<SourceMaterialList>([])
const provinces = ref<{ label: string; value: string }[]>([])
const tableData = ref<IReportItem[]>([])
const loading = ref(false)

const getList = () => {
  getAdvertiserList({
    pageNum: 1,
    pageSize: 999999,
  }).then(({ data }) => {
    // 广告主列表
    advertisers.value = data.records || []
  })
  getOriginalityList({
    pageNum: 1,
    pageSize: 999999,
    keyword: '',
  }).then(({ data }) => {
    // 创意列表
    originalitys.value = data.records || []
  })
  getMaterialList({
    pageNum: 1,
    pageSize: 999999,
  }).then(({ data }) => {
    // 素材列表
    SourceMaterials.value = data.records || []
  })
  getProvinceCity().then(({ data }) => {
    provinces.value = data.area.map((item: { name: string }) => {
      return {
        label: item.name,
        value: item.name,
      }
    })
  })
}

const onSubmit = (isExport = false) => {
  const p = {
    advertiserIds: pages.advertiserIds,
    creativeIds: pages.creativeIds,
    stuffIds: pages.stuffIds,
    startDate: pages.datas ? pages.datas[0] : '',
    endDate: pages.datas ? pages.datas[1] : '',
    provinces: pages.province ? [pages.province] : [],
    pageNum: pages.pageNum,
    pageSize: isExport ? 999999 : pages.pageSize,
  }
  const Func = pages.type == 'day' ? getAdvertiserReport : getAdvertiserMonthReport
  loading.value = true
  Func(p).then(({ data }) => {
    if (!isExport) {
      loading.value = false
      tableData.value = data.records
      pages.total = data.total
      return
    }
    if (!data.records || data.records.length == 0) {
      loading.value = false
      return
    }
    // 生成数据
    let strcsv = 'data:text/csv;charset=utf-8,\uFEFF日期,曝光数,点击数,用户数\r\n'
    ;(data.records || []).map((v: IReportItem) => {
      strcsv += `${v.sdate},${v.exposureCount},${v.clickCount},${v.uvCount},\r\n`
    })
    // 导出
    let link: HTMLAnchorElement | undefined = document.createElement('a')
    link.id = 'download-csv'
    link.setAttribute('href', encodeURI(strcsv))
    link.setAttribute('download', '广告' + '.csv')
    link.click()
    link = undefined
    loading.value = false
  })
}

const page = (val: number) => {
  pages.pageNum = val
  onSubmit()
}

getList()
onSubmit()
</script>

<style></style>
