<template>
  <div class="art-full-height">
    <!-- 搜索面板 -->
    <TableSearch v-model="searchForm" @search="handleSearch" @reset="resetSearchParams" />

    <ElCard class="art-table-card" shadow="never">
      <!-- 表格头部 -->
      <ArtTableHeader v-model:columns="columnChecks" :loading="loading" @refresh="refreshData">
        <template #left>
          <ElButton
            v-permission="'dice:lottery_pool_config:index:index'"
            type="primary"
            @click="showCurrentPoolDialog"
          >
            {{ $t('page.toolbar.viewCurrentPool') }}
          </ElButton>
        </template>
      </ArtTableHeader>

      <!-- 表格 -->
      <ArtTable
        ref="tableRef"
        rowKey="id"
        :loading="loading"
        :data="data"
        :columns="columns"
        :pagination="pagination"
        @sort-change="handleSortChange"
        @selection-change="handleSelectionChange"
        @pagination:size-change="handleSizeChange"
        @pagination:current-change="handleCurrentChange"
      >
        <!-- 操作列 -->
        <template #operation="{ row }">
          <div class="flex gap-2">
            <SaButton
              v-permission="'dice:lottery_pool_config:index:update'"
              type="secondary"
              @click="showDialog('edit', row)"
            />
            <!--            <SaButton-->
            <!--              v-permission="'dice:lottery_pool_config:index:destroy'"-->
            <!--              type="error"-->
            <!--              @click="deleteRow(row, api.delete, refreshData)"-->
            <!--            />-->
          </div>
        </template>
      </ArtTable>
    </ElCard>

    <!-- 编辑弹窗 -->
    <EditDialog
      v-model="dialogVisible"
      :dialog-type="dialogType"
      :data="dialogData"
      @success="refreshData"
    />
    <!-- 当前彩金池弹窗 -->
    <CurrentPoolDialog v-model="currentPoolVisible" @success="refreshData" />
  </div>
</template>

<script setup lang="ts">
  import { useI18n } from 'vue-i18n'
  import { useTable } from '@/hooks/core/useTable'
  import { useSaiAdmin } from '@/composables/useSaiAdmin'
  import api from '../../api/lottery_pool_config/index'
  import TableSearch from './modules/table-search.vue'
  import EditDialog from './modules/edit-dialog.vue'
  import CurrentPoolDialog from './modules/current-pool-dialog.vue'
  const { t } = useI18n()

  // 搜索表单
  const searchForm = ref({
    name: undefined,
    // type 字段已移除，改用 name 区分：default/killScore/up
  })

  // 搜索处理
  const handleSearch = (params: Record<string, any>) => {
    Object.assign(searchParams, params)
    getData()
  }

  // 奖池类型展示：按 name 映射
  const typeFormatter = (row: Record<string, unknown>) => {
    const n = String(row.name ?? '')
    if (n === 'default') return t('page.search.poolTypeNormal')
    if (n === 'killScore') return t('page.search.poolTypeKill')
    if (n === 'up') return t('page.search.poolTypeT1')
    return n || '-'
  }

  // 权重列带 %
  const weightFormatter = (prop: string) => (row: Record<string, unknown>) => {
    const v = row[prop]
    return v != null && v !== '' ? `${v}%` : '-'
  }

  // 表格配置
  const {
    columns,
    columnChecks,
    data,
    loading,
    getData,
    searchParams,
    pagination,
    resetSearchParams,
    handleSortChange,
    handleSizeChange,
    handleCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: api.list,
      columnsFactory: () => [
        { prop: 'name', label: 'page.table.name', align: 'center' },
        { prop: 'name', label: 'page.table.poolType', width: 100, align: 'center', formatter: typeFormatter },
        { prop: 'safety_line', label: 'page.table.safetyLine', align: 'center' },
        {
          prop: 't1_weight',
          label: 'page.table.t1PoolWeight',
          width: 100,
          align: 'center',
          formatter: weightFormatter('t1_weight')
        },
        {
          prop: 't2_weight',
          label: 'page.table.t2PoolWeight',
          width: 100,
          align: 'center',
          formatter: weightFormatter('t2_weight')
        },
        {
          prop: 't3_weight',
          label: 'page.table.t3PoolWeight',
          width: 100,
          align: 'center',
          formatter: weightFormatter('t3_weight')
        },
        {
          prop: 't4_weight',
          label: 'page.table.t4PoolWeight',
          width: 100,
          align: 'center',
          formatter: weightFormatter('t4_weight')
        },
        {
          prop: 't5_weight',
          label: 'page.table.t5PoolWeight',
          width: 100,
          align: 'center',
          formatter: weightFormatter('t5_weight')
        },
        {
          prop: 'operation',
          label: 'table.actions.operation',
          width: 60,
          align: 'center',
          fixed: 'right',
          useSlot: true
        }
      ]
    }
  })

  // 编辑配置
  const { dialogType, dialogVisible, dialogData, showDialog, handleSelectionChange } = useSaiAdmin()

  const currentPoolVisible = ref(false)
  function showCurrentPoolDialog() {
    currentPoolVisible.value = true
  }
</script>
