<script setup lang="ts">
import { Head } from '@inertiajs/vue3';
import { computed, ref, watch } from 'vue';
import { DataTable } from '@/components/data-table';
import { Card, CardContent } from '@/components/ui/card';
import { useDataTableQuery, useDataTableQuerySync } from '@/composables/useDataTableQuery';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type {
    OwnerVerificationItem,
    PaginatedOwnerVerificationRequests,
    SelectOption,
} from '@/types/owner-verification';
import type { DataTableSort } from '@/types/data-table';
import OwnerVerificationDetailModal from './table/OwnerVerificationDetailModal.vue';
import { createColumns } from './table/columns';
import { index } from '@/routes/admin/manage/owner_verifications';

const props = defineProps<{
    verificationRequests: PaginatedOwnerVerificationRequests;
    filters: {
        search: string;
        sort?: string;
        direction?: 'asc' | 'desc';
        status?: string;
        subject_type?: string;
        action?: string;
    };
    statusOptions: SelectOption[];
    subjectOptions: SelectOption[];
    actionOptions: SelectOption[];
}>();

const search = ref(props.filters.search ?? '');
const detailModalOpen = ref(false);
const selectedItem = ref<OwnerVerificationItem | null>(null);

const { query, setSearch, setSort, setFilter, resetFilters, syncFromServer } = useDataTableQuery({
    url: index.url(),
    initial: { ...props.filters },
    filterKeys: ['status', 'subject_type', 'action'],
});

useDataTableQuerySync(() => props.filters, syncFromServer);

const columns = computed(() => createColumns(openDetailModal));

const filterDefs = computed(() => [
    {
        key: 'status',
        label: 'Status',
        type: 'select' as const,
        options: props.statusOptions,
    },
    {
        key: 'subject_type',
        label: 'Modul',
        type: 'select' as const,
        options: props.subjectOptions,
    },
    {
        key: 'action',
        label: 'Aksi',
        type: 'select' as const,
        options: props.actionOptions,
    },
]);

const filterValues = computed(() => ({
    status: query.value.status ?? '',
    subject_type: query.value.subject_type ?? '',
    action: query.value.action ?? '',
}));

const currentSort = computed<DataTableSort | null>(() => {
    if (!query.value.sort || !query.value.direction) {
        return null;
    }

    return {
        column: query.value.sort,
        direction: query.value.direction,
    };
});

const pagination = computed(() => ({
    currentPage: props.verificationRequests.current_page,
    perPage: props.verificationRequests.per_page,
    lastPage: props.verificationRequests.last_page,
    total: props.verificationRequests.total,
}));

function openDetailModal(item: OwnerVerificationItem) {
    selectedItem.value = item;
    detailModalOpen.value = true;
}

watch(search, (value) => {
    setSearch(value);
});

watch(
    () => props.filters.search,
    (value) => {
        search.value = value ?? '';
    },
);
</script>

<template>
    <Head title="Verifikasi Owner" />

    <AdminLayout>
        <div class="space-y-1">
            <h2 class="text-2xl font-bold tracking-tight">
                Verifikasi Owner
            </h2>
        </div>

        <Card class="min-w-0">
            <CardContent class="min-w-0">
                <DataTable
                    v-model:search="search"
                    :columns="columns"
                    :data="verificationRequests.data"
                    :pagination="pagination"
                    :pagination-links="verificationRequests.links"
                    :sort="currentSort"
                    :filter-defs="filterDefs"
                    :filter-values="filterValues"
                    search-placeholder="Cari verifikasi..."
                    @sort-change="setSort"
                    @filter-change="setFilter"
                    @filters-reset="resetFilters"
                />
            </CardContent>
        </Card>

        <OwnerVerificationDetailModal
            v-model:open="detailModalOpen"
            :item="selectedItem"
        />
    </AdminLayout>
</template>
