<?php
session_start();
require_once __DIR__ . '/config/db.php';

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

    $username = trim($_POST['username']);
    $email    = trim($_POST['email']);
    $password = $_POST['password'];

    if ($username && $email && $password) {

        $hashed = password_hash($password, PASSWORD_DEFAULT);

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

        if ($stmt->execute([$username, $email, $hashed])) {
            $_SESSION['success_message'] =
                "Your institution registered successfully. Please login.";
            header("Location: login.php");
            exit();
        } else {
            $error = "Registration failed. Try again.";
        }
    } else {
        $error = "All fields are required.";
    }
}
?>

<!-- HTML PART -->
<?php require_once 'includes/header.php'; ?>

<div class="login-wrap">
    <h2>Register Institution</h2>

    <?php if (!empty($error)): ?>
        <p style="color:#fca5a5; font-size:13px;"><?= $error ?></p>
    <?php endif; ?>

    <form method="POST">
        <label>Username</label>
        <input type="text" name="username" required>

        <label>Email address</label>
        <input type="email" name="email" required>

        <label>Password</label>
        <input type="password" name="password" required>

        <button type="submit">Register</button>
    </form>
</div>

</body>
</html>
