<?php include 'includes/header.php'; ?>
<?php include 'includes/sidebar.php'; ?>
<?php require 'includes/db_connect.php'; ?>

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $fullname = mysqli_real_escape_string($conn, $_POST['fullname']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $user_pass = $_POST['password'];
    //$password = password_hash($_POST['password']);
     $password = md5($_POST['password']);

    $check = $conn->query("SELECT * FROM admin WHERE email = '$email'");
    if ($check->num_rows > 0) {
        $error = "Admin with this email already exists!";
    } else {
        $insert = $conn->query("INSERT INTO admin (fullname, email, password, user_pass, reg_date) 
                                VALUES ('$fullname', '$email', '$password', '$user_pass', NOW())");
        if ($insert) {
            $success = "Admin Registered Successfully!";
        } else {
            $error = "Error registering admin: " . $conn->error;
        }
    }
}
?>

<div class="container mt-4">
    <h4 class="text-orange">Create New Admin</h4>

    <?php if (isset($success)) echo "<div class='alert alert-success'>$success</div>"; ?>
    <?php if (isset($error)) echo "<div class='alert alert-danger'>$error</div>"; ?>

    <form method="post" action="">
        <div class="mb-3">
            <label class="form-label text-orange">Full Name</label>
            <input type="text" name="fullname" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label text-orange">Email</label>
            <input type="email" name="email" class="form-control" required>
        </div>

        <div class="mb-3">
            <label class="form-label text-orange">Password</label>
            <input type="password" name="password" class="form-control" required>
        </div>

        <button type="submit" class="btn btn-primary text-white">Register Admin</button>
    </form>
</div>

<?php include 'includes/footer.php'; ?>
