<?php 
require_once('../config.php');
include('../header/header.php');

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $username = trim($_POST['username']);
    $email = trim($_POST['email']);
    $password_plain = $_POST['password'];
    $password = password_hash($password_plain, PASSWORD_DEFAULT);

    $role = 'student'; // ✅ FORCE STUDENT
    $status = 'pending';

    try {

        // Check if email exists
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email = ?");
        $stmt->execute([$email]);

        if ($stmt->rowCount() > 0) {
            $error = "Email already exists.";
        } else {

            $stmt = $pdo->prepare("
                INSERT INTO users (username, email, password, role, status) 
                VALUES (?, ?, ?, ?, ?)
            ");

            $stmt->execute([$username, $email, $password, $role, $status]);

            $success = "Registration successful! Await admin approval.";
        }

    } catch (PDOException $e) {
        $error = "Error: " . $e->getMessage();
    }
}
?>

<div class="container py-5 min-vh-100 d-flex align-items-center">
    <div class="row w-100 justify-content-center">
        <div class="col-md-6 col-lg-5">
            <div class="card glass-card border-0 p-4 animate__animated animate__fadeInUp">
                <div class="text-center mb-4">
                    <img src="../images/logo.png" alt="Logo" class="rounded-circle shadow-sm mb-3" style="height: 80px;">
                    <h3 class="fw-bold">Student Registration</h3>
                    <p class="text-muted small">Create your account to start your learning journey</p>
                </div>

                <?php if ($error): ?>
                    <div class="alert alert-danger border-0 shadow-sm mb-4">
                        <i class="fas fa-exclamation-circle me-2"></i> <?php echo $error; ?>
                    </div>
                <?php endif; ?>

                <?php if ($success): ?>
                    <div class="alert alert-success border-0 shadow-sm mb-4 text-center py-4">
                        <div class="mb-3"><i class="fas fa-check-circle fa-3x"></i></div>
                        <h5 class="fw-bold">Registration Successful!</h5>
                        <p class="mb-0"><?php echo $success; ?></p>
                        <a href="login.php" class="btn btn-primary mt-3 px-4">Go to Login</a>
                    </div>
                <?php endif; ?>

                <?php if (!$success): ?>
                    <form method="POST">
                        <div class="mb-3">
                            <label class="form-label fw-medium small">Full Name</label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-end-0 rounded-start-3"><i class="fas fa-user text-muted"></i></span>
                                <input type="text" name="username" class="form-control bg-light border-start-0 rounded-end-3" placeholder="John Doe" required>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label class="form-label fw-medium small">Email Address</label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-end-0 rounded-start-3"><i class="fas fa-envelope text-muted"></i></span>
                                <input type="email" name="email" class="form-control bg-light border-start-0 rounded-end-3" placeholder="john@example.com" required>
                            </div>
                        </div>

                        <div class="mb-4">
                            <label class="form-label fw-medium small">Password</label>
                            <div class="input-group">
                                <span class="input-group-text bg-light border-end-0 rounded-start-3"><i class="fas fa-lock text-muted"></i></span>
                                <input type="password" name="password" class="form-control bg-light border-start-0 rounded-end-3" placeholder="••••••••" required>
                            </div>
                            <div class="form-text small">Minimum 8 characters with a mix of letters and numbers.</div>
                        </div>

                        <button class="btn btn-primary w-100 py-3 shadow-sm mb-3">
                            <i class="fas fa-user-plus me-2"></i> Create Account
                        </button>
                    </form>
                <?php endif; ?>

                <div class="text-center mt-2">
                    <p class="small text-muted mb-0">Already have an account? <a href="login.php" class="text-primary fw-bold text-decoration-none">Login here</a></p>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('../footer/footer.php'); ?>