<template>
  <div class="ma-content-block">
    <sa-table
      ref="crudRef"
      :options="options"
      :columns="columns"
      :searchForm="searchForm"
    >
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="8" :xs="24">
          <a-form-item label="结算日期" field="tdate">
            <a-range-picker
              v-model="searchForm.tdate"
              :show-time="false"
              mode="date"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="投放游戏" field="game_id">
            <a-select
              v-model="searchForm.game_id"
              placeholder="请选择投放游戏"
              allow-clear
              allow-search
            >
              <a-option
                v-for="item in gameOptions"
                :key="item.id"
                :value="item.id"
                :label="`${item.id}:${item.name}`"
              />
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="媒体类型" field="media_id">
            <a-select
              v-model="searchForm.media_id"
              placeholder="请选择媒体类型"
              allow-clear
              allow-search
            >
              <a-option
                v-for="item in mediaOptions"
                :key="item.id"
                :value="item.id"
                :label="`${item.id}:${item.name}`"
              />
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="渠道ID" field="agent_id">
            <a-input
              v-model="searchForm.agent_id"
              placeholder="请输入渠道ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="广告位ID" field="site_id">
            <a-input
              v-model="searchForm.site_id"
              placeholder="请输入广告位ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="负责人" field="auth_id">
            <sa-select
              v-model="searchForm.auth_id"
              :options="authOptions"
              placeholder="请选择负责人"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="录入方式" field="add_type">
            <sa-select
              v-model="searchForm.add_type"
              dict="add_type"
              placeholder="请选择录入方式"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="备注" field="memo">
            <a-input
              v-model="searchForm.memo"
              placeholder="请输入备注"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="录入时间" field="create_time">
            <a-range-picker
              v-model="searchForm.create_time"
              :show-time="true"
              mode="date"
            />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from "vue";
import { Message } from "@arco-design/web-vue";
import EditForm from "./edit.vue";
import api from "../../api/advert/mediaCost";
import commonAdvertApi from "../../api/advert/common";
import commonCenterApi from "../../api/center/common";

// 引用定义
const crudRef = ref();
const editRef = ref();
const viewRef = ref();
const mediaOptions = ref([]);
const gameOptions = ref([]);
const authOptions = ref([]);

// 搜索表单
const searchForm = ref({
  tdate: [],
  game_id: "",
  media_id: "",
  agent_id: "",
  site_id: "",
  auth_id: "",
  add_type: "",
  memo: "",
  create_time: [],
});

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ["/v1/advert/MediaCost/save"],
    func: async () => {
      editRef.value?.open();
    },
  },
  edit: {
    show: true,
    auth: ["/v1/advert/MediaCost/update"],
    func: async (record) => {
      editRef.value?.open("edit");
      editRef.value?.setFormData(record);
    },
  },
  delete: {
    show: true,
    auth: ["/v1/advert/MediaCost/destroy"],
    func: async (params) => {
      const resp = await api.destroy(params);
      if (resp.code === 200) {
        Message.success(`删除成功！`);
        crudRef.value?.refresh();
      }
    },
  },
});

// SaTable 列配置
const columns = reactive([
  { title: "结算日期", dataIndex: "tdate", width: 120 },
  { title: "广告位ID", dataIndex: "site_id", width: 120 },
  { title: "广告位名称", dataIndex: "site_name", width: 120 },
  { title: "渠道ID", dataIndex: "agent_id", width: 120 },
  { title: "游戏", dataIndex: "game_name", width: 120 },
  { title: "原始金额", dataIndex: "ori_money", width: 120 },
  { title: "结算金额", dataIndex: "money", width: 120 },
  { title: "录入时间", dataIndex: "create_time", width: 120 },
  {
    title: "录入方式",
    dataIndex: "add_type",
    type: "dict",
    dict: "add_type",
    width: 120,
  },
  { title: "备注", dataIndex: "memo", width: 120 },
  { title: "负责人", dataIndex: "auth_name", width: 120 },
]);

// 页面数据初始化
const initPage = async () => {
  await getMediaOptions();
  await getGameOptions();
  await getAuthOptions();
};

// 获取媒体类型
const getMediaOptions = async () => {
  const resp = await commonAdvertApi.getAgentOptionsApi();
  if (resp.code === 200) {
    mediaOptions.value = resp.data;
  }
};

// 获取负责人
const getAuthOptions = async () => {
  const resp = await commonAdvertApi.getAuthOptionsApi();
  if (resp.code === 200) {
    authOptions.value = resp.data;
  }
};

// 获取投放游戏
const getGameOptions = async () => {
  const resp = await commonCenterApi.getGameOptionsApi();
  if (resp.code === 200) {
    gameOptions.value = resp.data;
  }
};

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh();
};

// 页面加载完成执行
onMounted(async () => {
  initPage();
  refresh();
});
</script>
