<script setup lang="ts">
import { Head, Link } from '@inertiajs/vue3';
import { ArrowLeft } from '@lucide/vue';
import { computed } from 'vue';
import { Button } from '@/components/ui/button';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { EnumOption, RawMaterialListItem } from '@/types/raw-material';
import RawMaterialForm from './form/RawMaterialForm.vue';

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>

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

        <RawMaterialForm :submit-url="`/admin/master/raw-materials/${rawMaterial.id}`" method="put"
            submit-label="Perbarui" :initial-data="initialData" :units="units" />
    </AdminLayout>
</template>
