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

const props = defineProps({ fournisseur: Object })

const form = useForm({
    nom: props.fournisseur.nom, raison_sociale: props.fournisseur.raison_sociale ?? '',
    siret: props.fournisseur.siret ?? '', adresse: props.fournisseur.adresse ?? '',
    code_postal: props.fournisseur.code_postal ?? '', ville: props.fournisseur.ville ?? '',
    telephone: props.fournisseur.telephone ?? '', email: props.fournisseur.email ?? '',
    contact_commercial: props.fournisseur.contact_commercial ?? '',
    email_commercial: props.fournisseur.email_commercial ?? '',
    telephone_commercial: props.fournisseur.telephone_commercial ?? '',
    site_web: props.fournisseur.site_web ?? '', notes: props.fournisseur.notes ?? '',
    active: props.fournisseur.active,
})

function submit() { form.put(route('fournisseurs.update', props.fournisseur.id)) }
</script>

<template>
    <Head :title="`Modifier — ${fournisseur.nom}`" />
    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center gap-3">
                <Link :href="route('fournisseurs.show', fournisseur.id)" 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 {{ fournisseur.nom }}</h1>
            </div>
        </template>

        <form @submit.prevent="submit" class="max-w-3xl space-y-6">
            <div class="rounded-xl bg-white p-6 shadow-sm border border-gray-100">
                <h2 class="mb-4 text-sm font-semibold uppercase tracking-wide text-gray-500">Informations</h2>
                <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">Nom <span class="text-red-500">*</span></label>
                        <input v-model="form.nom" 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">Raison sociale</label><input v-model="form.raison_sociale" 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">SIRET</label><input v-model="form.siret" type="text" maxlength="14" 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">Adresse</label><textarea v-model="form.adresse" 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><label class="block text-sm font-medium text-gray-700">Code postal</label><input v-model="form.code_postal" 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">Ville</label><input v-model="form.ville" 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">Téléphone</label><input v-model="form.telephone" 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">Email</label><input v-model="form.email" type="email" 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">Site web</label><input v-model="form.site_web" type="url" 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">Contact commercial</label><input v-model="form.contact_commercial" 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">Email commercial</label><input v-model="form.email_commercial" type="email" 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">Tél. commercial</label><input v-model="form.telephone_commercial" 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">Notes</label><textarea v-model="form.notes" rows="3" 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">Fournisseur actif</label>
                    </div>
                </div>
            </div>
            <div class="flex justify-end gap-3">
                <Link :href="route('fournisseurs.show', fournisseur.id)" 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 ? 'Enregistrement...' : 'Enregistrer' }}</button>
            </div>
        </form>
    </AuthenticatedLayout>
</template>
