<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :sm="12" :md="6" :lg="7" :xl="6" :xxl="4" :xs="24">
          <a-form-item label="游戏" field="game_id">
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="24" :md="17" :lg="17" :xl="12" :xxl="8" :xs="24">
          <a-form-item label="注册日期" field="tdate">
            <a-space>
              <a-date-picker class="w-full" v-model="searchForm.tdate" :show-time="false" mode="date" />
              <a-button size="mini" type="primary" @click="refreshDay('yesterday')">前一天</a-button>
              <a-button size="mini" type="primary" @click="refreshDay('today')">当天</a-button>
              <a-button size="mini" type="primary" @click="refreshDay('tomorrow')">后一天</a-button>
            </a-space>
          </a-form-item>
        </a-col>
      </template>
      <!-- Table 自定义渲染 -->
    </sa-table>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import api from '../../api/gameLog/analyse'
import dayjs from 'dayjs'
import GameSelect from '@/components/game-select/index.vue'

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

// 搜索表单
const searchForm = ref({
  game_id: '',
  media_id: '',
  auth_id: '',
  agent_id: '',
  agent_name: '',
  site_name: '',
  site_id: '',
  tdate: '',
  data_type: 'recovery',
})

// SaTable 基础配置
const options = reactive({
  api: api.getRegHourDataList,
  pk: 'tdate',
  showSort: false,
  showSummary: true,
  singleLine: true,
  summary: [
    {
      action: 'totalRow',
      dataIndex: 'total',
    },
    {
      action: 'totalRow',
      dataIndex: 'h0',
    },
    {
      action: 'totalRow',
      dataIndex: 'h1',
    },
    {
      action: 'totalRow',
      dataIndex: 'h2',
    },
    {
      action: 'totalRow',
      dataIndex: 'h3',
    },
    {
      action: 'totalRow',
      dataIndex: 'h4',
    },
    {
      action: 'totalRow',
      dataIndex: 'h5',
    },
    {
      action: 'totalRow',
      dataIndex: 'h6',
    },
    {
      action: 'totalRow',
      dataIndex: 'h7',
    },
    {
      action: 'totalRow',
      dataIndex: 'h8',
    },
    {
      action: 'totalRow',
      dataIndex: 'h9',
    },
    {
      action: 'totalRow',
      dataIndex: 'h10',
    },
    {
      action: 'totalRow',
      dataIndex: 'h11',
    },
    {
      action: 'totalRow',
      dataIndex: 'h12',
    },
    {
      action: 'totalRow',
      dataIndex: 'h13',
    },
    {
      action: 'totalRow',
      dataIndex: 'h14',
    },
    {
      action: 'totalRow',
      dataIndex: 'h15',
    },
    {
      action: 'totalRow',
      dataIndex: 'h16',
    },
    {
      action: 'totalRow',
      dataIndex: 'h17',
    },
    {
      action: 'totalRow',
      dataIndex: 'h18',
    },
    {
      action: 'totalRow',
      dataIndex: 'h19',
    },
    {
      action: 'totalRow',
      dataIndex: 'h20',
    },
    {
      action: 'totalRow',
      dataIndex: 'h21',
    },
    {
      action: 'totalRow',
      dataIndex: 'h22',
    },
    {
      action: 'totalRow',
      dataIndex: 'h23',
    },
    {
      action: 'totalRow',
      dataIndex: 'h24',
    },
  ],

  operationColumn: false,
})

// SaTable 列配置
const columns = reactive([
  { title: '游戏ID', dataIndex: 'game_id', width: 130, fixed: 'left' },
  { title: '游戏名', dataIndex: 'game_name', width: 120, fixed: 'left' },
  { title: '注册用户数', dataIndex: 'total', width: 140, fixed: 'left' },
  { title: '0', dataIndex: 'h0', width: 80 },
  { title: '1', dataIndex: 'h1', width: 80 },
  { title: '2', dataIndex: 'h2', width: 80 },
  { title: '3', dataIndex: 'h3', width: 80 },
  { title: '4', dataIndex: 'h4', width: 80 },
  { title: '5', dataIndex: 'h5', width: 80 },
  { title: '6', dataIndex: 'h6', width: 80 },
  { title: '7', dataIndex: 'h7', width: 80 },
  { title: '8', dataIndex: 'h8', width: 80 },
  { title: '9', dataIndex: 'h9', width: 80 },
  { title: '10', dataIndex: 'h10', width: 80 },
  { title: '11', dataIndex: 'h11', width: 80 },
  { title: '12', dataIndex: 'h12', width: 80 },
  { title: '13', dataIndex: 'h13', width: 80 },
  { title: '14', dataIndex: 'h14', width: 80 },
  { title: '15', dataIndex: 'h15', width: 80 },
  { title: '16', dataIndex: 'h16', width: 80 },
  { title: '17', dataIndex: 'h17', width: 80 },
  { title: '18', dataIndex: 'h18', width: 80 },
  { title: '19', dataIndex: 'h19', width: 80 },
  { title: '20', dataIndex: 'h20', width: 80 },
  { title: '21', dataIndex: 'h21', width: 80 },
  { title: '22', dataIndex: 'h22', width: 80 },
  { title: '23', dataIndex: 'h23', width: 80 },
])

// 页面数据初始化
const initPage = () => {
  searchForm.value.tdate = dayjs().format('YYYY-MM-DD')
}

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

const refreshDay = async (day) => {
  console.log(day)
  switch (day) {
    case 'yesterday':
      searchForm.value.tdate = dayjs(searchForm.value.tdate).subtract(1, 'day').format('YYYY-MM-DD')
      break
    case 'today':
      searchForm.value.tdate = dayjs().format('YYYY-MM-DD')
      break
    case 'tomorrow':
      searchForm.value.tdate = dayjs(searchForm.value.tdate).add(1, 'day').format('YYYY-MM-DD')
      break
    default:
      break
  }
  refresh()
}

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

<script>
export default { name: 'v1/analyse/regHour' }
</script>
