<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { EnumOption, RawMaterialListItem } from '@/types/raw-material';
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import RawMaterialForm from './form/RawMaterialForm.vue';
import { index, update } from '@/routes/admin/master/raw_materials';

const props = defineProps<{
    rawMaterial: RawMaterialListItem;
    units: EnumOption[];
}>();

const initialData = computed(() => ({
    name: props.rawMaterial.name ?? '',
    unit: props.rawMaterial.unit ?? '',
    prices: (props.rawMaterial.prices ?? []).map((price) => ({
        id: price.id,
        variant: price.variant,
        price: price.price_input,
        stock: price.stock_input,
        images: price.images ?? [],
    })),
}));
</script>

<template>
    <Head title="Ubah Bahan Baku" />

    <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 Bahan Baku
                </h2>
            </div>

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

        <RawMaterialForm
            :submit-url="update.url(props.rawMaterial.id)"
            method="put"
            submit-label="Perbarui"
            :initial-data="initialData"
            :units="units"
        />
    </AdminLayout>
</template>
