<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="media_id">
            <media-select v-model="searchForm.media_id" />
          </a-form-item>
        </a-col>
        <a-col :sm="8" :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="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="负责人" field="auth_id">
            <auth-select v-model="searchForm.auth_id" multiple />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #agent_name="{ record }">
        <div v-if="!record.agent_name">
          <div style="color: purple">消耗</div>
          <div>注册</div>
          <div style="color: blue">注册成本</div>
          <div style="color: green">付费率</div>
          <div style="color: red">回本率</div>
        </div>
      </template>
      <template #total_raw="{ record }">
        <div>
          <div style="color: purple">
            {{ record.total_raw?.cost }}
          </div>
          <div>
            {{ record.total_raw?.reg_total }}
          </div>
          <div style="color: blue">
            {{ record.total_raw?.reg_cost }}
          </div>
          <div style="color: green">
            {{ record.total_raw?.pay_rate }}
          </div>
          <div style="color: red">
            {{ record.total_raw?.roi }}
          </div>
        </div>
      </template>
      <template #h0="{ record }">
        <div>
          <div style="color: purple">
            {{ record['h0']?.cost }}
          </div>
          <div>
            {{ record['h0']?.reg_total }}
          </div>
          <div style="color: blue">
            {{ record['h0']?.reg_cost }}
          </div>
          <div style="color: green">
            {{ record['h0']?.pay_rate }}
          </div>
          <div style="color: red">
            {{ record['h0']?.roi }}
          </div>
        </div>
      </template>
      <template v-for="i in 23" :key="i" #[`h${i}`]="{ record }">
        <div>
          <div style="color: purple">
            {{ record[`h${i}`]?.cost }}
          </div>
          <div>
            {{ record[`h${i}`]?.reg_total }}
          </div>
          <div style="color: blue">
            {{ record[`h${i}`]?.reg_cost }}
          </div>
          <div style="color: green">
            {{ record[`h${i}`]?.pay_rate }}
          </div>
          <div style="color: red">
            {{ record[`h${i}`]?.roi }}
          </div>
        </div>
      </template>
    </sa-table>
  </div>
</template>

<script setup>
import { onMounted, ref, reactive } from 'vue'
import api from '../../api/gameLog/channelAnalysis'
import dayjs from 'dayjs'
import commonApi from '../../api/common'
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 gameList = ref([])

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

// SaTable 基础配置
const options = reactive({
  api: api.getHourList,
  pk: 'id',
  rowSelection: { showCheckedAll: true },

  operationColumn: false,
})

// SaTable 列配置
const columns = reactive([
  { title: '渠道ID', dataIndex: 'agent_id', width: 80 },
  { title: '渠道名', dataIndex: 'agent_name', width: 100 },
  { title: '合计', dataIndex: 'total_raw', width: 80 },
  { title: 'h0', dataIndex: 'h0', width: 80 },
  { title: 'h1', dataIndex: 'h1', width: 80 },
  { title: 'h2', dataIndex: 'h2', width: 80 },
  { title: 'h3', dataIndex: 'h3', width: 80 },
  { title: 'h4', dataIndex: 'h4', width: 80 },
  { title: 'h5', dataIndex: 'h5', width: 80 },
  { title: 'h6', dataIndex: 'h6', width: 80 },
  { title: 'h7', dataIndex: 'h7', width: 80 },
  { title: 'h8', dataIndex: 'h8', width: 80 },
  { title: 'h9', dataIndex: 'h9', width: 80 },
  { title: 'h10', dataIndex: 'h10', width: 80 },
  { title: 'h11', dataIndex: 'h11', width: 80 },
  { title: 'h12', dataIndex: 'h12', width: 80 },
  { title: 'h13', dataIndex: 'h13', width: 80 },
  { title: 'h14', dataIndex: 'h14', width: 80 },
  { title: 'h15', dataIndex: 'h15', width: 80 },
  { title: 'h16', dataIndex: 'h16', width: 80 },
  { title: 'h17', dataIndex: 'h17', width: 80 },
  { title: 'h18', dataIndex: 'h18', width: 80 },
  { title: 'h19', dataIndex: 'h19', width: 80 },
  { title: 'h20', dataIndex: 'h20', width: 80 },
  { title: 'h21', dataIndex: 'h21', width: 80 },
  { title: 'h22', dataIndex: 'h22', width: 80 },
  { title: 'h23', dataIndex: 'h23', width: 80 },
])

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

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

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

<script>
export default { name: 'v1/channelAnalysis/hour' }
</script>
