<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="6" :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="6" :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"
            /> -->
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :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="6" :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="6" :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="6" :xs="24">
          <a-form-item label="负责人" field="auth_id">
            <auth-select v-model="searchForm.auth_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="登录日期" field="login_time">
            <a-range-picker class="w-full" v-model="searchForm.login_time" :clearable="false" />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="注册日期" field="reg_time">
            <a-range-picker class="w-full" v-model="searchForm.reg_time" />
          </a-form-item>
        </a-col>
      </template>
      <template #login_time="{ record }">
        {{ dayjs(record.login_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}
      </template>
      <template #reg_time="{ record }">
        {{ dayjs(record.reg_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}
      </template>

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

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

<script setup>
import { onMounted, ref, reactive } from 'vue'
import EditForm from './edit.vue'
import api from '../../api/gameLog/sdkLoginLog'
import commonApi from '../../api/common'
import dayjs from 'dayjs'
import GameSelect from '@/components/game-select/index.vue'
import AuthSelect from '@/components/auth-select/index.vue'

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

// 搜索表单
const searchForm = ref({
  user_name: '',
  game_id: '',
  media_id: '',
  agent_id: '',
  site_id: '',
  auth_id: '',
  login_time: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  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: 'media_id', width: 80 },
  { title: '广告位id', dataIndex: 'site_id', width: 100 },
  { title: '登录IP', dataIndex: 'ip', width: 120 },
  { title: '登录IMEI/IDFA', dataIndex: 'imei', width: 120 },
  { title: '登录OAID/CID', dataIndex: 'oaid', width: 120 },

  { title: '登录时间', dataIndex: 'login_time', width: 120 },
  { title: '注册时间', dataIndex: 'reg_time', width: 120 },

  { title: '手机品牌', dataIndex: 'brand', width: 120 },
  { title: '手机型号', dataIndex: 'model', width: 120 },
  { title: '系统版本', dataIndex: 'system_version', width: 100 },
  { title: 'sdk版本', dataIndex: 'sdk_version', width: 100 },
  { title: '设备类型', dataIndex: 'vt', type: 'dict', dict: 'vt', width: 120 },
  { title: '渠道名', dataIndex: 'agent_name', width: 100 },
  { title: '负责人', dataIndex: 'auth_name', width: 100 },
])

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

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

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

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

<script>
export default { name: 'v1/user-logs/sdkLoginLog' }
</script>
