<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 { PurchaseListItem } from '@/types/purchase';
import { edit, destroy } from '@/routes/admin/manage/purchases';

const props = defineProps<{
    purchase: PurchaseListItem;
}>();

const { can } = useCan();

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

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

        <Tooltip v-if="can('purchases.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('purchases.delete')"
        v-model:open="deleteConfirmOpen"
        title="Hapus belanja?"
        :description="`Belanja ${purchase.total_formatted} dari ${purchase.supplier_name} akan dihapus. Stok bahan baku akan dikurangi.`"
        confirm-label="Hapus"
        cancel-label="Batal"
        destructive
        :loading="deleteProcessing"
        @confirm="destroyPurchase"
    />
</template>
