<?php
require_once('includes/load.php');
page_require_level(2);

if (isset($_POST['update_request'])) {
    $request_id = $_POST['request_id'];
    $new_due_date = $_POST['due_date'];
    
    // Tambahkan validasi input lainnya jika diperlukan

    // Update data permintaan pembelian
    $sql = "UPDATE purchase_requests SET due_date = '{$new_due_date}' WHERE id = {$request_id}";
    $db->query($sql);

    $session->msg('s', "Request has been updated!");
    redirect('purchase_requests.php');
}

if (isset($_GET['id'])) {
    $request_id = $_GET['id'];
    $request = find_by_id('purchase_requests', $request_id);
} else {
    $session->msg("d", "Missing request ID.");
    redirect('purchase_requests.php');
}

$page_title = 'Edit Purchase Request';
include_once('layouts/header.php');
?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h4 class="panel-title">Edit Purchase Request</h4>
            </div>
            <div class="panel-body">
                <form method="post" action="edit_purchase_request.php">
                    <input type="hidden" name="request_id" value="<?php echo $request['id']; ?>">
                    <div class="form-group">
                        <label for="due_date">Due Date</label>
                        <input type="date" class="form-control" name="due_date" value="<?php echo $request['due_date']; ?>">
                    </div>
                    <!-- Tambahkan input lainnya sesuai kebutuhan -->
                    <button type="submit" name="update_request" class="btn btn-primary">Update</button>
                </form>
            </div>
        </div>
    </div>
</div>

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