<?php
/**
 * Page de connexion et inscription
 * Fichier : login.php
 */

require_once __DIR__ . '/config/config.php';

// Si déjà connecté, rediriger vers le dashboard approprié
if (isLoggedIn()) {
    $user = currentUser();
    if ($user['id_type'] == 1) {
        header('Location: enseignant/dashboard.php');
    } else {
        header('Location: eleve/dashboard.php');
    }
    exit;
}

// Récupérer les messages de session
$error = $_SESSION['error'] ?? null;
$success = $_SESSION['success'] ?? null;
unset($_SESSION['error'], $_SESSION['success']);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Connexion - Plateforme Mathématiques</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .container {
            display: flex;
            gap: 30px;
            max-width: 1000px;
            width: 100%;
        }
        
        .form-card {
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            padding: 40px;
            flex: 1;
            animation: slideIn 0.5s ease-out;
        }
        
        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 28px;
        }
        
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 500;
            font-size: 14px;
        }
        
        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 8px;
            font-size: 15px;
            transition: all 0.3s ease;
        }
        
        input[type="text"]:focus,
        input[type="password"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }
        
        .btn {
            width: 100%;
            padding: 14px;
            border: none;
            border-radius: 8px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(102, 126, 234, 0.4);
        }
        
        .btn-secondary {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }
        
        .btn-secondary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 20px rgba(245, 87, 108, 0.4);
        }
        
        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .alert-error {
            background: #fee;
            color: #c33;
            border: 1px solid #fcc;
        }
        
        .alert-success {
            background: #efe;
            color: #3c3;
            border: 1px solid #cfc;
        }
        
        .help-text {
            font-size: 13px;
            color: #888;
            margin-top: 5px;
        }
        
        .info-box {
            background: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 15px;
            border-radius: 5px;
            margin-top: 20px;
            font-size: 13px;
            color: #555;
        }
        
        .info-box strong {
            display: block;
            margin-bottom: 5px;
            color: #333;
        }
        
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- FORMULAIRE CONNEXION -->
        <div class="form-card">
            <h1>🔐 Connexion</h1>
            <p class="subtitle">Élèves de classes fixes et enseignants</p>
            
            <?php if ($error): ?>
                <div class="alert alert-error"><?= htmlspecialchars($error) ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
            <?php endif; ?>
            
            <form method="POST" action="auth.php">
                <div class="form-group">
                    <label for="login">Identifiant</label>
                    <input 
                        type="text" 
                        id="login" 
                        name="login" 
                        required 
                        autofocus
                        placeholder="Votre identifiant"
                    >
                </div>
                
                <div class="form-group">
                    <label for="password">Mot de passe</label>
                    <input 
                        type="password" 
                        id="password" 
                        name="password" 
                        required
                        placeholder="Votre mot de passe"
                    >
                </div>
                
                <button type="submit" class="btn btn-primary">
                    Se connecter
                </button>
            </form>
            
            <div class="info-box">
                <strong>📚 Élèves en classe fixe</strong>
                Votre identifiant et mot de passe vous ont été fournis par votre enseignant.
            </div>
        </div>
        
        <!-- FORMULAIRE INSCRIPTION LIBRE -->
        <div class="form-card">
            <h1>📝 Inscription libre</h1>
            <p class="subtitle">Pour les élèves en soutien</p>
            
            <form method="POST" action="auth.php?action=register">
                <div class="form-group">
                    <label for="prenom">Prénom</label>
                    <input 
                        type="text" 
                        id="prenom" 
                        name="prenom" 
                        required
                        placeholder="Votre prénom"
                    >
                </div>
                
                <div class="form-group">
                    <label for="nom">Nom</label>
                    <input 
                        type="text" 
                        id="nom" 
                        name="nom" 
                        required
                        placeholder="Votre nom"
                    >
                </div>
                
                <div class="form-group">
                    <label for="password_register">Mot de passe</label>
                    <input 
                        type="password" 
                        id="password_register" 
                        name="password" 
                        required
                        placeholder="Format JJMM (ex: 2710)"
                        pattern="\d{4}"
                        maxlength="4"
                    >
                    <div class="help-text">
                        📅 Format : JJMM (jour et mois de naissance, ex: 2710 pour le 27 octobre)
                    </div>
                </div>
                
                <button type="submit" class="btn btn-secondary">
                    S'inscrire au groupe de soutien
                </button>
            </form>
            
            <div class="info-box">
                <strong>💡 Inscription automatique</strong>
                Votre identifiant sera généré automatiquement au format : prenom.nom<br>
                Vous serez inscrit dans le groupe de soutien (pas de notes au bulletin).
            </div>
        </div>
    </div>
</body>
</html>
