<script setup lang="ts">
import { Trash2 } from '@lucide/vue';
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 { AttendanceListItem } from '@/types/attendance';
import { destroy } from '@/routes/admin/hr/attendances';

const props = defineProps<{
    attendance: AttendanceListItem;
}>();

const { can } = useCan();

const { open: deleteConfirmOpen, processing: deleteProcessing, destroy: destroyAttendance } = useDestroy({
    url: destroy.url(props.attendance.id),
    errorMessage: 'Gagal menghapus data presensi.',
});
</script>

<template>
    <div class="flex items-center justify-end gap-1">
        <Tooltip v-if="can('attendances.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('attendances.delete')"
        v-model:open="deleteConfirmOpen"
        title="Hapus data presensi?"
        :description="`Presensi tanggal ${attendance.attendance_date_formatted} akan dihapus secara permanen.`"
        confirm-label="Hapus"
        cancel-label="Batal"
        destructive
        :loading="deleteProcessing"
        @confirm="destroyAttendance"
    />
</template>
