<script setup lang="ts">
import { router } from '@inertiajs/vue3';
import { Banknote, 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 { useDestroy } from '@/composables/useDestroy';
import type { EmployeeAdvanceListItem } from '@/types/employee-advance';
import { destroy, approve, pay } from '@/routes/admin/finance/employee_advances';

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

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

const { can } = useCan();

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

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

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

const approveConfirmOpen = ref(false);
const payConfirmOpen = ref(false);
const approveProcessing = ref(false);
const payProcessing = ref(false);

const { open: deleteConfirmOpen, processing: deleteProcessing, destroy: destroyEmployeeAdvance } = useDestroy({
    url: destroy.url(props.employeeAdvance.id),
    errorMessage: 'Gagal menghapus kasbon.',
});

function approveEmployeeAdvance() {
    approveProcessing.value = true;

    router.post(approve.url(props.employeeAdvance.id), {}, {
        preserveScroll: true,
        onSuccess: () => {
            approveConfirmOpen.value = false;
        },
        onError: (errors) => {
            toast.error(errors.amount || errors.employee_advance || 'Gagal menyetujui kasbon.');
        },
        onFinish: () => {
            approveProcessing.value = false;
        },
    });
}

function payEmployeeAdvance() {
    payProcessing.value = true;

    router.post(pay.url(props.employeeAdvance.id), {}, {
        preserveScroll: true,
        onSuccess: () => {
            payConfirmOpen.value = false;
        },
        onError: (errors) => {
            toast.error(errors.amount || errors.employee_advance || 'Gagal melunasi kasbon.');
        },
        onFinish: () => {
            payProcessing.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', employeeAdvance)">
                    <Pencil class="size-4" />
                    <span class="sr-only">Ubah</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Ubah</TooltipContent>
        </Tooltip>

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

        <Tooltip v-if="employeeAdvance.can_pay && can('employee_advances.pay')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8" @click="payConfirmOpen = true">
                    <Banknote class="size-4" />
                    <span class="sr-only">Lunasi</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Lunasi Kasbon</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('employee_advances.verify')" v-model:open="approveConfirmOpen" title="Setujui kasbon?"
        :description="`Kasbon ${employeeAdvance.amount_formatted} untuk ${employeeAdvance.employee_name} akan dicairkan. Saldo kas akan berkurang.`"
        confirm-label="Setujui" cancel-label="Batal" :loading="approveProcessing" @confirm="approveEmployeeAdvance" />

    <ConfirmDialog v-if="can('employee_advances.pay')" v-model:open="payConfirmOpen" title="Lunasi kasbon?"
        :description="`Pelunasan kasbon ${employeeAdvance.amount_formatted} dari ${employeeAdvance.employee_name}. Saldo kas akan bertambah.`"
        confirm-label="Lunasi" cancel-label="Batal" :loading="payProcessing" @confirm="payEmployeeAdvance" />

    <ConfirmDialog v-if="canDelete" v-model:open="deleteConfirmOpen" title="Hapus kasbon?"
        :description="`Pengajuan kasbon ${employeeAdvance.amount_formatted} akan dihapus.`" confirm-label="Hapus"
        cancel-label="Batal" destructive :loading="deleteProcessing" @confirm="destroyEmployeeAdvance" />
</template>
