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

const props = defineProps({ article: Object, categories: Array, fournisseurs: Array })
const form = useForm({
    reference: props.article.reference ?? '', designation: props.article.designation,
    description: props.article.description ?? '', categorie_id: props.article.categorie_id ?? '',
    fournisseur_id: props.article.fournisseur_id ?? '', prix_unitaire_ht: props.article.prix_unitaire_ht ?? '',
    unite: props.article.unite, active: props.article.active,
})
function submit() { form.put(route('articles.update', props.article.id)) }
</script>

<template>
    <Head :title="`Modifier — ${article.designation}`" />
    <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">Modifier {{ article.designation }}</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 class="flex items-center gap-2"><input v-model="form.active" type="checkbox" id="active" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500" /><label for="active" class="text-sm font-medium text-gray-700">Article actif</label></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 ? '...' : 'Enregistrer' }}</button>
            </div>
        </form>
    </AuthenticatedLayout>
</template>
