<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="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="游戏" field="game_id">
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #letter="{ record }"> {{ record.game_id }}_{{ record.letter }} </template>
    </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 advertCommonApi from '../../api/advert/common'
import api from '../../api/advert/gamePackage'
import GameSelect from '@/components/game-select/index.vue'

// 引用定义
const crudRef = ref()
const editRef = ref()
const mediaOptions = ref([])

// 搜索表单
const searchForm = ref({
  media_id: '',
  game_id: '',
})

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

// SaTable 列配置
const columns = reactive([
  { title: 'ID', dataIndex: 'id', width: 80 },
  { title: '游戏ID', dataIndex: 'game_id', width: 80 },
  { title: '母包名称', dataIndex: 'name', width: 220 },
  { title: '打包目录', dataIndex: 'letter', width: 120 },
  { title: '包名', dataIndex: 'package_name', width: 180 },
  { title: 'APPID（头条）', dataIndex: 'tt_appid', width: 180 },
  { title: '账号ID（头条）', dataIndex: 'tt_advertiser_id', width: 180 },
])

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

// 获取媒体列表
const getMediaOptions = async () => {
  const resp = await advertCommonApi.getMediaOptionsApi()
  if (resp.code === 200) {
    mediaOptions.value = resp.data
  }
}

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

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