<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { CategoryOption, ProductListItem } from '@/types/product';
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import ProductForm from './form/ProductForm.vue';
import { index, update } from '@/routes/admin/master/products';

const props = defineProps<{
    product: ProductListItem & { description?: string | null };
    categories: CategoryOption[];
}>();

const initialData = computed(() => ({
    name: props.product.name ?? '',
    description: props.product.description ?? '',
    category_ids:
        props.product.categories?.map((category) => category.id) ?? [],
    variants: (props.product.variants ?? []).map((variant) => ({
        id: variant.id,
        name: variant.name,
        stock: variant.stock,
        retail_stock: variant.retail_stock,
        images: variant.images ?? [],
    })),
}));
</script>

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

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

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

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