<script setup lang="ts">
import { Head, Link } from '@inertiajs/vue3';
import { ArrowLeft } from '@lucide/vue';
import { computed } from 'vue';
import CuttingPosForm from '@/components/admin/manage/cuttings/CuttingPosForm.vue';
import { Button } from '@/components/ui/button';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type {
    CuttingEditItem,
    CuttingProductCatalogItem,
    CuttingRawMaterialCatalogItem,
} from '@/types/cutting';

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

function usesLengthUnit(unit?: string): boolean {
    return unit === 'yard' || unit === 'meter';
}

const initialData = computed(() => ({
    description: props.cutting.description ?? '',
    materials: props.cutting.materials.map((item) => {
        const unit = item.raw_material_price?.raw_material?.unit ?? 'kilogram';
        const usesCm = usesLengthUnit(unit);

        return {
            raw_material_price_id: item.raw_material_price_id,
            raw_material_name: item.raw_material_price?.raw_material?.name ?? '',
            variant: item.raw_material_price?.variant ?? '',
            unit,
            uses_length_unit: usesCm,
            unit_abbreviation: item.raw_material_price?.raw_material?.unit_abbreviation ?? '',
            stock_input: item.raw_material_price?.stock_input ?? '',
            material_usage: usesCm
                ? (item.material_usage_cm_input ?? item.material_usage_input)
                : item.material_usage_input,
            remaining_material: usesCm
                ? (item.remaining_material_cm_input ?? item.remaining_material_input)
                : item.remaining_material_input,
            images: item.raw_material_price?.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),
        warehouse_stock: String(item.warehouse_stock),
        cutting_reject: String(item.cutting_reject),
        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>

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

        <CuttingPosForm
            :raw-material-catalog="rawMaterialCatalog"
            :product-catalog="productCatalog"
            :initial-data="initialData"
            :submit-url="`/admin/manage/cuttings/${cutting.id}`"
            method="put"
            submit-label="Perbarui"
        />
    </AdminLayout>
</template>
