<script setup lang="ts">
import { Head, Link } from '@inertiajs/vue3';
import { ArrowLeft } from '@lucide/vue';
import { computed } from 'vue';
import OrderPosForm from '@/components/admin/manage/orders/OrderPosForm.vue';
import { Button } from '@/components/ui/button';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { EnumOption, OrderCatalogItem, OrderEditItem, SelectOption } from '@/types/order';

const props = defineProps<{
    order: OrderEditItem;
    customers: SelectOption[];
    marketings: SelectOption[];
    catalog: OrderCatalogItem[];
    channels: EnumOption[];
    storePriceTypes: EnumOption[];
}>();

const initialData = computed(() => ({
    customer_id: props.order.customer_id ? String(props.order.customer_id) : '',
    marketing_id: props.order.marketing_id ? String(props.order.marketing_id) : '',
    channel: props.order.channel,
    price_type: props.order.price_type,
    tiktok_order_id: props.order.tiktok_order_id ?? '',
    shopee_order_id: props.order.shopee_order_id ?? '',
    discount: String(props.order.discount),
    notes: props.order.notes ?? '',
    items: props.order.items.map((item) => ({
        product_variant_id: item.product_variant_id,
        product_name: item.product_name,
        variant_name: item.variant_name,
        quantity: item.quantity_input,
        unit_price: item.unit_price,
        images: item.product_variant?.images ?? [],
    })),
}));
</script>

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

    <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 Pesanan
                </h2>
                <p class="text-muted-foreground text-sm">
                    {{ order.order_number }}
                </p>
            </div>

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

        <OrderPosForm
            :customers="customers"
            :marketings="marketings"
            :catalog="catalog"
            :channels="channels"
            :store-price-types="storePriceTypes"
            :initial-data="initialData"
            :submit-url="`/admin/manage/orders/${order.id}`"
            method="put"
            submit-label="Perbarui"
        />
    </AdminLayout>
</template>
