<script setup>
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout.vue'
import { Head, Link, useForm } from '@inertiajs/vue3'

const props = defineProps({ categories: Array, fournisseurs: Array })
const form = useForm({ reference: '', designation: '', description: '', categorie_id: '', fournisseur_id: '', prix_unitaire_ht: '', unite: 'unité' })
function submit() { form.post(route('articles.store')) }
</script>

<template>
    <Head title="Nouvel article" />
    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center gap-3">
                <Link :href="route('articles.index')" class="text-gray-400 hover:text-gray-600"><svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" /></svg></Link>
                <h1 class="text-xl font-semibold text-gray-900">Nouvel article</h1>
            </div>
        </template>
        <form @submit.prevent="submit" class="max-w-2xl space-y-6">
            <div class="rounded-xl bg-white p-6 shadow-sm border border-gray-100">
                <div class="grid gap-4 sm:grid-cols-2">
                    <div class="sm:col-span-2"><label class="block text-sm font-medium text-gray-700">Désignation *</label><input v-model="form.designation" type="text" required class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" /></div>
                    <div><label class="block text-sm font-medium text-gray-700">Référence</label><input v-model="form.reference" type="text" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" /></div>
                    <div><label class="block text-sm font-medium text-gray-700">Catégorie</label><select v-model="form.categorie_id" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"><option value="">—</option><option v-for="c in categories" :key="c.id" :value="c.id">{{ c.nom }}</option></select></div>
                    <div><label class="block text-sm font-medium text-gray-700">Fournisseur</label><select v-model="form.fournisseur_id" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none"><option value="">—</option><option v-for="f in fournisseurs" :key="f.id" :value="f.id">{{ f.nom }}</option></select></div>
                    <div><label class="block text-sm font-medium text-gray-700">Prix HT</label><input v-model="form.prix_unitaire_ht" type="number" min="0" step="0.01" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" /></div>
                    <div><label class="block text-sm font-medium text-gray-700">Unité</label><input v-model="form.unite" type="text" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" /></div>
                    <div class="sm:col-span-2"><label class="block text-sm font-medium text-gray-700">Description</label><textarea v-model="form.description" rows="2" class="mt-1 block w-full rounded-lg border border-gray-300 px-3 py-2 text-sm focus:border-blue-500 focus:outline-none" /></div>
                </div>
            </div>
            <div class="flex justify-end gap-3">
                <Link :href="route('articles.index')" class="rounded-lg border border-gray-300 px-5 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 transition-colors">Annuler</Link>
                <button type="submit" :disabled="form.processing" class="rounded-lg bg-blue-600 px-5 py-2 text-sm font-medium text-white hover:bg-blue-700 disabled:opacity-50 transition-colors">{{ form.processing ? '...' : 'Créer' }}</button>
            </div>
        </form>
    </AuthenticatedLayout>
</template>
