<?php require "../includes/header.php"; ?>
<?php require "../../config/config.php"; ?>

<?php
if(!isset($_SESSION['admin_username'])){ header("location: ../login.php"); exit; }

$success = $error = '';
if(isset($_POST['submit'])){
    if(empty($_POST['username']) || empty($_POST['email']) || empty($_POST['password'])){
        $error = 'All fields are required.';
    } else {
        $username = $_POST['username'];
        $email    = $_POST['email'];
        $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

        // Check if email exists
        $check = $conn->prepare("SELECT id FROM admins WHERE email = :email");
        $check->execute([':email' => $email]);
        if($check->rowCount() > 0){
            $error = 'Email already exists.';
        } else {
            $insert = $conn->prepare("INSERT INTO admins (username, email, mypassword) VALUES (:username, :email, :mypassword)");
            $insert->execute([':username' => $username, ':email' => $email, ':mypassword' => $password]);
            $success = 'Admin created successfully!';
        }
    }
}
?>

<div class="topbar">
    <h4><i class="fa fa-user-plus mr-2"></i> Create Admin</h4>
    <div class="admin-badge">
        <div class="avatar"><?php echo strtoupper(substr($_SESSION['admin_username'],0,1)); ?></div>
        <span><?php echo htmlspecialchars($_SESSION['admin_username']); ?></span>
        <a href="../logout.php"><i class="fa fa-sign-out"></i> Logout</a>
    </div>
</div>

<div class="main-content">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="admin-form-card">
                <h5 class="mb-4" style="color:#1a2d5a; font-weight:700;">Create New Admin</h5>

                <?php if($success): ?><div class="alert alert-success"><?php echo $success; ?> <a href="index.php">View all admins</a></div><?php endif; ?>
                <?php if($error): ?><div class="alert alert-danger"><?php echo $error; ?></div><?php endif; ?>

                <form method="POST" action="create.php">
                    <div class="form-group">
                        <label>Username</label>
                        <input type="text" name="username" class="form-control" placeholder="Enter username" required>
                    </div>
                    <div class="form-group">
                        <label>Email Address</label>
                        <input type="email" name="email" class="form-control" placeholder="Enter email" required>
                    </div>
                    <div class="form-group mb-4">
                        <label>Password</label>
                        <input type="password" name="password" class="form-control" placeholder="Enter password" required>
                    </div>
                    <button type="submit" name="submit" class="btn btn-admin-primary">
                        <i class="fa fa-save mr-1"></i> Create Admin
                    </button>
                    <a href="index.php" class="btn btn-secondary ml-2">Cancel</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php require "../includes/footer.php"; ?>
