<?php
// Ya no necesitas session_start() porque menu.php ya la tiene
include("../conexion.php");

// Obtener el ID del usuario
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if($id <= 0) {
    header("Location: menu.php?page=usuarios");
    exit();
}

$sql = "SELECT * FROM usuarios WHERE id = $id";
$resultado = mysqli_query($conexion, $sql);
$fila = mysqli_fetch_assoc($resultado);

if(!$fila){
    header("Location: menu.php?page=usuarios");
    exit();
}

// Obtener mensajes de error
$error = isset($_GET['error']) ? $_GET['error'] : '';
?>

<style>
    /* ===== ESTILOS MEJORADOS ===== */
    .form-card {
        background: white;
        border-radius: 20px;
        padding: 35px 40px;
        box-shadow: 0 10px 40px rgba(0,0,0,0.08);
        border: 1px solid rgba(0,0,0,0.03);
        transition: all 0.3s ease;
    }
    .form-card:hover {
        box-shadow: 0 15px 50px rgba(0,0,0,0.12);
    }
    
    .form-card .form-control {
        border-radius: 12px;
        border: 2px solid #e8ecf1;
        padding: 12px 16px;
        transition: all 0.3s ease;
        background: #fafbfc;
        font-size: 0.95rem;
    }
    .form-card .form-control:focus {
        border-color: #EC407A;
        background: white;
        box-shadow: 0 0 0 4px rgba(236, 64, 122, 0.12);
    }
    .form-card .form-control::placeholder {
        color: #b0b8c4;
        font-size: 0.9rem;
    }
    .form-card .form-control:disabled {
        background: #f1f3f5;
        cursor: not-allowed;
    }
    
    .form-card .form-label {
        font-weight: 600;
        color: #2d3748;
        font-size: 0.9rem;
        margin-bottom: 6px;
    }
    .form-card .form-label i {
        color: #EC407A;
        margin-right: 6px;
    }
    
    .form-card .input-icon-wrapper {
        position: relative;
    }
    .form-card .input-icon-wrapper i {
        position: absolute;
        left: 14px;
        top: 50%;
        transform: translateY(-50%);
        color: #a0aec0;
        font-size: 1rem;
        transition: all 0.3s ease;
        pointer-events: none;
    }
    .form-card .input-icon-wrapper .form-control {
        padding-left: 42px;
    }
    .form-card .input-icon-wrapper .form-control:focus + i {
        color: #EC407A;
    }
    
    .btn-primary {
        background: linear-gradient(135deg, #EC407A, #D81B60);
        border: none;
        padding: 12px 35px;
        border-radius: 12px;
        font-weight: 600;
        transition: all 0.3s ease;
        color: white;
        font-size: 0.95rem;
        letter-spacing: 0.3px;
        box-shadow: 0 4px 15px rgba(236, 64, 122, 0.3);
    }
    .btn-primary:hover {
        background: linear-gradient(135deg, #D81B60, #c2185b);
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(236, 64, 122, 0.4);
        color: white;
    }
    .btn-primary:active {
        transform: translateY(0);
    }
    
    .btn-secondary {
        border-radius: 12px;
        padding: 12px 30px;
        font-weight: 600;
        transition: all 0.3s ease;
        border: 2px solid #e2e8f0;
        background: white;
        color: #4a5568;
    }
    .btn-secondary:hover {
        background: #f7fafc;
        border-color: #cbd5e0;
        transform: translateY(-2px);
        color: #2d3748;
    }
    
    .form-card .info-box {
        background: linear-gradient(135deg, #fdf2f8, #fce4ec);
        border-radius: 14px;
        padding: 16px 20px;
        border-left: 4px solid #EC407A;
        margin-bottom: 25px;
        display: flex;
        align-items: center;
        gap: 12px;
        flex-wrap: wrap;
    }
    .form-card .info-box i {
        color: #EC407A;
        font-size: 1.3rem;
    }
    .form-card .info-box .info-text {
        color: #4a5568;
        font-size: 0.92rem;
        margin: 0;
    }
    .form-card .info-box .info-text strong {
        color: #2d3748;
    }
    .form-card .info-box .badge-id {
        background: rgba(236, 64, 122, 0.15);
        color: #D81B60;
        padding: 4px 14px;
        border-radius: 20px;
        font-weight: 600;
        font-size: 0.85rem;
        margin-left: auto;
    }
    .form-card .info-box .badge-id i {
        font-size: 0.8rem;
        margin-right: 4px;
    }
    
    .form-card .password-hint {
        font-size: 0.8rem;
        color: #a0aec0;
        margin-top: 5px;
        display: flex;
        align-items: center;
        gap: 5px;
    }
    .form-card .password-hint i {
        color: #a0aec0;
        font-size: 0.75rem;
    }
    
    .form-card .required-star {
        color: #EC407A;
        margin-left: 2px;
    }
    
    .form-card .divider {
        border: none;
        border-top: 1px solid #e8ecf1;
        margin: 25px 0 20px 0;
    }
    
    .form-card .btn-group-custom {
        display: flex;
        gap: 12px;
        flex-wrap: wrap;
    }
    
    .alert-modern {
        border-radius: 14px;
        padding: 16px 20px;
        border: none;
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 20px;
        animation: slideDown 0.4s ease;
    }
    .alert-modern.alert-danger {
        background: #fff5f5;
        border-left: 4px solid #e53e3e;
        color: #c53030;
    }
    .alert-modern.alert-danger i {
        color: #e53e3e;
        font-size: 1.2rem;
    }
    .alert-modern.alert-success {
        background: #f0fff4;
        border-left: 4px solid #38a169;
        color: #276749;
    }
    .alert-modern .btn-close {
        margin-left: auto;
    }

    @keyframes slideDown {
        from { opacity: 0; transform: translateY(-10px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .page-header-custom {
        margin-bottom: 28px;
    }
    .page-header-custom h2 {
        font-weight: 700;
        font-size: 1.7rem;
        color: #2d3748;
        display: flex;
        align-items: center;
        gap: 12px;
    }
    .page-header-custom h2 i {
        color: #EC407A;
        background: #fce4ec;
        padding: 10px;
        border-radius: 14px;
        font-size: 1.4rem;
    }
    .page-header-custom .btn-back {
        border-radius: 12px;
        padding: 10px 24px;
        font-weight: 500;
        transition: all 0.3s ease;
        background: #f7fafc;
        border: 2px solid #e2e8f0;
        color: #4a5568;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    .page-header-custom .btn-back:hover {
        background: #edf2f7;
        transform: translateY(-2px);
        color: #2d3748;
    }

    /* Responsive */
    @media (max-width: 768px) {
        .form-card {
            padding: 24px 20px;
        }
        .page-header-custom h2 {
            font-size: 1.3rem;
        }
        .page-header-custom .btn-back {
            padding: 8px 16px;
            font-size: 0.85rem;
        }
        .btn-primary, .btn-secondary {
            width: 100%;
            justify-content: center;
        }
        .form-card .btn-group-custom {
            flex-direction: column;
        }
        .form-card .info-box {
            flex-direction: column;
            text-align: center;
        }
        .form-card .info-box .badge-id {
            margin-left: 0;
        }
    }
</style>

<!-- ===== HEADER ===== -->
<div class="page-header-custom d-flex justify-content-between align-items-center flex-wrap gap-3">
    <h2>
        <i class="fas fa-user-edit"></i>
        Editar Usuario
    </h2>
    <a href="menu.php?page=usuarios" class="btn-back">
        <i class="fas fa-arrow-left"></i> Volver
    </a>
</div>

<!-- ===== MENSAJES DE ERROR ===== -->
<?php if($error == 'email_existente'): ?>
<div class="alert-modern alert-danger">
    <i class="fas fa-exclamation-circle"></i>
    <span>El correo electrónico <strong>ya está en uso</strong> por otro usuario.</span>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($error == 'campos_vacios'): ?>
<div class="alert-modern alert-danger">
    <i class="fas fa-exclamation-triangle"></i>
    <span>Por favor, <strong>completa todos los campos</strong> del formulario.</span>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($error == 'email_invalido'): ?>
<div class="alert-modern alert-danger">
    <i class="fas fa-envelope"></i>
    <span>El <strong>correo electrónico</strong> no es válido.</span>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($error == 'usuario_no_encontrado'): ?>
<div class="alert-modern alert-danger">
    <i class="fas fa-user-slash"></i>
    <span>El <strong>usuario</strong> no existe.</span>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<?php if($error == 'error_actualizacion'): ?>
<div class="alert-modern alert-danger">
    <i class="fas fa-exclamation-triangle"></i>
    <span><strong>Error</strong> al actualizar el usuario. Intenta nuevamente.</span>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<!-- ===== FORMULARIO ===== -->
<div class="form-card">
    <div class="info-box">
        <i class="fas fa-info-circle"></i>
        <span class="info-text">
            <strong>Editando usuario:</strong> <?php echo htmlspecialchars($fila['nombre']); ?>
        </span>
        <span class="badge-id">
            <i class="fas fa-hashtag"></i> ID #<?php echo $fila['id']; ?>
        </span>
    </div>

    <!-- ===== ACTION CORREGIDO ===== -->
    <form action="actualizar.php" method="POST">
        <input type="hidden" name="id" value="<?php echo $fila['id']; ?>">

        <div class="row g-4">
            <!-- NOMBRE -->
            <div class="col-md-6">
                <label for="nombre" class="form-label">
                    <i class="fas fa-user"></i> Nombre Completo <span class="required-star">*</span>
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-user"></i>
                    <input type="text" class="form-control" id="nombre" name="nombre"
                        value="<?php echo htmlspecialchars($fila['nombre']); ?>" required>
                </div>
            </div>

            <!-- EMAIL -->
            <div class="col-md-6">
                <label for="email" class="form-label">
                    <i class="fas fa-envelope"></i> Correo Electrónico <span class="required-star">*</span>
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-envelope"></i>
                    <input type="email" class="form-control" id="email" name="email"
                        value="<?php echo htmlspecialchars($fila['email']); ?>" required>
                </div>
            </div>

            <!-- EDAD -->
            <div class="col-md-6">
                <label for="edad" class="form-label">
                    <i class="fas fa-calendar-alt"></i> Edad <span class="required-star">*</span>
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-calendar-alt"></i>
                    <input type="number" class="form-control" id="edad" name="edad"
                        value="<?php echo $fila['edad']; ?>" min="1" max="120" required>
                </div>
            </div>

            <!-- CONTRASEÑA -->
            <div class="col-md-6">
                <label for="password" class="form-label">
                    <i class="fas fa-lock"></i> Nueva Contraseña
                </label>
                <div class="input-icon-wrapper">
                    <i class="fas fa-lock"></i>
                    <input type="password" class="form-control" id="password" name="password"
                        placeholder="Dejar vacío para conservar la actual">
                </div>
                <div class="password-hint">
                    <i class="fas fa-info-circle"></i>
                    Solo llenar si deseas cambiar la contraseña
                </div>
            </div>
        </div>

        <hr class="divider">

        <!-- ===== BOTONES ===== -->
        <div class="btn-group-custom">
            <button type="submit" class="btn btn-primary">
                <i class="fas fa-save me-2"></i> Actualizar Usuario
            </button>
            <a href="menu.php?page=usuarios" class="btn btn-secondary">
                <i class="fas fa-times me-2"></i> Cancelar
            </a>
        </div>
    </form>
</div>