<script setup lang="ts">
import type { ColumnDef } from '@tanstack/vue-table'

import type { DataTableProps } from '@/components/data-table/types'
import type { RiskRule } from '@/pages/admin/risk-control/data/schema'

import BulkActions from '@/components/data-table/bulk-actions.vue'
import DataTable from '@/components/data-table/data-table.vue'
import { SelectColumn } from '@/components/data-table/table-columns'
import { generateVueTable } from '@/components/data-table/use-generate-vue-table'
import DataTableViewOptions from '@/components/data-table/view-options.vue'
import { getColumns } from '@/pages/admin/risk-control/components/columns'
import DataTableToolbar from '@/pages/admin/risk-control/components/data-table-toolbar.vue'

const props = defineProps<Omit<DataTableProps<RiskRule>, 'columns'> & {
  onToggleStatus: (row: RiskRule) => void
  onEdit: (row: RiskRule) => void
  onDelete: (row: RiskRule) => void
}>()
const emit = defineEmits<{
  refresh: []
  batchDisable: []
  batchEnable: []
  batchDelete: []
}>()

const columns = computed(() => [
  SelectColumn as ColumnDef<RiskRule>,
  ...getColumns({
    onToggleStatus: props.onToggleStatus,
    onEdit: props.onEdit,
    onDelete: props.onDelete,
  }),
])

const table = generateVueTable<RiskRule>({ ...props, columns: columns.value }, columns.value)

const columnLabels: Record<string, string> = {
  select: '选择',
  type: '类型',
  value: '封禁值',
  reason: '原因',
  status: '状态',
  expires_at: '过期时间',
  created_at: '创建时间',
}

defineExpose({
  table,
})
</script>

<template>
  <DataTable :columns="columns" :data :loading :table @refresh="emit('refresh')">
    <template #toolbar>
      <div class="space-y-3">
        <BulkActions :table="table" entity-name="rules">
          <UiButton variant="outline" size="sm" @click="emit('batchEnable')">
            批量启用
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchDisable')">
            批量禁用
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            批量删除
          </UiButton>
        </BulkActions>
        <div class="flex flex-wrap items-center justify-between gap-2">
          <div class="flex flex-wrap items-center gap-2">
            <DataTableToolbar :table />
            <slot name="filters" />
          </div>
          <div class="flex items-center gap-2">
            <DataTableViewOptions :table="table" :column-labels="columnLabels" />
          </div>
        </div>
      </div>
    </template>
  </DataTable>
</template>
