<script setup lang="ts">
import BackButton from '@/components/button/BackButton.vue';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type {
    EnumOption,
    OrderCatalogItem,
    OrderEditItem,
    SelectOption,
} from '@/types/order';
import { Head } from '@inertiajs/vue3';
import { computed, ref } from 'vue';
import OrderPosForm from './form/OrderPosForm.vue';
import { index, update } from '@/routes/admin/manage/orders';

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

const customers = ref([...props.customers]);

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,
    payment_type: props.order.payment_type,
    is_affiliate: props.order.is_affiliate,
    tiktok_order_id: props.order.tiktok_order_id ?? '',
    shopee_order_id: props.order.shopee_order_id ?? '',
    discount: String(props.order.discount),
    nego_price: props.order.nego_price != null ? String(props.order.nego_price) : '',
    notes: props.order.notes ?? '',
    status: props.order.status,
    photos: props.order.photos ?? null,
    items: props.order.items.map((item) => ({
        product_variant_id: item.product_variant_id,
        product_name: item.product_name,
        variant_name: item.variant_name,
        stock_quality: item.stock_quality ?? 'good',
        stock_quality_label:
            item.stock_quality === 'reject' ? 'Reject' : 'Bagus',
        quantity: item.quantity_input,
        unit_price: item.unit_price,
        images: item.product_variant?.images ?? [],
    })),
}));

function onCustomerCreated(customer: { id: number; name: string }) {
    customers.value.push({ value: customer.id, label: customer.name });
}
</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-sm text-muted-foreground">
                    {{ order.order_number }}
                </p>
            </div>

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

        <OrderPosForm
            :customers="customers"
            :marketings="marketings"
            :catalog="catalog"
            :channels="channels"
            :store-price-types="storePriceTypes"
            :payment-types="paymentTypes"
            :initial-data="initialData"
            :submit-url="update.url(props.order.id)"
            method="put"
            submit-label="Perbarui"
            @customer-created="onCustomerCreated"
        />
    </AdminLayout>
</template>
