<?php
require_once 'inc/connect.php';
session_start();

$feedback = '';

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username   = trim($_POST['username'] ?? '');
    $password   = $_POST['password'] ?? '';
    $email      = trim($_POST['email'] ?? '');
    $fullname   = trim($_POST['fullname'] ?? '');
    $role       = $_POST['role'] ?? 'exam_admin';

    if ($username && $password && $email && $role) {
        $stmt = mysqli_prepare($mysqli, "SELECT admin_id FROM admin_table WHERE username = ? OR email = ?");
        mysqli_stmt_bind_param($stmt, 'ss', $username, $email);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);

        if (mysqli_stmt_num_rows($stmt) > 0) {
            $feedback = "<div class='alert alert-warning'>Username or Email already exists.</div>";
        } else {
            $password_hash = password_hash($password, PASSWORD_DEFAULT);
            $insert = mysqli_prepare($mysqli, "INSERT INTO admin_table (username, password_hash, email, fullname, role, status) VALUES (?, ?, ?, ?, ?, 'active')");
            mysqli_stmt_bind_param($insert, 'sssss', $username, $password_hash, $email, $fullname, $role);
            mysqli_stmt_execute($insert);
            $feedback = "<div class='alert alert-success'>Admin account created successfully.</div>";
        }
    } else {
        $feedback = "<div class='alert alert-danger'>All fields are required.</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Enroll Admin</title>
  <link rel="stylesheet" href="/plugins/fontawesome-free/css/all.min.css">
  <link rel="stylesheet" href="/plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <link rel="stylesheet" href="/dist/css/adminlte.min.css">
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
  <?php include 'inc/admin_nav.php'; ?>

  <div class="content-wrapper">
    <section class="content mt-5">
      <div class="container">
        <div class="card card-primary">
          <div class="card-header"><h3 class="card-title">Enroll New Admin</h3></div>
          <form method="POST" action="">
            <div class="card-body">
              <?= $feedback ?>

              <div class="form-group">
                <label for="fullname">Full Name</label>
                <input type="text" name="fullname" class="form-control" id="fullname" required>
              </div>

              <div class="form-group">
                <label for="username">Username</label>
                <input type="text" name="username" class="form-control" id="username" required>
              </div>

              <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" name="email" class="form-control" id="email" required>
              </div>

              <div class="form-group">
                <label for="password">Password</label>
                <input type="password" name="password" class="form-control" id="password" required>
              </div>

              <div class="form-group">
                <label for="role">Admin Role</label>
                <select name="role" class="form-control" id="role" required>
                  <option value="super_admin">Super Admin</option>
                  <option value="exam_admin" selected>Exam Admin</option>
                  <option value="teacher">Teacher</option>
                  <option value="reviewer">Reviewer</option>
                </select>
              </div>
            </div>
            <div class="card-footer">
              <button type="submit" class="btn btn-primary">Create Admin</button>
            </div>
          </form>
        </div>
      </div>
    </section>
  </div>
</div>

<script src="/plugins/jquery/jquery.min.js"></script>
<script src="/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="/dist/js/adminlte.min.js"></script>
</body>
</html>
