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

if (!checkRoles(['superadmin'])) {
    header('Location: unauthorized.php');
    exit;
}

if (!isset($_GET['id'])) {
    die("ID Bahagian tidak sah.");
}

$id = intval($_GET['id']);

// ambil data lama
$stmt = $conn->prepare("SELECT * FROM bahagian WHERE ID = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$bahagian = $result->fetch_assoc();

if (!$bahagian) {
    die("Data bahagian tidak dijumpai.");
}

// update bila submit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nama = $_POST['NamaBhgn'];
    $kod = $_POST['KodBhgn'];
    $keterangan = $_POST['Keterangan'];
    $mdfBy = $_SESSION['user_id']; // ikut session user login

    $update = $conn->prepare("UPDATE bahagian 
                              SET NamaBhgn=?, KodBhgn=?, Keterangan=?, MdfDate=NOW(), MdfBy=? 
                              WHERE ID=?");
    $update->bind_param("sssii", $nama, $kod, $keterangan, $mdfBy, $id);

    if ($update->execute()) {
        echo "<script>alert('Bahagian berjaya dikemaskini');window.location='view_bahagian.php';</script>";
    } else {
        echo "<script>alert('Ralat semasa kemaskini bahagian');</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">
        <!-- Topbar -->
        <?php include('topbar.php'); ?>
        <!-- Sidebar -->
        <?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">Kemaskini Bahagian</h4>
                    </div>
                    <div class="col-md-7 align-self-center text-end">
                        <ol class="breadcrumb justify-content-end">
                            <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                            <li class="breadcrumb-item"><a href="view_bahagian.php">Senarai Bahagian</a></li>
                            <li class="breadcrumb-item active">Kemaskini Bahagian</li>
                        </ol>
                    </div>
                </div>

                <!-- form card -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <h4 class="card-title">Edit Bahagian</h4>
                                <form method="POST">
                                    <div class="form-group">
                                        <label>Nama Bahagian</label>
                                        <input type="text" name="NamaBhgn" class="form-control" 
                                            value="<?php echo htmlspecialchars($bahagian['NamaBhgn']); ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Kod Bahagian</label>
                                        <input type="text" name="KodBhgn" class="form-control" 
                                            value="<?php echo htmlspecialchars($bahagian['KodBhgn']); ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Keterangan</label>
                                        <textarea name="Keterangan" class="form-control"><?php echo htmlspecialchars($bahagian['Keterangan']); ?></textarea>
                                    </div>
                                    <a href="view_bahagian.php" class="btn btn-secondary">Kembali</a>
                                    <button type="submit" class="btn btn-success">Kemaskini</button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

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

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