<?php
// registro.php - Registro de nuevos usuarios
// C.E.I. Federico Froebel - SIN ENCRIPTACION
require_once 'db.php';

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    $confirm_password = trim($_POST['confirm_password'] ?? '');
    $nombre_completo = trim($_POST['nombre_completo'] ?? '');
    $cedula = trim($_POST['cedula'] ?? '');
    $rol = $_POST['rol'] ?? 'secretaria';

    // Validaciones
    if (empty($username) || empty($password) || empty($confirm_password) || empty($nombre_completo)) {
        $error = 'Todos los campos marcados con * son obligatorios';
    } elseif (strlen($username) < 4) {
        $error = 'El usuario debe tener al menos 4 caracteres';
    } elseif (strlen($password) < 6) {
        $error = 'La contrasena debe tener al menos 6 caracteres';
    } elseif ($password !== $confirm_password) {
        $error = 'Las contrasenas no coinciden';
    } else {
        // Verificar si el usuario ya existe
        $check = $conn->prepare("SELECT id FROM usuarios WHERE username = ?");
        $check->bind_param("s", $username);
        $check->execute();
        $res = $check->get_result();

        if ($res->num_rows > 0) {
            $error = 'El usuario ya existe. Elija otro nombre de usuario.';
        } else {
            // Insertar nuevo usuario SIN ENCRIPTACION
            $stmt = $conn->prepare("INSERT INTO usuarios (username, password, nombre_completo, cedula, rol, activo) VALUES (?, ?, ?, ?, ?, 1)");
            $stmt->bind_param("sssss", $username, $password, $nombre_completo, $cedula, $rol);

            if ($stmt->execute()) {
                $success = 'Usuario registrado exitosamente. Ahora puede iniciar sesion.';
            } else {
                $error = 'Error al registrar el usuario: ' . $conn->error;
            }
        }
    }
}

