<script setup lang="ts">
import { Head } from '@inertiajs/vue3';
import { Plus } from '@lucide/vue';
import { computed, ref, watch } from 'vue';
import LeaveRequestFormModal from './form/LeaveRequestFormModal.vue';
import RejectLeaveRequestModal from './form/RejectLeaveRequestModal.vue';
import { createColumns } from './table/columns';
import { DataTable } from '@/components/data-table';
import { Button } from '@/components/ui/button';
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 { DataTableSort } from '@/types/data-table';
import type { LeaveRequestListItem, LeaveRequestPageProps } from '@/types/leave-request';

const props = defineProps<LeaveRequestPageProps>();

const { can } = useCan();
const search = ref(props.filters.search ?? '');
const formModalOpen = ref(false);
const rejectModalOpen = ref(false);
const editingLeaveRequest = ref<LeaveRequestListItem | null>(null);
const rejectingLeaveRequest = ref<LeaveRequestListItem | null>(null);

const { query, setSearch, setSort, resetFilters, syncFromServer } = useDataTableQuery({
    url: '/admin/hr/leave-requests',
    initial: { ...props.filters },
});

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

const columns = computed(() => createColumns(
    props.authEmployeeId,
    openEditModal,
    openRejectModal,
));

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

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

function openEditModal(leaveRequest: LeaveRequestListItem) {
    editingLeaveRequest.value = leaveRequest;
    formModalOpen.value = true;
}

function openRejectModal(leaveRequest: LeaveRequestListItem) {
    rejectingLeaveRequest.value = leaveRequest;
    rejectModalOpen.value = true;
}

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

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

<template>

    <Head title="Cuti" />

    <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">
                    Cuti
                </h2>
            </div>

            <Button v-if="canSubmit && !hasPending" class="shrink-0 self-start sm:self-center" @click="openCreateModal">
                <Plus class="size-4" />
                Ajukan
            </Button>
        </div>

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

        <LeaveRequestFormModal v-if="canSubmit" v-model:open="formModalOpen"
            :leave-request="editingLeaveRequest" />

        <RejectLeaveRequestModal v-if="can('leave-requests.verify')" v-model:open="rejectModalOpen"
            :leave-request="rejectingLeaveRequest" />
    </AdminLayout>
</template>
