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

import { computed } from 'vue'
import { useI18n } from 'vue-i18n'

import type { DataTableProps } from '@/components/data-table/types'
import type { Card } from '@/pages/admin/cards/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/cards/components/columns'
import DataTableToolbar from '@/pages/admin/cards/components/data-table-toolbar.vue'

const props = defineProps<Omit<DataTableProps<Card>, 'columns'> & {
  onToggleStatus: (row: Card) => void
  onDelete: (row: Card) => void
  searchFilter?: string
}>()

const emit = defineEmits<{
  'refresh': []
  'batchDisable': []
  'batchEnable': []
  'batchDelete': []
  'batchExport': []
  'export': []
  'update:searchFilter': [value: string]
}>()

const { t } = useI18n()

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

const table = generateVueTable<Card>({
  get data() { return props.data },
  get loading() { return props.loading },
  columns: columns.value,
}, columns.value)

const columnLabels: Record<string, string> = {
  select: 'admin.cards.select',
  card_key: 'admin.cards.columns.cardKey',
  application_name: 'admin.cards.columns.application',
  card_type_name: 'admin.cards.columns.type',
  card_type_price: 'admin.cards.columns.faceValue',
  card_type_value: 'admin.cards.columns.duration',
  status: 'admin.cards.columns.status',
  app_user_username: 'admin.cards.columns.usedUser',
  used_at: 'admin.cards.columns.usedTime',
  creator_username: 'admin.cards.columns.creator',
  created_at: 'admin.cards.columns.createdAt',
}

defineExpose({
  table,
})
</script>

<template>
  <DataTable :columns="columns" :data :loading :table @refresh="emit('refresh')">
    <template #toolbar>
      <div class="space-y-3">
        <div class="flex items-center justify-end">
          <slot name="actions" />
        </div>
        <BulkActions :table="table" entity-name="cards">
          <UiButton variant="outline" size="sm" @click="emit('batchEnable')">
            {{ t('admin.cards.batchEnableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchDisable')">
            {{ t('admin.cards.batchDisableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchExport')">
            {{ t('admin.cards.batchExportBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.cards.batchDeleteBtn') }}
          </UiButton>
        </BulkActions>
        <div class="flex flex-wrap items-center justify-between gap-2">
          <div class="flex flex-wrap items-center gap-2">
            <DataTableToolbar
              :table
              :search-filter="searchFilter"
              @update:search-filter="emit('update:searchFilter', $event)"
            />
            <slot name="filters" />
          </div>
          <div class="flex items-center gap-2">
            <UiButton variant="outline" size="sm" @click="emit('export')">
              {{ t('admin.cards.exportBtn') }}
            </UiButton>
            <DataTableViewOptions :table="table" :column-labels="columnLabels" />
          </div>
        </div>
      </div>
    </template>
  </DataTable>
</template>
