<script setup lang="ts">
import AdminLayout from '@/Layouts/AdminLayout.vue';
import admin from '@/routes/admin';
import { Head, useForm } from '@inertiajs/vue3';
import { ArrowLeft } from 'lucide-vue-next';
import { computed } from 'vue';

const props = defineProps<{
    products: Array<{
        id: number;
        name: string;
        selling_price: number;
    }>;
}>();

const form = useForm({
    product_id: '',
    quantity: '',
});

const selectedProduct = computed(() => {
    return props.products.find(p => p.id == form.product_id);
});

const total = computed(() => {
    if (selectedProduct.value && form.quantity) {
        return (selectedProduct.value.selling_price * parseInt(form.quantity)).toFixed(2);
    }
    return '0.00';
});

const submit = () => {
    form.post(admin.sales.store.url());
};
</script>

<template>
    <AdminLayout>
        <Head title="Record Sale" />

        <div class="animate-fade-in space-y-8 pb-12">
            <div class="flex items-center gap-4">
                <Link
                    :href="admin.sales.index.url()"
                    class="rounded-xl border border-slate-200 bg-white p-3 shadow-sm transition-colors hover:bg-slate-50 dark:border-slate-700 dark:bg-slate-800 dark:hover:bg-slate-700"
                >
                    <ArrowLeft :size="20" />
                </Link>
                <div>
                    <h1 class="text-3xl font-black tracking-tight text-slate-900 dark:text-white">Record Sale</h1>
                    <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">Record a new sale transaction.</p>
                </div>
            </div>

            <form @submit.prevent="submit" class="rounded-4xl border border-slate-200/60 bg-white p-8 shadow-sm dark:border-slate-700 dark:bg-slate-800">
                <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Product</label>
                        <select
                            v-model="form.product_id"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        >
                            <option value="">Select Product</option>
                            <option v-for="product in products" :key="product.id" :value="product.id">
                                {{ product.name }} (${{ product.selling_price }})
                            </option>
                        </select>
                        <div v-if="form.errors.product_id" class="mt-1 text-sm text-red-600">{{ form.errors.product_id }}</div>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Quantity</label>
                        <input
                            v-model="form.quantity"
                            type="number"
                            min="1"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        />
                        <div v-if="form.errors.quantity" class="mt-1 text-sm text-red-600">{{ form.errors.quantity }}</div>
                    </div>
                    <div class="md:col-span-2">
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Total</label>
                        <input
                            :value="`$${total}`"
                            type="text"
                            readonly
                            class="mt-1 block w-full rounded-lg border border-slate-300 bg-slate-50 px-3 py-2 shadow-sm dark:border-slate-600 dark:bg-slate-600 dark:text-white"
                        />
                    </div>
                </div>
                <div class="mt-6 flex justify-end">
                    <button
                        type="submit"
                        :disabled="form.processing"
                        class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white transition-colors hover:bg-brand-700 disabled:opacity-50"
                    >
                        {{ form.processing ? 'Recording...' : 'Record Sale' }}
                    </button>
                </div>
            </form>
        </div>
    </AdminLayout>
</template>