<script setup lang="ts">
import { router } from '@inertiajs/vue3';
import { Check, Pencil, Trash2, X } from '@lucide/vue';
import { computed, ref } from 'vue';
import { toast } from 'vue-sonner';
import ConfirmDialog from '@/components/ConfirmDialog.vue';
import { Button } from '@/components/ui/button';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
import { useCan } from '@/composables/useCan';
import type { LeaveRequestListItem } from '@/types/leave-request';
import { destroy, approve } from '@/routes/admin/hr/leave_requests';

const props = defineProps<{
    leaveRequest: LeaveRequestListItem;
    authEmployeeId: number | null;
}>();

const emit = defineEmits<{
    edit: [leaveRequest: LeaveRequestListItem];
    reject: [leaveRequest: LeaveRequestListItem];
}>();

const { can } = useCan();

const isOwner = computed(() => (
    props.authEmployeeId !== null
    && props.leaveRequest.employee_id === props.authEmployeeId
));

const canEdit = computed(() => (
    props.leaveRequest.is_editable
    && !can('leave_requests.verify')
    && isOwner.value
    && can('leave_requests.update')
));

const canDelete = computed(() => (
    props.leaveRequest.is_editable
    && !can('leave_requests.verify')
    && isOwner.value
    && can('leave_requests.delete')
));

const deleteConfirmOpen = ref(false);
const approveConfirmOpen = ref(false);
const deleteProcessing = ref(false);
const approveProcessing = ref(false);

function destroyLeaveRequest() {
    deleteProcessing.value = true;

    router.delete(destroy.url(props.leaveRequest.id), {
        preserveScroll: true,
        onSuccess: () => {
            deleteConfirmOpen.value = false;
        },
        onError: () => {
            toast.error('Gagal menghapus pengajuan cuti.');
        },
        onFinish: () => {
            deleteProcessing.value = false;
        },
    });
}

function approveLeaveRequest() {
    approveProcessing.value = true;

    router.post(approve.url(props.leaveRequest.id), {}, {
        preserveScroll: true,
        onSuccess: () => {
            approveConfirmOpen.value = false;
        },
        onError: () => {
            toast.error('Gagal menyetujui pengajuan cuti.');
        },
        onFinish: () => {
            approveProcessing.value = false;
        },
    });
}
</script>

<template>
    <div class="flex items-center justify-end gap-1">
        <Tooltip v-if="canEdit">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8" @click="emit('edit', leaveRequest)">
                    <Pencil class="size-4" />
                    <span class="sr-only">Ubah</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Ubah</TooltipContent>
        </Tooltip>

        <Tooltip v-if="leaveRequest.can_verify && can('leave_requests.verify')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8 text-green-600 hover:text-green-600"
                    @click="approveConfirmOpen = true">
                    <Check class="size-4" />
                    <span class="sr-only">Setujui</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Setujui</TooltipContent>
        </Tooltip>

        <Tooltip v-if="leaveRequest.can_verify && can('leave_requests.verify')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="text-destructive hover:text-destructive size-8"
                    @click="emit('reject', leaveRequest)">
                    <X class="size-4" />
                    <span class="sr-only">Tolak</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Tolak</TooltipContent>
        </Tooltip>

        <Tooltip v-if="canDelete">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="text-destructive hover:text-destructive size-8"
                    @click="deleteConfirmOpen = true">
                    <Trash2 class="size-4" />
                    <span class="sr-only">Hapus</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Hapus</TooltipContent>
        </Tooltip>
    </div>

    <ConfirmDialog v-if="can('leave_requests.verify')" v-model:open="approveConfirmOpen" title="Setujui cuti?"
        :description="`Cuti ${leaveRequest.employee_name} (${leaveRequest.start_date_formatted} - ${leaveRequest.end_date_formatted}, ${leaveRequest.total_days} hari) akan disetujui.`"
        confirm-label="Setujui" cancel-label="Batal" :loading="approveProcessing" @confirm="approveLeaveRequest" />

    <ConfirmDialog v-if="canDelete" v-model:open="deleteConfirmOpen" title="Hapus pengajuan cuti?"
        :description="`Pengajuan cuti ${leaveRequest.employee_name} akan dihapus.`" confirm-label="Hapus"
        cancel-label="Batal" destructive :loading="deleteProcessing" @confirm="destroyLeaveRequest" />
</template>
