<?php
require_once('source/session.php');
require_once('source/error.php');
require_once('source/dbconfig.php');
require_once('source/function.php');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['id'];
    $fullname = $_POST['fullname'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $access_level = $_POST['access_level'];

    // Update admin details in the database
    $result = sql($DBH, "UPDATE tbl_admin SET fullname = ?, email = ?, username = ?, access_level = ? WHERE id = ?", array($fullname, $email, $username, $access_level, $id));

    if ($result) {
        $_SESSION['msg'] = "success";
        header("Location: edit_admin.php?id=" . $id);
        exit();
    } else {
        $_SESSION['error_message'] = "Failed to update admin details";
        header("Location: edit_admin.php?id=" . $id);
        exit();
    }
} else {
    // Redirect to the edit_admin.php page if the request method is not POST
    header("Location: edit_admin.php");
    exit();
}
?>
