<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="game_id">
            <a-tree-select
              v-model="searchForm.game_id"
              :data="gameList"
              placeholder="请选择游戏"
              allow-clear
              :field-names="{ title: 'name', key: 'id' }"
              allow-search
              tree-checked-strategy="child"
              :tree-checkable="true"
              :max-tag-count="1"
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="媒体ID" field="media_id">
            <a-input
              v-model="searchForm.media_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="负责人ID" field="auth_id">
            <a-input
              v-model="searchForm.auth_id"
              placeholder="请输入负责人ID"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="用户名" field="user_name">
            <a-input
              v-model="searchForm.user_name"
              placeholder="请输入用户名"
              allow-clear
            />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="注册日期" field="reg_time">
            <a-date-picker
              class="w-full"
              v-model="searchForm.reg_time"
              :show-time="false"
              mode="date"
              placeholder="请选择注册日期"
            />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #roles="{ record }">
        <div v-if="record && record.roles && record.roles.length > 0">
          <p
            v-for="(role, index) in record.roles"
            :key="`${role.role_name}-${index}`"
          >
            {{ role.server_name }} {{ role.role_name }} ({{ role.role_level }})
          </p>
        </div>
        <div v-else>
          <span>-</span>
        </div>
      </template>
    </sa-table>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from "vue";
import { Message } from "@arco-design/web-vue";
import api from "../../api/gameLog/roleData";
import dayjs from "dayjs";
import commonApi from "../../api/common";

// 引用定义
const crudRef = ref();
const gameList = ref([]);

// 搜索表单
const searchForm = ref({
  game_id: "",
  media_id: "",
  site_id: "",
  auth_id: "",
  user_name: "",
  reg_time: "",
});

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  operationColumn: false,
  showSort: false,
});

// SaTable 列配置
const columns = reactive([
  { title: "用户名", dataIndex: "user_name", width: 120 },
  { title: "游戏名", dataIndex: "game_name", width: 120 },
  { title: "渠道id", dataIndex: "agent_id", width: 100 },
  { title: "广告位id", dataIndex: "site_id", width: 100 },
  { title: "注册IP", dataIndex: "ip", width: 180 },
  { title: "注册时间", dataIndex: "create_time", width: 180 },
  { title: "角色信息", width: 180, dataIndex: "roles" },
  { title: "渠道名", dataIndex: "agent_name", width: 120 },
  { title: "负责人", dataIndex: "auth_name", width: 120 },
]);

// 页面数据初始化
const initPage = async () => {
  searchForm.value.reg_time = dayjs().format("YYYY-MM-DD");
  await getGameList();
};

// 获取游戏列表
const getGameList = async () => {
  const res = await commonApi.getGameListTreeApi();
  if (res.code === 200) {
    gameList.value = res.data;
  }
};

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

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