<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      :request-api="getTableList"
      :init-param="initParam"
      :data-callback="dataCallback"
      row-key="advertiserId"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" plain @click="batchDelete(scope.selectedList)"> 批量同步广告主消耗数据 </el-button>
      </template>
    </ProTable>
    <SynchronizationAdvertiserAccountCostDt ref="synchronizationAdvertiserAccountCostDt" />
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { User } from "@/api/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { ElMessage, ElMessageBox } from "element-plus";
import { Delete } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps, HeaderRenderScope } from "@/components/ProTable/interface";
import { getUserGender } from "@/api/modules/user";
import { genderType, advertiserStatus } from "@/utils/dict";

import { getAdvertiserAccountCostDtList } from "@/api/modules/dataManage";
import SynchronizationAdvertiserAccountCostDt from "./components/SynchronizationAdvertiserAccountCostDt.vue";
const synchronizationAdvertiserAccountCostDt = ref<any>();
// ProTable 实例
const proTable = ref<ProTableInstance>();

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({});
const advertiserId = ref<string[]>([]);

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 content && total 这些字段，可以在这里进行处理成这些字段
// 或者直接去 hooks/useTable.ts 文件中把字段改为你后端对应的就行
const dataCallback = (data: any) => {
  return {
    content: data?.content || [],
    total: Number(data?.total) || 0
  };
};

// 如果你想在请求之前对当前请求参数做一些操作，可以自定义如下函数：params 为当前所有的请求参数（包括分页），最后返回请求列表接口
// 默认不做操作就直接在 ProTable 组件上绑定	:requestApi="getUserList"
const getTableList = (params: any) => {
  let newParams = JSON.parse(JSON.stringify(params));
  newParams.createTime && (newParams.startTime = newParams.createTime[0]);
  newParams.createTime && (newParams.endTime = newParams.createTime[1]);
  // delete newParams.createTime;
  // 处理获取时间日期范围
  newParams.dt && (newParams.dtStart = newParams.dt[0]);
  newParams.dt && (newParams.dtEnd = newParams.dt[1]);
  delete newParams.dt;
  return getAdvertiserAccountCostDtList(newParams);
};

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: "selection", fixed: "left", width: 70 },
  {
    prop: "dt",
    label: "获取时间",
    width: 160,
    search: {
      el: "date-picker",
      span: 2,
      props: { type: "daterange", valueFormat: "YYYY-MM-DD" }
    }
  },
  { prop: "advertiserId", label: "广告主ID", search: { el: "input" }, width: 200,
  render: scope => {
    return (
      <div>
        {scope.row?.advertiserId || "--"}
        <div style="font-size: 12px; color: #999;">{scope.row?.advertiserName || "--"}</div>
        <div style="font-size: 12px; color: #999;">{scope.row?.advertiserStatus ? advertiserStatus.find(item => item.value === scope.row?.advertiserStatus)?.label || "--" : "--"}</div>
      </div>
    );
  }
  },
  // { prop: "advertiserName", label: "广告主名称" },
  // { prop: "advertiserStatus", label: "广告主状态", enum: advertiserStatus },
  {
    prop: "advCompanyId",
    label: "广告主公司ID",
    search: { el: "input" },
    isShow: false
  },
  {
    prop: "advCompanyName",
    label: "广告主公司名称",
    search: { el: "input" },
    isShow: false
  },
  { prop: "cost", label: "总消耗" },
  { prop: "noGrantCost", label: "非赠款消耗" },
  { prop: "materialCost", label: "素材总消耗" },
  { prop: "firstPublishMaterialCost", label: "首发素材消耗" },
  { prop: "highQualityMaterialCost", label: "优质素材消耗" },
  { prop: "poorQualityMaterialCost", label: "低质素材消耗" },
  { prop: "lowEfficiencyMaterialCost", label: "低效素材消耗" },
  { prop: "improvableMaterialCost", label: "可提升素材消耗" },
]);
// 批量同步素材消耗
const batchDelete = async (selectedList: any[]) => {
  // if (!selectedList || selectedList.length === 0) {
  //   ElMessage.warning("请先选择要同步的数据");
  //   return;
  // }
  // 可以获取选中的广告主ID列表
  if(selectedList.length > 0) {
    advertiserId.value = [...new Set(selectedList.map(item => item.advertiserId))];
    console.log("广告主ID列表：", advertiserId.value);
  }
  const params = {
    title: "同步广告主消耗数据",
    isView: false,
    row: { advertiserId: advertiserId.value || [] },
    api: undefined,
    getTableList: proTable.value?.getTableList
  };
  synchronizationAdvertiserAccountCostDt.value?.acceptParams(params);
};

onMounted(async () => {});
</script>
