<script setup lang="ts">
import { Head, Link } from '@inertiajs/vue3';
import { ArrowLeft } from '@lucide/vue';
import { computed } from 'vue';
import PurchasePosForm from '@/components/admin/manage/purchases/PurchasePosForm.vue';
import { Button } from '@/components/ui/button';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { PurchaseCatalogItem, PurchaseEditItem, SelectOption } from '@/types/purchase';

const props = defineProps<{
    purchase: PurchaseEditItem;
    suppliers: SelectOption[];
    catalog: PurchaseCatalogItem[];
}>();

const initialData = computed(() => ({
    supplier_id: String(props.purchase.supplier_id),
    discount: String(props.purchase.discount),
    notes: props.purchase.notes ?? '',
    items: props.purchase.items.map((item) => ({
        raw_material_price_id: item.raw_material_price_id,
        raw_material_name: item.raw_material_name,
        variant: item.variant_name,
        unit_abbreviation: item.unit_abbreviation,
        quantity: item.quantity_input,
        unit_price: item.unit_price,
        thumb_url: item.raw_material_price?.images?.[0]?.thumb_url ?? null,
    })),
    photos: props.purchase.photos ?? null,
}));
</script>

<template>
    <Head title="Ubah Belanja" />

    <AdminLayout title="Ubah Belanja">
        <div class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
            <div class="space-y-1">
                <h2 class="text-2xl font-bold tracking-tight">
                    Ubah Belanja
                </h2>
            </div>

            <Button variant="outline" as-child class="shrink-0 self-start sm:self-center">
                <Link href="/admin/manage/purchases">
                    <ArrowLeft class="size-4" />
                    Kembali
                </Link>
            </Button>
        </div>

        <PurchasePosForm
            :suppliers="suppliers"
            :catalog="catalog"
            :initial-data="initialData"
            :submit-url="`/admin/manage/purchases/${purchase.id}`"
            method="put"
            submit-label="Perbarui"
        />
    </AdminLayout>
</template>
