<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>
          <ElSpace wrap>
            <ElButton v-permission="'dice:game:index:save'" @click="showDialog('add')" v-ripple>
              <template #icon>
                <ArtSvgIcon icon="ri:add-fill" />
              </template>
              {{ $t('table.actions.add') }}
            </ElButton>
            <ElButton
              v-permission="'dice:game:index:destroy'"
              :disabled="selectedRows.length === 0"
              @click="deleteSelectedRows(api.delete, refreshData)"
              v-ripple
            >
              <template #icon>
                <ArtSvgIcon icon="ri:delete-bin-5-line" />
              </template>
              {{ $t('table.actions.delete') }}
            </ElButton>
          </ElSpace>
        </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 #status="{ row }">
          <ElTag :type="row.status === 1 ? 'success' : 'info'">{{
            row.status === 1 ? $t('page.table.statusEnabled') : $t('page.table.statusDisabled')
          }}</ElTag>
        </template>
        <template #operation="{ row }">
          <div class="flex gap-2">
            <SaButton
              v-permission="'dice:game:index:update'"
              type="secondary"
              @click="showDialog('edit', row)"
            />
            <SaButton
              v-permission="'dice:game: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"
    />
  </div>
</template>

<script setup lang="ts">
  import { useTable } from '@/hooks/core/useTable'
  import { useSaiAdmin } from '@/composables/useSaiAdmin'
  import api from '../../api/game/index'
  import TableSearch from './modules/table-search.vue'
  import EditDialog from './modules/edit-dialog.vue'

  const searchForm = ref({
    provider_code: undefined,
    game_code: undefined,
    game_type: undefined,
    status: undefined
  })

  const handleSearch = (params: Record<string, unknown>) => {
    Object.assign(searchParams, params)
    getData()
  }

  const {
    columns,
    columnChecks,
    data,
    loading,
    getData,
    searchParams,
    pagination,
    resetSearchParams,
    handleSortChange,
    handleSizeChange,
    handleCurrentChange,
    refreshData
  } = useTable({
    core: {
      apiFn: api.list,
      apiParams: { limit: 100 },
      columnsFactory: () => [
        { type: 'selection', align: 'center' },
        { prop: 'id', label: 'page.table.id', width: 80, align: 'center' },
        { prop: 'provider', label: 'page.table.provider', minWidth: 120, align: 'center' },
        { prop: 'provider_code', label: 'page.table.providerCode', minWidth: 120, align: 'center' },
        { prop: 'game_code', label: 'page.table.gameCode', minWidth: 120, align: 'center' },
        { prop: 'game_key', label: 'page.table.gameKey', minWidth: 120, align: 'center' },
        { prop: 'game_name', label: 'page.table.gameName', minWidth: 120, align: 'center' },
        { prop: 'game_name_en', label: 'page.table.gameNameEn', minWidth: 120, align: 'center' },
        { prop: 'game_type', label: 'page.table.gameType', minWidth: 90, align: 'center' },
        { prop: 'sort', label: 'page.table.sort', width: 80, align: 'center' },
        { prop: 'status', label: 'page.table.status', width: 90, align: 'center', useSlot: true },
        { prop: 'update_time', label: 'page.table.updateTime', minWidth: 160, align: 'center' },
        {
          prop: 'operation',
          label: 'table.actions.operation',
          width: 110,
          align: 'center',
          fixed: 'right',
          useSlot: true
        }
      ]
    }
  })

  const {
    dialogType,
    dialogVisible,
    dialogData,
    showDialog,
    deleteRow,
    deleteSelectedRows,
    handleSelectionChange,
    selectedRows
  } = useSaiAdmin()
</script>
