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

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

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

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

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

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

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

    $update = $conn->prepare("UPDATE subkategori 
                              SET Sub_kategori=?, Keterangan=?, MdfDate=NOW(), MdfBy=? 
                              WHERE ID=?");
    $update->bind_param("ssii", $sub_kategori, $keterangan, $mdfBy, $id);

    if ($update->execute()) {
        echo "<script>alert('Subkategori berjaya dikemaskini');window.location='view_sub.php';</script>";
    } else {
        echo "<script>alert('Ralat semasa kemaskini subkategori');</script>";
    }
    $update->close();
}
?>

<!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 Subkategori</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_sub.php">Senarai Subkategori</a></li>
                            <li class="breadcrumb-item active">Kemaskini Subkategori</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 Subkategori</h4>
                                <form method="POST">
                                    <div class="form-group">
                                        <label>Kod Subkategori</label>
                                        <input type="text" name="Sub_kategori" class="form-control" 
                                            value="<?php echo htmlspecialchars($sub['Sub_kategori']); ?>" required>
                                    </div>
                                    <div class="form-group">
                                        <label>Keterangan</label>
                                        <textarea name="Keterangan" class="form-control"><?php echo htmlspecialchars($sub['Keterangan']); ?></textarea>
                                    </div>
                                    <a href="view_sub.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>