<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">
            <game-select v-model="searchForm.game_id" multiple />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :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="8" :xs="24">
          <a-form-item label="作者" field="author_id">
            <author-select v-model="searchForm.author_id" />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="媒体" field="media_id">
            <media-select v-model="searchForm.media_id" />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="消耗筛选" field="cost_type">
            <a-select v-model="searchForm.cost_type" placeholder="请选择消耗筛选" :options="costTypeOptions" />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="素材名称" field="material_name">
            <a-input v-model="searchForm.material_name" placeholder="请输入素材名称" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :xs="24">
          <a-form-item label="素材ID" field="material_id">
            <a-input v-model="searchForm.material_id" placeholder="请输入素材ID" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :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="8" :xs="24">
          <a-form-item label="归类" field="group">
            <a-select v-model="searchForm.group" placeholder="请选择归类" :options="groupOptions" />
          </a-form-item>
        </a-col>
      </template>

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

<script setup>
import { onMounted, ref, reactive } from 'vue'
import api from '@/views/v1/api/gameLog/material'
import dayjs from 'dayjs'
import GameSelect from '@/components/game-select/index.vue'
import MediaSelect from '@/components/media-select/index.vue'
import AuthorSelect from '@/components/author-select/index.vue'
import AuthSelect from '@/components/auth-select/index.vue'

// 引用定义
const crudRef = ref()
const costTypeOptions = ref([
  {
    value: 1,
    label: '2000消耗以上（包括2000）',
  },
  {
    value: 2,
    label: '2000消耗以下',
  },
])
const groupOptions = ref([
  {
    value: 1,
    label: '按素材ID',
  },
  {
    value: 2,
    label: '按作者',
  },
  {
    value: 3,
    label: '按素材ID + 投手',
  },
])

// 搜索表单
const searchForm = ref({
  game_id: '',
  media_id: '',
  auth_id: '',
  agent_id: '',
  site_id: '',
  reg_date: [],
  cost_type: '',
  material_name: '',
  material_id: '',
  group: 1,
})

// SaTable 基础配置
const options = reactive({
  api: api.getMaterialListApi,
  pk: 'id',
  showSort: false,
  operationColumn: false,
  showSummary: true,
  summary: [
    {
      action: 'totalRow',
      dataIndex: 'cost',
    },
    {
      action: 'totalRow',
      dataIndex: 'ad_show',
    },
    {
      action: 'totalRow',
      dataIndex: 'click',
    },
    {
      action: 'totalRow',
      dataIndex: 'ad_click_rate',
    },
    {
      action: 'totalRow',
      dataIndex: 'active',
    },
    {
      action: 'totalRow',
      dataIndex: 'register',
    },
    {
      action: 'totalRow',
      dataIndex: 'reg_cost',
    },
    {
      action: 'totalRow',
      dataIndex: 'pay_amount',
    },
    {
      action: 'totalRow',
      dataIndex: 'pay_cost',
    },
  ],
})

// SaTable 列配置
const columns = reactive([
  { title: '素材ID', dataIndex: 'material_id', width: 120 },
  { title: '素材名称', dataIndex: 'material_name', width: 290 },
  { title: '作者', dataIndex: 'author_id', width: 120 },
  { title: '投手', dataIndex: 'auth_id', width: 120 },
  { title: '消耗', dataIndex: 'cost', width: 120 },
  { title: '展示数', dataIndex: 'ad_show', width: 120 },
  { title: '点击数', dataIndex: 'click', width: 120 },
  { title: '点击率', dataIndex: 'ad_click_rate', width: 120 },
  { title: '激活数', dataIndex: 'active', width: 120 },
  { title: '注册数', dataIndex: 'register', width: 120 },
  { title: '注册成本', dataIndex: 'reg_cost', width: 120 },
  { title: '付费数', dataIndex: 'pay_amount', width: 120 },
  { title: '付费成本', dataIndex: 'pay_cost', width: 120 },
])

// 页面数据初始化
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>
