<?php
$page_title = 'Edit SRM Data';
require_once('includes/load.php');
page_require_level(4);

// Periksa apakah ID data yang akan diubah telah diberikan melalui parameter URL
if (isset($_GET['id'])) {
    $srm_id = (int)$_GET['id'];

    // Periksa apakah data SRM dengan ID yang diberikan ada dalam database
    $srm = find_by_id('srm_data', $srm_id);

    if (!$srm) {
        $session->msg("d", "Data SRM tidak ditemukan.");
        redirect('srm.php');
    }
} else {
    $session->msg("d", "ID data SRM tidak valid.");
    redirect('srm.php');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $vendor_name = $db->escape($_POST['vendor_name']);
    $description = $db->escape($_POST['description']);
    $quantity_receivable = (int) $_POST['quantity_receivable'];
    $quantity_unreceivable = (int) $_POST['quantity_unreceivable'];

    // Lakukan validasi data di sini jika diperlukan

    // Lakukan query SQL UPDATE untuk mengubah data di database
    $sql = "UPDATE srm_data SET vendor_name = '$vendor_name', description = '$description', 
            quantity_receivable = $quantity_receivable, quantity_unreceivable = $quantity_unreceivable WHERE id = $srm_id";

    if ($db->query($sql)) {
        $session->msg("s", "Data SRM berhasil diperbarui.");
        redirect('srm.php');
    } else {
        $session->msg("d", "Gagal memperbarui data SRM.");
        redirect('edit_srm.php?id=' . $srm_id);
    }
}

include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Edit SRM Data</h3>
            </div>
            <div class="panel-body">
                <form action="edit_srm.php?id=<?php echo $srm_id; ?>" method="post">
                    <div class="form-group">
                        <label for="vendor_name">Nama Vendor:</label>
                        <input type="text" class="form-control" name="vendor_name" id="vendor_name" value="<?php echo $srm['vendor_name']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="description">Deskripsi Material:</label>
                        <textarea class="form-control" name="description" id="description" rows="3" required><?php echo $srm['description']; ?></textarea>
                    </div>
                    <div class="form-group">
                        <label for="quantity_receivable">Quantity Receivable:</label>
                        <input type="number" class="form-control" name="quantity_receivable" id="quantity_receivable" value="<?php echo $srm['quantity_receivable']; ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="quantity_unreceivable">Quantity Unreceivable:</label>
                        <input type="number" class="form-control" name="quantity_unreceivable" id="quantity_unreceivable" value="<?php echo $srm['quantity_unreceivable']; ?>" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>
