<template>
  <div class="ma-content-block">
    <sa-table
      ref="crudRef"
      :options="options"
      :columns="columns"
      :searchForm="searchForm"
    >
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="游戏" name="game_id">
            <a-tree-select
              v-model="searchForm.game_id"
              :data="gameListTree"
              tree-checked-strategy="child"
              :tree-checkable="false"
              :max-tag-count="2"
              :fieldNames="{ title: 'name', key: 'id' }"
              allow-search
              allow-clear
              placeholder="请选择游戏"
            >
            </a-tree-select>
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #game_id="{ record }">
        {{ record.game_id }}:{{ record.game_name }}
      </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 api from "../../api/center/iosPayWay";
import commonApi from "../../api/common";

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

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

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  add: {
    show: true,
    auth: ["/v1/center/IosPayWay/save"],
    func: async () => {
      editRef.value?.open();
    },
  },
  edit: {
    show: true,
    auth: ["/v1/center/IosPayWay/update"],
    func: async (record) => {
      editRef.value?.open("edit");
      editRef.value?.setFormData(record);
    },
  },
  delete: {
    show: true,
    auth: ["/v1/center/IosPayWay/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: "game_id", width: 180 },
  { title: "开启时间", dataIndex: "pay_stime", width: 180 },
  { title: "结束时间", dataIndex: "pay_etime", width: 180 },
  { title: "充值金额", dataIndex: "pay_money", width: 180 },
  { title: "充值次数", dataIndex: "pay_num", width: 180 },
  { title: "注册天数", dataIndex: "reg_day", width: 180 },
  { title: "屏蔽地区", dataIndex: "city", width: 180 },

  {
    title: "充值渠道开关",
    dataIndex: "pay_way",
    type: "dict",
    dict: "ios_pay_way",
    width: 120,
  },
]);

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

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

// 获取游戏列表树形
const getGameListTree = async () => {
  const resp = await commonApi.getGameListTreeNoAuthApi({
    single: true,
  });
  if (resp.code === 200) {
    gameListTree.value = resp.data;
  }
};

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