<?php
include('session_handler.php');

// Pastikan hanya superadmin boleh akses
if (!checkRoles(['superadmin'])) {
    header('Location: unauthorized.php'); 
    exit;
}

// Semak ID
if (!isset($_GET['id'])) {
    echo "Ralat: ID tidak diberikan.";
    exit;
}
$id = intval($_GET['id']);

// Ambil data jabatan
$stmt = $conn->prepare("SELECT * FROM jabatan WHERE ID = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows == 0) {
    echo "Jabatan tidak dijumpai.";
    exit;
}
$jabatan = $result->fetch_assoc();

// Update bila submit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = $_POST['NamaJbtn'];
    $kod  = $_POST['KodJbtn'];
    $mdfBy = $_SESSION['user_id']; // ikut sistem login
    $mdfDate = date('Y-m-d H:i:s');

    $update = $conn->prepare("UPDATE jabatan SET NamaJbtn=?, KodJbtn=?, MdfDate=?, MdfBy=? WHERE ID=?");
    $update->bind_param("sssii", $nama, $kod, $mdfDate, $mdfBy, $id);

    if ($update->execute()) {
        echo "<script>alert('Maklumat jabatan berjaya dikemaskini'); window.location='senarai_jabatan.php';</script>";
        exit;
    } else {
        echo "<script>alert('Ralat semasa kemaskini');</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<?php include('header.php'); ?>
<body class="skin-blue fixed-layout">
    <div class="preloader">
        <div class="loader"><div class="loader__figure"></div>
        <p class="loader__label">SPNR Surat/Memo</p></div>
    </div>

    <div id="main-wrapper">
        <?php include('topbar.php'); ?>
        <?php include('leftsidebar.php'); ?>

        <div class="page-wrapper">
            <div class="container-fluid">
                <div class="row page-titles">
                    <div class="col-md-5 align-self-center">
                        <h4 class="text-themecolor">Edit Jabatan</h4>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <form method="post">
                                    <div class="mb-3">
                                        <label>Nama Jabatan</label>
                                        <input type="text" name="NamaJbtn" class="form-control"
                                               value="<?php echo htmlspecialchars($jabatan['NamaJbtn']); ?>" required>
                                    </div>
                                    <div class="mb-3">
                                        <label>Kod Jabatan</label>
                                        <input type="text" name="KodJbtn" class="form-control"
                                               value="<?php echo htmlspecialchars($jabatan['KodJbtn']); ?>" required>
                                    </div>
                                    <a href="view_jabatan.php" class="btn btn-warning">Batal</a>
                                    <button type="submit" class="btn btn-success">Simpan</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

        <?php include('footer.php'); ?>
    </div>
    <?php include('jquery.php'); ?>
</body>
</html>