// Si ya esta logueado, redirigir al dashboard
if (isset($_SESSION['usuario'])) {
    header("Location: dashboard.php");
    exit();
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registro de Usuario - C.E.I. Federico Froebel</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            overflow: hidden;
            position: relative;
            padding: 20px;
        }

        /* ===== FONDO CON IMAGEN DE LA ESCUELA ===== */
        .bg-school {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: url('fondo.jpg') center center / cover no-repeat;
            z-index: -2;
            animation: kenburns 25s ease-in-out infinite alternate;
        }

        @keyframes kenburns {
            0% { transform: scale(1) translate(0, 0); }
            50% { transform: scale(1.08) translate(-1%, -1%); }
            100% { transform: scale(1) translate(0, 0); }
        }

        .bg-overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(26,35,126,0.88) 0%, rgba(49,27,146,0.85) 40%, rgba(0,0,0,0.75) 100%);
            z-index: -1;
        }

        /* ===== PARTICULAS FLOTANTES ===== */
        .particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            pointer-events: none;
            overflow: hidden;
        }

        .particle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255,255,255,0.12);
            animation: float-particle linear infinite;
        }

        .particle:nth-child(1) { width: 8px; height: 8px; left: 8%; top: 100%; animation-duration: 13s; animation-delay: 0s; }
        .particle:nth-child(2) { width: 12px; height: 12px; left: 18%; top: 100%; animation-duration: 16s; animation-delay: 2s; }
        .particle:nth-child(3) { width: 6px; height: 6px; left: 28%; top: 100%; animation-duration: 11s; animation-delay: 1s; }
        .particle:nth-child(4) { width: 15px; height: 15px; left: 38%; top: 100%; animation-duration: 19s; animation-delay: 3s; }
        .particle:nth-child(5) { width: 10px; height: 10px; left: 48%; top: 100%; animation-duration: 15s; animation-delay: 0.5s; }
        .particle:nth-child(6) { width: 7px; height: 7px; left: 58%; top: 100%; animation-duration: 12s; animation-delay: 2.5s; }
        .particle:nth-child(7) { width: 14px; height: 14px; left: 68%; top: 100%; animation-duration: 17s; animation-delay: 1.5s; }
        .particle:nth-child(8) { width: 9px; height: 9px; left: 78%; top: 100%; animation-duration: 14s; animation-delay: 4s; }
        .particle:nth-child(9) { width: 11px; height: 11px; left: 88%; top: 100%; animation-duration: 18s; animation-delay: 0s; }
        .particle:nth-child(10) { width: 8px; height: 8px; left: 3%; top: 100%; animation-duration: 15s; animation-delay: 3s; }

        @keyframes float-particle {
            0% { transform: translateY(0) rotate(0deg); opacity: 0; }
            10% { opacity: 1; }
            90% { opacity: 1; }
            100% { transform: translateY(-120vh) rotate(720deg); opacity: 0; }
        }

        /* ===== ONDAS ANIMADAS ===== */
        .waves {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            height: 120px;
            z-index: 0;
            pointer-events: none;
        }

        .wave {
            position: absolute;
            bottom: 0;
            left: 0;
            width: 200%;
            height: 100%;
            background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1440 320'%3E%3Cpath fill='rgba(255,255,255,0.08)' d='M0,192L48,197.3C96,203,192,213,288,229.3C384,245,480,267,576,250.7C672,235,768,181,864,181.3C960,181,1056,235,1152,234.7C1248,235,1344,181,1392,154.7L1440,128L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z'%3E%3C/path%3E%3C/svg%3E") repeat-x;
            background-size: 50% 100%;
            animation: wave-move 8s linear infinite;
        }

        .wave:nth-child(2) {
            bottom: 10px;
            opacity: 0.5;
            animation-duration: 12s;
            animation-direction: reverse;
        }

        .wave:nth-child(3) {
            bottom: 20px;
            opacity: 0.3;
            animation-duration: 16s;
        }

        @keyframes wave-move {
            0% { transform: translateX(0); }
            100% { transform: translateX(-50%); }
        }

        /* ===== CONTENEDOR ===== */
        .register-container {
            position: relative;
            z-index: 1;
            width: 100%;
            max-width: 520px;
            animation: slideUp 0.8s ease-out;
        }

        @keyframes slideUp {
            from { opacity: 0; transform: translateY(40px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* ===== MEMBRETE ===== */
        .membrete-section {
            text-align: center;
            margin-bottom: 20px;
            animation: fadeInScale 0.8s ease-out both;
        }

        .membrete-img {
            width: 200px;
            height: auto;
            max-height: 90px;
            object-fit: contain;
            border-radius: 12px;
            box-shadow: 0 8px 25px rgba(0,0,0,0.3);
            animation: shimmer 3s ease-in-out infinite;
            background: white;
            padding: 5px;
        }

        @keyframes shimmer {
            0%, 100% { box-shadow: 0 8px 25px rgba(0,0,0,0.3); }
            50% { box-shadow: 0 8px 35px rgba(255,255,255,0.25); }
        }

        @keyframes fadeInScale {
            from { opacity: 0; transform: scale(0.85); }
            to { opacity: 1; transform: scale(1); }
        }

        /* ===== TARJETA DE REGISTRO ===== */
        .register-card {
            background: rgba(255, 255, 255, 0.96);
            border-radius: 24px;
            padding: 40px 35px;
            box-shadow: 0 30px 70px rgba(0,0,0,0.35);
            backdrop-filter: blur(10px);
            animation: slideUp 0.7s ease-out 0.15s both;
        }

        .register-header {
            text-align: center;
            margin-bottom: 28px;
        }

        .register-header h3 {
            font-size: 1.7rem;
            color: #1a237e;
            font-weight: 700;
            margin-bottom: 6px;
            animation: slideInLeft 0.5s ease-out 0.4s both;
        }

        .register-header p {
            color: #777;
            font-size: 0.85rem;
            animation: slideInLeft 0.5s ease-out 0.5s both;
        }

        @keyframes slideInLeft {
            from { opacity: 0; transform: translateX(-15px); }
            to { opacity: 1; transform: translateX(0); }
        }

        /* ===== MENSAJES ===== */
        .error-msg {
            background: linear-gradient(135deg, #ffebee 0%, #fce4ec 100%);
            color: #c62828;
            padding: 14px 18px;
            border-radius: 12px;
            font-size: 0.85rem;
            margin-bottom: 22px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-left: 4px solid #c62828;
            animation: shake 0.5s ease;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            20% { transform: translateX(-8px); }
            40% { transform: translateX(8px); }
            60% { transform: translateX(-4px); }
            80% { transform: translateX(4px); }
        }

        .success-msg {
            background: linear-gradient(135deg, #e8f5e9 0%, #e0f2f1 100%);
            color: #2e7d32;
            padding: 14px 18px;
            border-radius: 12px;
            font-size: 0.85rem;
            margin-bottom: 22px;
            display: flex;
            align-items: center;
            gap: 10px;
            border-left: 4px solid #4caf50;
            animation: slideInLeft 0.4s ease-out;
        }

        /* ===== FORMULARIO ===== */
        .form-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 16px;
        }

        .form-group {
            margin-bottom: 8px;
        }

        .form-group.full-width {
            grid-column: 1 / -1;
        }

        .form-group label {
            display: block;
            font-size: 0.8rem;
            font-weight: 600;
            color: #444;
            margin-bottom: 6px;
        }

        .form-group label .required {
            color: #e53935;
            margin-left: 2px;
        }

        .input-icon {
            position: relative;
        }

        .input-icon input,
        .input-icon select {
            width: 100%;
            padding: 14px 14px 14px 46px;
            border: 2.5px solid #e8e8e8;
            border-radius: 12px;
            font-size: 0.9rem;
            font-family: 'Poppins', sans-serif;
            transition: all 0.35s ease;
            background: #fafafa;
        }

        .input-icon input:focus,
        .input-icon select:focus {
            outline: none;
            border-color: #1a237e;
            background: white;
            box-shadow: 0 0 0 5px rgba(26,35,126,0.1);
            transform: translateY(-2px);
        }

        .input-icon .icon {
            position: absolute;
            left: 16px;
            top: 50%;
            transform: translateY(-50%);
            font-size: 1rem;
            opacity: 0.5;
            transition: all 0.3s;
        }

        .input-icon input:focus + .icon,
        .input-icon input:focus ~ .icon,
        .input-icon select:focus + .icon,
        .input-icon select:focus ~ .icon {
            opacity: 1;
            color: #1a237e;
        }

        /* ===== BOTON ===== */
        .btn-register {
            width: 100%;
            padding: 16px;
            background: linear-gradient(135deg, #1a237e 0%, #3949ab 50%, #5c6bc0 100%);
            background-size: 200% 200%;
            color: white;
            border: none;
            border-radius: 14px;
            font-size: 1.05rem;
            font-weight: 700;
            font-family: 'Poppins', sans-serif;
            cursor: pointer;
            transition: all 0.4s ease;
            margin-top: 10px;
            box-shadow: 0 6px 25px rgba(26,35,126,0.35);
            animation: gradientShift 4s ease infinite;
            position: relative;
            overflow: hidden;
            grid-column: 1 / -1;
        }

        @keyframes gradientShift {
            0%, 100% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
        }

        .btn-register::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
            transition: left 0.5s;
        }

        .btn-register:hover {
            transform: translateY(-3px);
            box-shadow: 0 12px 35px rgba(26,35,126,0.45);
        }

        .btn-register:hover::before {
            left: 100%;
        }

        .btn-register:active {
            transform: translateY(0);
        }

        /* ===== ENLACE LOGIN ===== */
        .login-link {
            text-align: center;
            margin-top: 22px;
            font-size: 0.85rem;
            color: #666;
        }

        .login-link a {
            color: #1a237e;
            font-weight: 600;
            text-decoration: none;
            position: relative;
            transition: color 0.3s;
        }

        .login-link a::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 0;
            height: 2px;
            background: linear-gradient(90deg, #1a237e, #3949ab);
            transition: width 0.3s ease;
        }

        .login-link a:hover {
            color: #3949ab;
        }

        .login-link a:hover::after {
            width: 100%;
        }

        /* ===== FOOTER ===== */
        .footer-register {
            text-align: center;
            margin-top: 20px;
            color: rgba(255,255,255,0.7);
            font-size: 0.75rem;
            position: relative;
            z-index: 1;
        }

        /* ===== RESPONSIVE ===== */
        @media (max-width: 600px) {
            .form-grid {
                grid-template-columns: 1fr;
            }
            .register-card {
                padding: 28px 22px;
            }
            .register-header h3 {
                font-size: 1.4rem;
            }
            .membrete-img {
                width: 150px;
            }
        }
    </style>
</head>
<body>

    <!-- Fondo de la escuela -->
    <div class="bg-school"></div>
    <div class="bg-overlay"></div>

    <!-- Particulas flotantes -->
    <div class="particles">
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
        <div class="particle"></div>
    </div>

    <!-- Ondas animadas -->
    <div class="waves">
        <div class="wave"></div>
        <div class="wave"></div>
        <div class="wave"></div>
    </div>

    <!-- Contenedor -->
    <div class="register-container">
        <!-- Membrete -->
        <div class="membrete-section">
            <img src="men.jpg" alt="Membrete C.E.I. Federico Froebel" class="membrete-img" onerror="this.style.display='none'">
        </div>

        <!-- Tarjeta de registro -->
        <div class="register-card">
            <div class="register-header">
                <h3>Crear Cuenta</h3>
                <p>Complete el formulario para registrarse en el sistema</p>
            </div>

            <?php if ($error): ?>
            <div class="error-msg">
                <span>&#9888;</span> <?= htmlspecialchars($error) ?>
            </div>
            <?php endif; ?>

            <?php if ($success): ?>
            <div class="success-msg">
                <span>&#10004;</span> <?= htmlspecialchars($success) ?>
            </div>
            <?php endif; ?>

            <form method="POST" action="" autocomplete="off">
                <div class="form-grid">
                    <div class="form-group full-width">
                        <label>Nombre Completo <span class="required">*</span></label>
                        <div class="input-icon">
                            <input type="text" name="nombre_completo" placeholder="Ej: Maria Gonzalez" required autofocus>
                            <span class="icon">&#128100;</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Usuario <span class="required">*</span></label>
                        <div class="input-icon">
                            <input type="text" name="username" placeholder="Min. 4 caracteres" required>
                            <span class="icon">&#128481;</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Cedula</label>
                        <div class="input-icon">
                            <input type="text" name="cedula" placeholder="V-12345678">
                            <span class="icon">&#127380;</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Contrasena <span class="required">*</span></label>
                        <div class="input-icon">
                            <input type="password" name="password" placeholder="Min. 6 caracteres" required>
                            <span class="icon">&#128274;</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Confirmar Contrasena <span class="required">*</span></label>
                        <div class="input-icon">
                            <input type="password" name="confirm_password" placeholder="Repita la contrasena" required>
                            <span class="icon">&#9989;</span>
                        </div>
                    </div>

                    <div class="form-group full-width">
                        <label>Rol en el Sistema <span class="required">*</span></label>
                        <div class="input-icon">
                            <select name="rol" required>
                                <option value="secretaria">Secretaria</option>
                                <option value="directora">Directora</option>
                                <option value="admin">Administrador</option>
                            </select>
                            <span class="icon">&#127941;</span>
                        </div>
                    </div>

                    <button type="submit" class="btn-register">Registrarse</button>
                </div>
            </form>

            <div class="login-link">
                Ya tienes una cuenta? <a href="login.php">Inicia sesion aqui</a>
            </div>
        </div>

        <p class="footer-register">&copy; <?= date('Y') ?> C.E.I. Federico Froebel &middot; Todos los derechos reservados</p>
    </div>

</body>
</html>
