<script setup lang="ts">
import { Link, router } from '@inertiajs/vue3';
import { KeyRound, Pencil, Trash2 } from '@lucide/vue';
import { 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 { EmployeeListItem } from '@/types/employee';

const props = defineProps<{
    employee: EmployeeListItem;
}>();

const { can } = useCan();

const deleteConfirmOpen = ref(false);
const resetPasswordConfirmOpen = ref(false);
const deleteProcessing = ref(false);
const resetPasswordProcessing = ref(false);

function destroyEmployee() {
    deleteProcessing.value = true;

    router.delete(`/admin/hr/employees/${props.employee.id}`, {
        onSuccess: () => {
            deleteConfirmOpen.value = false;
        },
        onError: () => {
            toast.error('Gagal menghapus pegawai.');
        },
        onFinish: () => {
            deleteProcessing.value = false;
        },
    });
}

function resetPassword() {
    resetPasswordProcessing.value = true;

    router.post(`/admin/hr/employees/${props.employee.id}/reset-password`, {}, {
        preserveScroll: true,
        onSuccess: () => {
            resetPasswordConfirmOpen.value = false;
        },
        onError: () => {
            toast.error('Gagal mereset kata sandi.');
        },
        onFinish: () => {
            resetPasswordProcessing.value = false;
        },
    });
}
</script>

<template>
    <div class="flex items-center justify-end gap-1">
        <Tooltip v-if="can('employees.update')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8" as-child>
                    <Link :href="`/admin/hr/employees/${employee.id}/edit`">
                        <Pencil class="size-4" />
                        <span class="sr-only">Ubah</span>
                    </Link>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Ubah</TooltipContent>
        </Tooltip>

        <Tooltip v-if="can('employees.reset-password')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8" @click="resetPasswordConfirmOpen = true">
                    <KeyRound class="size-4" />
                    <span class="sr-only">Reset Kata Sandi</span>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Reset Kata Sandi</TooltipContent>
        </Tooltip>

        <Tooltip v-if="can('employees.delete')">
            <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('employees.reset-password')"
        v-model:open="resetPasswordConfirmOpen"
        title="Reset kata sandi pegawai?"
        :description="`Kata sandi ${employee.profile?.full_name ?? 'pegawai'} akan direset ke kata sandi default sistem. Pengguna akan otomatis logout dari semua sesi aktif.`"
        confirm-label="Reset Kata Sandi" cancel-label="Batal" :loading="resetPasswordProcessing"
        @confirm="resetPassword" />

    <ConfirmDialog
        v-if="can('employees.delete')"
        v-model:open="deleteConfirmOpen"
        title="Hapus pegawai?"
        :description="`Data pegawai ${employee.profile?.full_name ?? ''} akan dihapus secara permanen beserta akun pengguna terkait. Tindakan ini tidak dapat dibatalkan.`"
        confirm-label="Hapus" cancel-label="Batal" destructive :loading="deleteProcessing" @confirm="destroyEmployee" />
</template>
