<script setup lang="ts">
import { Link } from '@inertiajs/vue3';
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 { RawMaterialListItem } from '@/types/raw-material';
import { edit, destroy } from '@/routes/admin/master/raw_materials';

const props = defineProps<{
    material: RawMaterialListItem;
}>();

const { can } = useCan();

const { open: deleteConfirmOpen, processing: deleteProcessing, destroy: destroyRawMaterial } = useDestroy({
    url: destroy.url(props.material.id),
    preserveScroll: false,
    errorMessage: 'Gagal menghapus bahan baku.',
});
</script>

<template>
    <div class="flex items-center justify-end gap-1">
        <Tooltip v-if="can('raw_materials.update')">
            <TooltipTrigger as-child>
                <Button variant="ghost" size="icon" class="size-8" as-child>
                    <Link :href="edit.url(props.material.id)">
                        <Pencil class="size-4" />
                        <span class="sr-only">Ubah</span>
                    </Link>
                </Button>
            </TooltipTrigger>
            <TooltipContent>Ubah</TooltipContent>
        </Tooltip>

        <Tooltip v-if="can('raw_materials.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('raw_materials.delete')" v-model:open="deleteConfirmOpen" title="Hapus bahan baku?"
        :description="`Bahan baku ${material.name} akan dihapus beserta seluruh variannya. Tindakan ini tidak dapat dibatalkan.`"
        confirm-label="Hapus" cancel-label="Batal" destructive :loading="deleteProcessing"
        @confirm="destroyRawMaterial" />
</template>
