<!-- 广告报表列表页 -->
<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
          multiple
          collapse-tags
          v-model="pages.mediaIds"
          placeholder="站点"
          style="width: 200px"
        >
          <el-option
            v-for="item in SiteList"
            :key="item.mediaId"
            :label="item.mediaName"
            :value="item.mediaId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="广告位">
        <el-select
          filterable
          clearable
          multiple
          collapse-tags
          v-model="pages.slotIds"
          placeholder="广告位"
          style="width: 200px"
        >
          <el-option
            v-for="item in SlotIds"
            :key="item.slotId"
            :label="item.slotName"
            :value="item.slotId"
          />
        </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 { Pages } from '@/types/Tool'
import type { IReportItem } from '@/types/Report'
import type { SiteList } from '@/types/Site'
import type { AsSpaceItem } from '@/types/AsSpace'
import { getMediaReport, getMediaMonthReport, getSiteList, getAsSpaceList } from '@/api/index'
import { formatDate } from '@/tool/index'
import { reactive, ref } from 'vue'
const tableData = ref<IReportItem[]>([])
const date_ = formatDate(new Date().getTime())
const SiteList = ref<SiteList>([])
const pages = reactive<Pages>({
  pageNum: 1,
  pageSize: 10,
  mediaIds: [],
  slotIds: [],
  datas: [
    date_.fmtYear + '-' + date_.fmtMonth + '-' + date_.fmtDay,
    date_.fmtYear + '-' + date_.fmtMonth + '-' + date_.fmtDay,
  ],
  total: 0,
  type: 'month',
})
const loading = ref(false)
const SlotIds = ref<AsSpaceItem[]>([])

const getAsSpaceListFunc = () => {
  return new Promise((resolve) => {
    let i = 0
    const len = pages.mediaIds?.length || 0
    pages.mediaIds?.map((v) => {
      getAsSpaceList({
        pageNum: 1,
        pageSize: 999999,
        mediaId: v,
      }).then(({ data }) => {
        SlotIds.value.push(...data.records)
        if (i++ >= len) resolve(true)
      })
    })
  })
}

const onSubmit = (isExport = false) => {
  const p = {
    startDate: pages.datas ? pages.datas[0] : '',
    endDate: pages.datas ? pages.datas[1] : '',
    pageNum: pages.pageNum,
    pageSize: isExport ? 999999 : pages.pageSize,
  }
  const Func = pages.type == 'day' ? getMediaReport : getMediaMonthReport
  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()
}

getSiteList().then((res) => {
  SiteList.value = res.data || []
  pages.mediaIds = typeof SiteList.value[0].mediaId === 'number' ? [SiteList.value[0].mediaId] : []
  getAsSpaceListFunc().then(() => {
    onSubmit()
  })
})
</script>

<style></style>
