<?php
session_start();

require_once 'config.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $login = $_POST['login'] ?? '';
    $mot_de_passe = $_POST['mot_de_passe'] ?? '';
    
    if ($login && $mot_de_passe) {
        $stmt = $pdo->prepare("SELECT * FROM utilisateur WHERE login = ? AND etat_compte = 'actif'");
        $stmt->execute([$login]);
        $user = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($user && $mot_de_passe === $user['mot_de_passe']) {
            $_SESSION['id_user'] = $user['id_user'];
            $_SESSION['user_login'] = $user['login'];
            $_SESSION['user_nom'] = ($user['nom'] ?? '') . ' ' . ($user['prenom'] ?? '');
            $_SESSION['user_role'] = $user['role'];
            
            switch($user['role']) {
                case 'super_admin':
                case 'admin':
                    $_SESSION['admin_logged_in'] = true;
                    header('Location: admin.php');
                    break;
                case 'enseignant':
                    $stmt2 = $pdo->prepare("SELECT id_enseignant FROM enseignant WHERE id_user = ?");
                    $stmt2->execute([$user['id_user']]);
                    $enseignant = $stmt2->fetch();
                    if ($enseignant) $_SESSION['id_enseignant'] = $enseignant['id_enseignant'];
                    header('Location: enseignant.php');
                    break;
                case 'etudiant':
                    $stmt2 = $pdo->prepare("SELECT id_etudiant FROM etudiants WHERE id_user = ?");
                    $stmt2->execute([$user['id_user']]);
                    $etudiant = $stmt2->fetch();
                    if ($etudiant) $_SESSION['id_etudiant'] = $etudiant['id_etudiant'];
                    header('Location: eleve.php');
                    break;
                case 'stagiaire':
                    $stmt2 = $pdo->prepare("SELECT id_stagiaire FROM stagiaire WHERE id_user = ?");
                    $stmt2->execute([$user['id_user']]);
                    $stagiaire = $stmt2->fetch();
                    if ($stagiaire) $_SESSION['id_stagiaire'] = $stagiaire['id_stagiaire'];
                    header('Location: stagiaire.php');
                    break;
                default:
                    $error = 'Rôle utilisateur inconnu';
                    break;
            }
            exit();
        } else {
            $error = 'Identifiants incorrects ou compte inactif';
        }
    } else {
        $error = 'Veuillez remplir tous les champs';
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion | CPN Formation</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Roboto', sans-serif;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #f2f3f6 0%, #e2e8f0 100%);
            position: relative;
            overflow-x: hidden;
        }

        /* Particules de fond animées */
        .bg-particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: 0;
            overflow: hidden;
        }

        .particle {
            position: absolute;
            background: linear-gradient(135deg, #667eea20, #764ba220);
            border-radius: 50%;
            animation: float 20s infinite ease-in-out;
        }

        @keyframes float {
            0%, 100% { transform: translateY(0) translateX(0); opacity: 0.3; }
            25% { transform: translateY(-30px) translateX(20px); opacity: 0.5; }
            50% { transform: translateY(20px) translateX(-15px); opacity: 0.4; }
            75% { transform: translateY(-10px) translateX(10px); opacity: 0.6; }
        }

        /* Conteneur principal */
        .login-wrapper {
            width: 100%;
            max-width: 1100px;
            padding: 20px;
            position: relative;
            z-index: 10;
            animation: fadeInUp 0.6s ease-out;
        }

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

        /* Carte principale */
        .login-container {
            display: flex;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 32px;
            overflow: hidden;
            box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
            backdrop-filter: blur(10px);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .login-container:hover {
            transform: translateY(-5px);
            box-shadow: 0 30px 60px -15px rgba(0, 0, 0, 0.3);
        }

        /* Colonne de gauche - Zone d'image */
        .login-image {
            flex: 1.2;
            background: linear-gradient(135deg, #2f6d8c 0%, #095175 100%);
            padding: 50px 40px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            position: relative;
            overflow: hidden;
        }

        .login-image::before {
            content: '';
            position: absolute;
            width: 150%;
            height: 150%;
            top: -25%;
            left: -25%;
            background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
            animation: slowRotate 30s linear infinite;
        }

        @keyframes slowRotate {
            from { transform: rotate(0deg); }
            to { transform: rotate(360deg); }
        }

        .login-image img {
            position: relative;
            z-index: 2;
            max-width: 80%;
            height: auto;
            border-radius: 24px;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
            transition: transform 0.3s ease;
        }

        .login-image img:hover {
            transform: scale(1.02);
        }

        .image-caption {
            position: relative;
            z-index: 2;
            text-align: center;
            margin-top: 30px;
            color: white;
        }

        .image-caption h3 {
            font-size: 24px;
            font-weight: 700;
            margin-bottom: 8px;
            letter-spacing: -0.5px;
        }

        .image-caption p {
            font-size: 13px;
            opacity: 0.85;
        }

        /* Colonne de droite - Formulaire */
        .login-form {
            flex: 0.8;
            padding: 50px 45px;
            background: white;
        }

        .form-header {
            text-align: center;
            margin-bottom: 35px;
        }

        .form-header .logo-small {
            max-width: 120px;
            margin-bottom: 20px;
            border-radius: 16px;
            transition: transform 0.3s ease;
        }

        .form-header .logo-small:hover {
            transform: scale(1.05);
        }

        .form-header h1 {
            font-size: 32px;
            font-weight: 800;
            background: linear-gradient(135deg, #1e293b, #2f6d8c);
            background-clip: text;
            -webkit-background-clip: text;
            color: transparent;
            letter-spacing: -0.5px;
            margin-bottom: 8px;
        }

        .form-header p {
            color: #64748b;
            font-size: 14px;
        }

        /* Message d'erreur */
        .error-message {
            background: linear-gradient(135deg, #fee2e2, #fecaca);
            color: #991b1b;
            padding: 14px 18px;
            border-radius: 16px;
            margin-bottom: 25px;
            font-size: 13px;
            display: flex;
            align-items: center;
            gap: 12px;
            border-left: 4px solid #ef4444;
            font-weight: 500;
            animation: shake 0.4s ease;
        }

        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-5px); }
            75% { transform: translateX(5px); }
        }

        .error-message::before {
            content: '⚠️';
            font-size: 18px;
        }

        /* Groupes de formulaire */
        .form-group {
            margin-bottom: 22px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #1e293b;
            font-size: 13px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }

        .input-wrapper {
            position: relative;
        }

        .input-wrapper input {
            width: 100%;
            padding: 15px 18px;
            background: #f8fafc;
            border: 2px solid #e2e8f0;
            border-radius: 16px;
            color: #1e293b;
            font-size: 15px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .input-wrapper input:focus {
            outline: none;
            border-color: #2f6d8c;
            background: white;
            box-shadow: 0 0 0 4px rgba(47, 109, 140, 0.1);
        }

        .input-wrapper input::placeholder {
            color: #94a3b8;
            font-weight: 400;
        }

        /* Icônes dans les champs */
        .input-wrapper {
            position: relative;
        }

        .input-icon {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
            font-size: 18px;
            pointer-events: none;
        }

        /* Bouton de connexion */
        .login-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #2f6d8c, #095175);
            color: white;
            border: none;
            border-radius: 16px;
            font-size: 16px;
            font-weight: 700;
            letter-spacing: 0.5px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            position: relative;
            overflow: hidden;
            box-shadow: 0 5px 20px rgba(47, 109, 140, 0.3);
            margin-top: 10px;
        }

        .login-btn::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 0;
            height: 0;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.25);
            transform: translate(-50%, -50%);
            transition: width 0.5s, height 0.5s;
        }

        .login-btn:hover::before {
            width: 300px;
            height: 300px;
        }

        .login-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 30px rgba(47, 109, 140, 0.4);
        }

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

        /* Footer */
        .form-footer {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e2e8f0;
        }

        .form-footer p {
            color: #94a3b8;
            font-size: 12px;
        }

        /* Responsive */
        @media (max-width: 900px) {
            .login-container {
                flex-direction: column;
                max-width: 480px;
                margin: 0 auto;
            }
            
            .login-image {
                padding: 40px 30px;
            }
            
            .login-image img {
                max-width: 180px;
            }
            
            .image-caption h3 {
                font-size: 20px;
            }
            
            .login-form {
                padding: 40px 35px;
            }
            
            .form-header h1 {
                font-size: 28px;
            }
        }

        @media (max-width: 550px) {
            .login-wrapper {
                padding: 15px;
            }
            
            .login-form {
                padding: 30px 25px;
            }
            
            .form-header h1 {
                font-size: 24px;
            }
            
            .form-header .logo-small {
                max-width: 80px;
            }
            
            .login-btn {
                padding: 13px;
                font-size: 14px;
            }
        }

        /* Scrollbar personnalisée */
        ::-webkit-scrollbar {
            width: 8px;
        }
        
        ::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 10px;
        }
        
        ::-webkit-scrollbar-thumb {
            background: linear-gradient(135deg, #2f6d8c, #095175);
            border-radius: 10px;
        }
    </style>
</head>
<body>
    <!-- Particules animées -->
    <div class="bg-particles" id="particles"></div>

    <div class="login-wrapper">
        <div class="login-container">
            <!-- Colonne gauche : Zone d'image décorative -->
            <div class="login-image">
                <img src="image1.png" alt="Welcome to CPN" onerror="this.src='https://via.placeholder.com/400x500?text=CPN+Formation'">
                <div class="image-caption">
                    <h3>✨ CPN Formation ✨</h3>
                    <p>La formation au service de votre avenir</p>
                </div>
            </div>

            <!-- Colonne droite : Formulaire de connexion -->
            <div class="login-form">
                <div class="form-header">
                    <img src="logo.png" alt="CPN" class="logo-small" onerror="this.style.display='none'">
                    <h1>Connexion</h1>
                    <p>Accédez à votre espace personnel</p>
                </div>

                <?php if ($error): ?>
                    <div class="error-message">
                        <?php echo htmlspecialchars($error); ?>
                    </div>
                <?php endif; ?>

                <form method="POST" action="">
                    <div class="form-group">
                        <label>Identifiant</label>
                        <div class="input-wrapper">
                            <input type="text" name="login" placeholder="Entrez votre identifiant" required>
                            <span class="input-icon">👤</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Mot de passe</label>
                        <div class="input-wrapper">
                            <input type="password" name="mot_de_passe" placeholder="Entrez votre mot de passe" required>
                            <span class="input-icon">🔒</span>
                        </div>
                    </div>

                    <button type="submit" class="login-btn">
                        Se connecter →
                    </button>
                </form>

                <div class="form-footer">
                    <p>© 2026 CPN Formation - Tous droits réservés</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        // Génération des particules animées
        function createParticles() {
            const container = document.getElementById('particles');
            const particleCount = 30;
            
            for (let i = 0; i < particleCount; i++) {
                const particle = document.createElement('div');
                particle.classList.add('particle');
                
                // Taille aléatoire entre 5px et 30px
                const size = Math.random() * 25 + 5;
                particle.style.width = size + 'px';
                particle.style.height = size + 'px';
                
                // Position aléatoire
                particle.style.left = Math.random() * 100 + '%';
                particle.style.top = Math.random() * 100 + '%';
                
                // Durée d'animation aléatoire
                const duration = Math.random() * 20 + 15;
                particle.style.animationDuration = duration + 's';
                
                // Délai aléatoire
                particle.style.animationDelay = Math.random() * 10 + 's';
                
                container.appendChild(particle);
            }
        }
        
        // Lancer les particules au chargement
        createParticles();
    </script>
</body>
</html>