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

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

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

const { t } = useI18n()

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

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

const columnLabels: Record<string, string> = {
  select: 'admin.announcements.select',
  title: 'admin.announcements.columns.title',
  application_name: 'admin.announcements.columns.application',
  type: 'admin.announcements.columns.type',
  status: 'admin.announcements.columns.status',
  is_top: 'admin.announcements.columns.isTop',
  created_at: 'admin.announcements.columns.createdAt',
}

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="announcements">
          <UiButton variant="outline" size="sm" @click="emit('batchExport')">
            {{ t('admin.announcements.batchExportBtn') }}
          </UiButton>
          <UiButton variant="destructive" size="sm" @click="emit('batchDelete')">
            {{ t('admin.announcements.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.announcements.exportBtn') }}
            </UiButton>
            <DataTableViewOptions :table="table" :column-labels="columnLabels" />
          </div>
        </div>
      </div>
    </template>
  </DataTable>
</template>
