<?php
include('config.php');
$error = "";
$success = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    $stmt = $conn->prepare("SELECT id FROM users WHERE username = ? UNION SELECT id FROM pending_users WHERE username = ?");
    $stmt->bind_param("ss", $username, $username);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $error = "Ce nom d'utilisateur est déjà utilisé.";
    } else {
        $stmt = $conn->prepare("INSERT INTO pending_users (username, password) VALUES (?, ?)");
        $stmt->bind_param("ss", $username, $password);
        if ($stmt->execute()) {
            $success = "Votre demande a été envoyée. Un administrateur doit la valider.";
        } else {
            $error = "Erreur lors de l'inscription.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <link rel="icon" href="https://i.postimg.cc/5972zzWx/Capturec-removebg-preview.png" type="image/png">
    <title>Inscription - INSPE</title>
    <link href="https://fonts.googleapis.com/css2?family=Comfortaa:wght@300;400;600&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/particles.js"></script>
    <style>
        /* PARTICLES */
        #particles-js { position: fixed; width: 100%; height: 100%; top: 0; left: 0; z-index: -1; }

        /* RESET */
        * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Comfortaa', cursive; color: #fff; }
        body {
            margin: 0;
            font-family: 'Comfortaa', cursive;
            background-color: #0e0e0e;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        /* CONTAINER */
        .form-container {
            background: linear-gradient(145deg, #2b2b2b, #111);
            padding: 40px;
            border-radius: 15px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.5);
            width: 100%;
            max-width: 450px;
            position: relative;
        }

        h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #ffffff;
        }

        label {
            display: block;
            margin-bottom: 6px;
            font-weight: 600;
        }

        input[type="text"],
        input[type="password"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            background-color: #1e1e1e;
            border: 1px solid #333;
            border-radius: 8px;
            color: white;
        }

        button {
            width: 100%;
            padding: 14px;
            background-color: #c84dd0;
            color: white;
            border: none;
            border-radius: 12px;
            font-weight: 600;
            font-size: 15px;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        button:hover {
            background-color: rgb(191, 38, 202);
        }

        .message {
            margin-top: 15px;
            text-align: center;
            font-size: 0.95em;
            padding: 10px;
            border-radius: 8px;
        }

        .error {
            background-color: #8b0000;
            color: #ffcfcf;
        }

        .success {
            background-color: #2d572c;
            color: #c4ffc4;
        }

        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #8b5cf6;
            text-decoration: none;
        }

        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div id="particles-js"></div> <!-- Particles.js background -->
    
    <div class="form-container">
        <h2>Créer un compte</h2>
        <form method="POST">
            <label for="username">Nom d'utilisateur</label>
            <input type="text" name="username" id="username" required>

            <label for="password">Mot de passe</label>
            <input type="password" name="password" id="password" required>

            <button type="submit">Envoyer la demande</button>

            <?php if ($error): ?>
                <div class="message error"><?= htmlspecialchars($error) ?></div>
            <?php elseif ($success): ?>
                <div class="message success"><?= htmlspecialchars($success) ?></div>
            <?php endif; ?>
        </form>

        <a href="login.php" class="back-link">← Retour à la connexion</a>
    </div>

<script>
    particlesJS('particles-js', {
    particles: {
      number: { value: 60 },
      color: { value: '#c84dd0' },
      shape: { type: 'circle' },
      size: { value: 3 },
      move: { speed: 1 }
    }
    });
</script>

</body>
</html>
