<script setup lang="ts">
import { Pencil, 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 { SupplierListItem } from '@/types/supplier';
import { destroy } from '@/routes/admin/master/suppliers';

const props = defineProps<{
    supplier: SupplierListItem;
}>();

const emit = defineEmits<{
    edit: [supplier: SupplierListItem];
}>();

const { can } = useCan();

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

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

        <Tooltip v-if="can('suppliers.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('suppliers.delete')" v-model:open="deleteConfirmOpen" title="Hapus supplier?"
        :description="`Supplier ${supplier.name} akan dihapus secara permanen. Tindakan ini tidak dapat dibatalkan.`"
        confirm-label="Hapus" cancel-label="Batal" destructive :loading="deleteProcessing" @confirm="destroySupplier" />
</template>
