<script setup lang="ts">
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import { index, update } from '@/routes/admin/manage/restocks';
import type {
    RestockCartItem,
    RestockEditItem,
    RestockProductCatalogItem,
} from '@/types/restock';
import RestockPosForm from './form/RestockPosForm.vue';

const props = defineProps<{
    restock: RestockEditItem;
    productCatalog: RestockProductCatalogItem[];
}>();

const initialData = computed(() => ({
    notes: props.restock.notes ?? '',
    stock_type: props.restock.stock_type ?? 'good',
    photos: props.restock.photos ? [props.restock.photos] : [],
    items: props.restock.items.map((item) => ({
        product_variant_id: item.product_variant_id,
        product_name: item.product_name,
        variant_name: item.variant_name,
        stock: item.stock ?? 0,
        quantity: item.quantity_input,
        unit_price: item.unit_price,
        images: item.product_variant?.images ?? [],
    })),
}));
</script>

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

    <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 Restock
                </h2>
            </div>

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

        <RestockPosForm
            :product-catalog="productCatalog"
            :initial-data="initialData"
            :submit-url="update.url(restock.id)"
            method="put"
            submit-label="Perbarui"
        />
    </AdminLayout>
</template>
