<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type {
    CuttingEditItem,
    CuttingProductCatalogItem,
    CuttingRawMaterialCatalogItem,
} from '@/types/cutting';
import { Head } from '@inertiajs/vue3';
import { computed } from 'vue';
import CuttingPosForm from './form/CuttingPosForm.vue';
import { index, update } from '@/routes/admin/manage/cuttings';

const props = defineProps<{
    cutting: CuttingEditItem;
    rawMaterialCatalog: CuttingRawMaterialCatalogItem[];
    productCatalog: CuttingProductCatalogItem[];
}>();

const initialData = computed(() => ({
    description: props.cutting.description ?? '',
    sewing_cost: String(props.cutting.sewing_cost ?? 0),
    other_cost: String(props.cutting.other_cost ?? 0),
    materials: props.cutting.materials.map((item) => ({
        raw_material_price_id: item.raw_material_price_id,
        raw_material_name: item.raw_material_name ?? '',
        variant: item.variant ?? '',
        unit: item.unit ?? 'kilogram',
        unit_abbreviation: item.unit_abbreviation ?? '',
        stock_input: item.stock_input ?? '',
        material_usage: item.material_usage_input,
        images: item.images ?? [],
    })),
    results: props.cutting.results.map((item) => ({
        product_variant_id: item.product_variant_id,
        product_name: item.product_variant?.product?.name ?? '',
        variant_name: item.product_variant?.name ?? '',
        stock: item.product_variant?.stock ?? 0,
        cutting_result: String(item.cutting_result),
        sample: String(item.sample),
        original_outside_sample: String(item.original_outside_sample),
        images: item.product_variant?.images ?? [],
    })),
}));
</script>

<template>

    <Head title="Ubah Cutting" />

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

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

        <CuttingPosForm :raw-material-catalog="rawMaterialCatalog" :product-catalog="productCatalog"
            :initial-data="initialData" :submit-url="update.url(props.cutting.id)" method="put"
            submit-label="Perbarui" />
    </AdminLayout>
</template>
