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

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

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

function onCustomerCreated(customer: { id: number; name: string }) {
    customers.value.push({ value: customer.id, label: customer.name });
}
</script>

<template>
    <Head title="Tambah 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">
                    Tambah Pesanan
                </h2>
            </div>

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

        <OrderPosForm
            :customers="customers"
            :marketings="marketings"
            :catalog="catalog"
            :channels="channels"
            :store-price-types="storePriceTypes"
            :payment-types="paymentTypes"
            :draft-items="draftItems"
            :submit-url="store.url()"
            method="post"
            submit-label="Simpan"
            @customer-created="onCustomerCreated"
        />
    </AdminLayout>
</template>
