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

const props = defineProps<Omit<DataTableProps<CardType>, 'columns'> & {
  onEdit: (row: CardType) => void
  onToggleStatus: (row: CardType) => void
  onDelete: (row: CardType) => void
}>()

const emit = defineEmits<{
  refresh: []
  batchEnable: []
  batchDisable: []
  batchDelete: []
}>()

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  select: 'admin.cardTypes.select',
  name: 'admin.cardTypes.columns.name',
  description: 'admin.cardTypes.columns.description',
  application_name: 'admin.cardTypes.columns.application',
  billing_type: 'admin.cardTypes.columns.billingType',
  price: 'admin.cardTypes.columns.price',
  value: 'admin.cardTypes.columns.value',
  generatedCount: 'admin.cardTypes.columns.generatedCount',
  status: 'admin.cardTypes.columns.status',
  created_at: 'admin.cardTypes.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="cardTypes">
          <UiButton variant="outline" size="sm" @click="emit('batchEnable')">
            {{ t('admin.cardTypes.batchEnableBtn') }}
          </UiButton>
          <UiButton variant="outline" size="sm" @click="emit('batchDisable')">
            {{ t('admin.cardTypes.batchDisableBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.cardTypes.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 />
            <slot name="filters" />
          </div>
          <DataTableViewOptions :table="table" :column-labels="columnLabels" />
        </div>
      </div>
    </template>
  </DataTable>
</template>
