<script setup lang="ts">
import CreateButton from '@/components/button/CreateButton.vue';
import { DataTable } from '@/components/data-table';
import { Card, CardContent } from '@/components/ui/card';
import { useCan } from '@/composables/useCan';
import {
    useDataTableQuery,
    useDataTableQuerySync,
} from '@/composables/useDataTableQuery';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { CustomerListItem, PaginatedCustomers } from '@/types/customer';
import type { DataTableSort } from '@/types/data-table';
import { Head } from '@inertiajs/vue3';
import { computed, ref, watch } from 'vue';
import CustomerFormModal from './form/CustomerFormModal.vue';
import { createColumns } from './table/columns';
import { index } from '@/routes/admin/master/customers';

const props = defineProps<{
    customers: PaginatedCustomers;
    filters: {
        search: string;
        sort?: string;
        direction?: 'asc' | 'desc';
    };
}>();

const { can } = useCan();
const search = ref(props.filters.search ?? '');
const formModalOpen = ref(false);
const editingCustomer = ref<CustomerListItem | null>(null);

const { query, setSearch, setSort, resetFilters, syncFromServer } =
    useDataTableQuery({
        url: index.url(),
        initial: { ...props.filters },
    });

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

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

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.customers.current_page,
    perPage: props.customers.per_page,
    lastPage: props.customers.last_page,
    total: props.customers.total,
}));

function openCreateModal() {
    editingCustomer.value = null;
    formModalOpen.value = true;
}

function openEditModal(customer: CustomerListItem) {
    editingCustomer.value = customer;
    formModalOpen.value = true;
}

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

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

<template>
    <Head title="Customer" />

    <AdminLayout>
        <div
            class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between"
        >
            <div class="space-y-1">
                <h2 class="text-2xl font-bold tracking-tight">Customer</h2>
            </div>

            <CreateButton
                v-if="can('customers.create')"
                @click="openCreateModal"
            />
        </div>

        <Card class="min-w-0">
            <CardContent class="min-w-0">
                <DataTable
                    v-model:search="search"
                    :columns="columns"
                    :data="customers.data"
                    :pagination="pagination"
                    :pagination-links="customers.links"
                    :sort="currentSort"
                    @sort-change="setSort"
                    @filters-reset="resetFilters"
                />
            </CardContent>
        </Card>

        <CustomerFormModal
            v-if="can('customers.create') || can('customers.update')"
            v-model:open="formModalOpen"
            :customer="editingCustomer"
        />
    </AdminLayout>
</template>
