<?php
include("db.php");

$admin = [];

// Retrieve admin information based on the provided admin_id
if (isset($_GET['admin_id'])) {
    $admin_id = $_GET['admin_id'];
    $get_admin_query = "SELECT * FROM admin_table WHERE admin_id = $admin_id";
    $result = mysqli_query($db, $get_admin_query);
    $admin = mysqli_fetch_assoc($result);
}

// Update admin information in the database if the form is submitted
if (isset($_POST['editadmin'])) {
    $admin_id = mysqli_real_escape_string($db, $_POST['admin_id']);
    $admin_name = mysqli_real_escape_string($db, $_POST['admin_name']);
    $admin_post = mysqli_real_escape_string($db, $_POST['admin_post']);
    $email = mysqli_real_escape_string($db, $_POST['email']);

    // Update admin information in the database
    $update_admin_query = "UPDATE admin_table SET admin_name='$admin_name', admin_post='$admin_post', email='$email' WHERE admin_id = $admin_id";

    if (mysqli_query($db, $update_admin_query)) {
        echo "<script>alert('Admin information updated successfully.');</script>";
        header("Location: index.php?env=edit_admin&admin_id={$admin_id}");
        exit();
    } else {
        echo "<script>alert('Error updating admin information: ".mysqli_error($db)."');</script>";
    }
}
?>

<!-- Admin profile edit form -->
<div class="container-fluid pt-4 px-4">
    <div class="bg-secondary text-center rounded p-4">
        <div class="d-flex align-items-center justify-content-between mb-4">
            <h6 class="mb-0">Edit Admin Information</h6>
        </div>
        <div class="table-responsive">
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
                        <!-- Form for editing admin information -->
                        <form method="POST" action="index.php?env=edit_admin&admin_id=<?php echo htmlentities($admin['admin_id']); ?>">
                            <input type="hidden" name="admin_id" value="<?php echo htmlentities($admin['admin_id']); ?>">
                            <div class="form-group">
                                <input type="text" name="admin_name" value="<?php echo htmlentities($admin['admin_name']); ?>" class="form-control" placeholder="Admin Name" required><br/>
                            </div>
                            <div class="form-group">
                                <input type="text" name="admin_post" value="<?php echo htmlentities($admin['admin_post']); ?>" class="form-control" placeholder="Admin Post" required><br/>
                            </div>       
                            <div class="form-group">
                                <input type="email" name="email" value="<?php echo htmlentities($admin['email']); ?>" class="form-control" placeholder="Email" required><br/>
                            </div>
                            <div class="form-group">
                                <button type="submit" name="editadmin" class="btn btn-outline-primary btn-block">Save Changes</button>
                            </div>
                        </form>
                    </div>
                    <div class="col-md-6"></div>
                </div>
            </div>
        </div>
    </div>
</div>