<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="game_id">
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="注册日期" field="reg_date">
            <a-range-picker class="w-full" v-model="searchForm.reg_date" :show-time="false" mode="date" />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="负责人" field="agent_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="media_id">
            <media-select v-model="searchForm.media_id" />
          </a-form-item>
        </a-col>
        <a-col :sm="6" :xs="24">
          <a-form-item label="显示方式" field="agent_id">
            <a-select v-model="searchForm.show_type" :options="showTypeOptions" />
          </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'
import MediaSelect from '@/components/media-select/index.vue'
import AuthSelect from '@/components/auth-select/index.vue'

// 引用定义
const crudRef = ref()
const showTypeOptions = ref([
  {
    label: '按比率',
    value: 'rate',
  },
  {
    label: '按人数',
    value: 'num',
  },
])

// 搜索表单
const searchForm = ref({
  game_id: '',
  reg_date: [],
  auth_id: '',
  media_id: '',
  show_type: 'rate',
})

// SaTable 基础配置
const options = reactive({
  api: api.getPayRetention,
  pk: 'date',
  showSort: false,
  showSummary: true,
  operationColumn: false,
  summary: [
    {
      action: 'totalRow',
      dataIndex: 'reg_date',
    },
    {
      action: 'totalRow',
      dataIndex: 'reg_pay_num',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_2',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_3',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_4',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_5',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_6',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_7',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_15',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_30',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_35',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_40',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_55',
    },
    {
      action: 'totalRow',
      dataIndex: 'remain_60',
    },
  ],
})

// SaTable 列配置
const columns = reactive([
  { title: '注册日期', dataIndex: 'reg_date', width: 120, fixed: 'left' },
  { title: '付费人数', dataIndex: 'reg_pay_num', width: 120 },
  {
    title: '次留',
    dataIndex: 'remain_2',
    width: 90,
  },
  {
    title: '3留',
    dataIndex: 'remain_3',
    width: 90,
  },
  {
    title: '4留',
    dataIndex: 'remain_4',
    width: 90,
  },
  {
    title: '5留',
    dataIndex: 'remain_5',
    width: 90,
  },
  {
    title: '6留',
    dataIndex: 'remain_6',
    width: 90,
  },
  {
    title: '7留',
    dataIndex: 'remain_7',
    width: 90,
  },
  {
    title: '15留',
    dataIndex: 'remain_15',
    width: 90,
  },
  {
    title: '30留',
    dataIndex: 'remain_30',
    width: 90,
  },
  {
    title: '35留',
    dataIndex: 'remain_35',
    width: 90,
  },
  {
    title: '40留',
    dataIndex: 'remain_40',
    width: 90,
  },
  {
    title: '55留',
    dataIndex: 'remain_55',
    width: 90,
  },
  {
    title: '60留',
    dataIndex: 'remain_60',
    width: 90,
  },
])

// 页面数据初始化
const initPage = async () => {
  searchForm.value.reg_date[0] = dayjs().subtract(8, 'day').format('YYYY-MM-DD')
  searchForm.value.reg_date[1] = dayjs().subtract(1, 'day').format('YYYY-MM-DD')
}

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

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

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