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

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

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

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

// Dapatkan maklumat surat
$stmt = $conn->prepare("SELECT * FROM surat WHERE ID = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo "Surat tidak dijumpai.";
    exit;
}

$surat = $result->fetch_assoc();

// Jika POST (kemaskini data)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $tarikh = $_POST['Tarikh'];
    $tajuk = $_POST['Tajuk'];
    $lokasi = $_POST['Lokasi'];
    $mdfby = $_SESSION['user_id'];

    $update = $conn->prepare("UPDATE surat SET Tarikh=?, Tajuk=?, Lokasi=?, MdfBy=? WHERE ID=?");
    $update->bind_param("sssii", $tarikh, $tajuk, $lokasi, $mdfby, $id);

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

<!DOCTYPE html>
<html lang="en">
<!-- header part -->
<?php include('header.php'); ?>
<!-- end header -->
<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 header -->
        <?php include('topbar.php'); ?>
        <!-- Left Sidebar -->
        <?php include('leftsidebar.php'); ?>

        <!-- Page wrapper -->
        <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 Maklumat Surat</h4>
                    </div>
                    <div class="col-md-7 align-self-center text-end">
                        <div class="d-flex justify-content-end align-items-center">
                            <ol class="breadcrumb justify-content-end">
                                <li class="breadcrumb-item"><a href="dashboard.php">Home</a></li>
                                <li class="breadcrumb-item active">Kemaskini Surat/Memo</li>
                            </ol>
                        </div>
                    </div>
                </div>

                <!-- Start form content -->
                <div class="row">
                    <div class="col-12">
                        <div class="card">
                            <div class="card-body">
                                <form method="post">
                                    <div class="mb-3">
                                        <label>No. Rujukan</label>
                                        <input type="text" class="form-control" value="<?php echo htmlspecialchars($surat['no_rujukan']); ?>" readonly>
                                    </div>

                                    <div class="mb-3">
                                        <label>Tarikh</label>
                                        <input type="date" name="Tarikh" class="form-control" value="<?php echo $surat['Tarikh']; ?>" required>
                                    </div>

                                    <div class="mb-3">
                                        <label>Tajuk</label>
                                        <input type="text" name="Tajuk" class="form-control" value="<?php echo htmlspecialchars($surat['Tajuk']); ?>" required>
                                    </div>

                                    <div class="mb-3">
                                        <label>Lokasi</label>
                                        <input type="text" name="Lokasi" class="form-control" value="<?php echo htmlspecialchars($surat['Lokasi']); ?>" required>
                                    </div>

                                    <a href="view_norujukan.php" class="btn btn-warning">Batal</a>
                                    <button type="submit" class="btn btn-success">Simpan</button>                          
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- End form content -->

            </div>
        </div>

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

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