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

const props = defineProps({ user: Object, services: Array, roles: Array })
const form = useForm({
    name: props.user.name, email: props.user.email,
    service_id: props.user.service_id ?? '', telephone: props.user.telephone ?? '',
    role: props.user.roles?.[0]?.name ?? 'lecteur',
    password: '', password_confirmation: '',
})
function submit() { form.put(route('users.update', props.user.id)) }
const roleLabels = { admin: 'Administrateur', responsable: 'Responsable', acheteur: 'Acheteur', lecteur: 'Lecteur' }
</script>

<template>
    <Head :title="`Modifier — ${user.name}`" />
    <AuthenticatedLayout>
        <template #header>
            <div class="flex items-center gap-3">
                <Link :href="route('users.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 {{ user.name }}</h1>
            </div>
        </template>
        <form @submit.prevent="submit" class="max-w-xl 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">Nom *</label><input v-model="form.name" 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 class="sm:col-span-2"><label class="block text-sm font-medium text-gray-700">Email *</label><input v-model="form.email" type="email" 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">Service</label><select v-model="form.service_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="s in services" :key="s.id" :value="s.id">{{ s.nom }}</option></select></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 class="sm:col-span-2"><label class="block text-sm font-medium text-gray-700">Rôle *</label>
                        <select v-model="form.role" 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">
                            <option v-for="r in roles" :key="r.name" :value="r.name">{{ roleLabels[r.name] ?? r.name }}</option>
                        </select>
                    </div>
                    <div><label class="block text-sm font-medium text-gray-700">Nouveau mot de passe</label><input v-model="form.password" type="password" 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">Confirmation</label><input v-model="form.password_confirmation" type="password" 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('users.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>
