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

const props = defineProps({
    usuario: Object,
});

const form = useForm({
    name: props.usuario.name,
    email: props.usuario.email,
    password: '',
    password_confirmation: '',
    rol: props.usuario.rol,
    activo: props.usuario.activo === 1 || props.usuario.activo === true,
});

const submit = () => {
    console.log('Submitting form with id:', props.usuario.id);
    form.put(route('usuarios.update', props.usuario.id), {
        onError: (errors) => {
            console.log('Validation errors:', errors);
        },
        onSuccess: () => {
            console.log('Form submitted successfully');
        }
    });
};
</script>

<template>
    <Head title="Editar Usuario" />

    <AppLayout title="Actualizar Usuario">
        <!-- Main Design Overhaul - FINAL FIX -->
        <main class="flex-1 flex justify-center py-10 px-4 sm:px-6 bg-[#f8f9fc] font-display">
            <div class="w-full max-w-2xl">
                <!-- Header Card -->
                <div class="mb-6 flex items-center gap-4">
                    <div class="flex h-12 w-12 items-center justify-center rounded-xl bg-[#0066FF] text-white shadow-lg shadow-[#0066FF]/20">
                        <!-- SVG instead of Material symbols for reliability -->
                        <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                        </svg>
                    </div>
                    <div>
                        <h1 class="text-xl font-bold text-slate-900 tracking-tight">Actualizar Usuario</h1>
                        <p class="text-sm text-slate-500">Gestionar acceso y perfil del sistema</p>
                    </div>
                </div>

                <!-- Main Form Card -->
                <div class="bg-white rounded-2xl shadow-sm border border-slate-200 overflow-hidden">
                    <div class="p-6 border-b border-slate-100 bg-[#f8f9fc]/50 flex items-center justify-between">
                        <div>
                            <h2 class="text-lg font-bold text-slate-900">Información del Perfil</h2>
                            <p class="text-sm text-slate-500">Actualice los datos personales y los permisos de acceso.</p>
                        </div>
                    </div>

                    <form @submit.prevent="submit" class="p-8 space-y-8">
                        <!-- Personal Info Grid -->
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                            <div class="flex flex-col gap-2">
                                <label class="text-sm font-semibold text-slate-700">Nombre Completo</label>
                                <input 
                                    v-model="form.name"
                                    class="h-11 px-4 rounded-lg border-slate-200 bg-white text-slate-900 focus:border-[#0066FF] focus:ring-[#0066FF]/20 transition-all shadow-sm" 
                                    placeholder="Ej. Juan Pérez" 
                                    type="text"
                                />
                                <InputError :message="form.errors.name" />
                            </div>

                            <div class="flex flex-col gap-2">
                                <label class="text-sm font-semibold text-slate-700">Correo Electrónico</label>
                                <input 
                                    v-model="form.email"
                                    class="h-11 px-4 rounded-lg border-slate-200 bg-white text-slate-900 focus:border-[#0066FF] focus:ring-[#0066FF]/20 transition-all shadow-sm" 
                                    placeholder="usuario@empresa.com" 
                                    type="email"
                                />
                                <InputError :message="form.errors.email" />
                            </div>

                            <div class="flex flex-col gap-2">
                                <label class="text-sm font-semibold text-slate-700">Rol del Usuario</label>
                                <select 
                                    v-model="form.rol"
                                    class="h-11 px-4 rounded-lg border-slate-200 bg-white text-slate-900 focus:border-[#0066FF] focus:ring-[#0066FF]/20 transition-all shadow-sm"
                                >
                                    <option value="admin">Administrador</option>
                                    <option value="ejecutivo">Ejecutivo</option>
                                </select>
                                <InputError :message="form.errors.rol" />
                            </div>

                            <div class="flex items-center gap-3 pt-4">
                                <label class="flex items-center gap-3 cursor-pointer group">
                                    <div class="relative inline-flex items-center">
                                        <input type="checkbox" v-model="form.activo" class="sr-only peer">
                                        <div class="w-11 h-6 bg-slate-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-[#0066FF] transition-colors"></div>
                                    </div>
                                    <span class="text-sm font-semibold text-slate-700">Usuario Activo</span>
                                </label>
                            </div>
                        </div>

                        <!-- Password Section -->
                        <div class="pt-8 border-t border-slate-100">
                            <div class="mb-6">
                                <h3 class="text-base font-bold text-slate-900">Cambiar Contraseña <span class="text-slate-400 font-normal text-sm ml-1">(opcional)</span></h3>
                                <p class="text-xs text-slate-500 mt-1">Si no desea cambiar la contraseña, deje estos campos en blanco.</p>
                            </div>

                            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                                <div class="flex flex-col gap-2">
                                    <label class="text-sm font-semibold text-slate-700">Nueva Contraseña</label>
                                    <div class="relative">
                                        <input 
                                            v-model="form.password"
                                            class="w-full h-11 px-4 rounded-lg border-slate-200 bg-white text-slate-900 focus:border-[#0066FF] focus:ring-[#0066FF]/20 transition-all shadow-sm pr-10" 
                                            placeholder="••••••••" 
                                            type="password"
                                        />
                                        <div class="absolute right-3 top-1/2 -translate-y-1/2 text-slate-400 cursor-pointer">
                                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
                                            </svg>
                                        </div>
                                    </div>
                                    <InputError :message="form.errors.password" />
                                </div>

                                <div class="flex flex-col gap-2">
                                    <label class="text-sm font-semibold text-slate-700">Confirmar Contraseña</label>
                                    <div class="relative">
                                        <input 
                                            v-model="form.password_confirmation"
                                            class="w-full h-11 px-4 rounded-lg border-slate-200 bg-white text-slate-900 focus:border-[#0066FF] focus:ring-[#0066FF]/20 transition-all shadow-sm" 
                                            placeholder="••••••••" 
                                            type="password"
                                        />
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Action Buttons -->
                        <div class="flex items-center justify-end gap-3 pt-8 border-t border-slate-100">
                            <Link 
                                :href="route('usuarios.index')"
                                class="px-6 py-2.5 rounded-lg border border-slate-200 text-slate-600 font-semibold text-sm hover:bg-slate-50 transition-colors"
                            >
                                Cancelar
                            </Link>
                            <button 
                                type="submit"
                                :disabled="form.processing"
                                class="px-6 py-2.5 rounded-lg bg-[#0066FF] text-white font-semibold text-sm hover:bg-blue-700 transition-colors shadow-sm shadow-[#0066FF]/20 disabled:opacity-50"
                            >
                                {{ form.processing ? 'Actualizando...' : 'Actualizar Usuario' }}
                            </button>
                        </div>
                    </form>
                </div>

                <!-- Footer Metadata -->
                <div class="mt-6 flex justify-between items-center px-2">
                    <p class="text-xs text-slate-400 italic">ID Usuario: #{{ usuario.id }}</p>
                    <div class="flex gap-4">
                        <Link :href="route('usuarios.index')" class="text-xs text-[#0066FF] hover:underline font-medium">Ver Historial de Cambios</Link>
                    </div>
                </div>
            </div>
        </main>
    </AppLayout>
</template>
