<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// Retrieve errors and old input if redirected back from register_save.php
$errors = $_SESSION['errors'] ?? [];
$old    = $_SESSION['old'] ?? [];
unset($_SESSION['errors'], $_SESSION['old']);
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>ICMR-NIRRCH - Register</title>

<!-- Favicon -->
<link rel="icon" href="assets/img/logo.png" type="image/png">

<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">

<!-- Bootstrap Icons -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet">

<style>
body { 
    padding-top: 120px; /* Space for fixed header */ 
    font-family: "Noto Sans", sans-serif;
    background-color: #f8f9fa;
}

.card {
    border-radius: 0.5rem;
}

.card-title {
    font-weight: bold;
}

.btn-primary {
    background-color: #1490d7;
    border-color: #1490d7;
}

.btn-primary:hover {
    background-color: #0f78c1;
    border-color: #0f78c1;
}

a {
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}
</style>
</head>
<body>

<!-- Include Header -->
<?php include 'header.php'; ?>

<!-- Registration Form -->
<section class="py-5">
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-6 col-lg-5">
            <div class="card shadow-sm">
                <div class="card-body p-4">
                    <h3 class="card-title mb-4 text-center">Register</h3>

                    <!-- Display All Validation Errors -->
                    <?php if(!empty($errors)): ?>
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                            <?php foreach($errors as $err): ?>
                                <li><?= htmlspecialchars($err) ?></li>
                            <?php endforeach; ?>
                            </ul>
                        </div>
                    <?php endif; ?>

                    <form method="post" action="register_save.php">
                        <div class="mb-3">
                            <input type="text" name="full_name" class="form-control" placeholder="Full Name" required
                                   value="<?= isset($old['full_name']) ? htmlspecialchars($old['full_name']) : ''; ?>">
                        </div>
                        <div class="mb-3">
                            <input type="text" name="mobile" class="form-control" placeholder="Mobile" required
                                   value="<?= isset($old['mobile']) ? htmlspecialchars($old['mobile']) : ''; ?>">
                        </div>
                        <div class="mb-3">
                            <input type="email" name="email" class="form-control" placeholder="Email" required
                                   value="<?= isset($old['email']) ? htmlspecialchars($old['email']) : ''; ?>">
                        </div>
                        <div class="mb-3">
                            <input type="password" name="password" class="form-control" placeholder="Password" required>
                        </div>
                        <div class="mb-3">
                            <input type="password" name="cpassword" class="form-control" placeholder="Confirm Password" required>
                        </div>
                        <button type="submit" class="btn btn-primary w-100" name="register">Register</button>
                    </form>

                    <p class="mt-3 text-center">
                        Already have an account? <a href="login.php">Login here</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
</section>

<!-- Include Footer -->
<?php include 'footer.php'; ?>

<!-- Bootstrap 5 JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
