<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type {
    PurchaseCatalogItem,
    PurchaseEditItem,
    SelectOption,
} from '@/types/purchase';
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import PurchasePosForm from './form/PurchasePosForm.vue';
import { index, update } from '@/routes/admin/manage/purchases';

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

const initialData = computed(() => ({
    supplier_id: String(props.purchase.supplier_id),
    discount: String(props.purchase.discount),
    shipping_cost: String(props.purchase.shipping_cost ?? 0),
    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>
        <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>

            <BackButton :href="index.url()" />
        </div>

        <PurchasePosForm
            :suppliers="suppliers"
            :catalog="catalog"
            :initial-data="initialData"
            :submit-url="update.url(props.purchase.id)"
            method="put"
            submit-label="Perbarui"
        />
    </AdminLayout>
</template>
