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

// Fungsi untuk mendapatkan semua permintaan pembelian
function get_purchase_requests() {
    global $db;

    $sql = "SELECT * FROM purchase_requests";
    $result = $db->query($sql);

    if (!$result) {
        die("Query failed: " . $db->error);
    }

    $requests = [];
    while ($row = $db->fetch_assoc($result)) {
        $requests[] = $row;
    }

    return $requests;
}

// Fungsi untuk mengupdate status permintaan pembelian
function update_purchase_request_status($id, $status) {
    global $db;

    $id = intval($id);
    $status = $db->escape($status);

    $sql = "UPDATE purchase_requests SET status = '{$status}' WHERE id = {$id}";

    return $db->query($sql);
}

// Proses untuk menyetujui atau menolak permintaan pembelian
if (isset($_POST['update_status'])) {
    $id = isset($_POST['id']) ? intval($_POST['id']) : 0;
    $status = isset($_POST['status']) ? $db->escape($_POST['status']) : '';

    if (update_purchase_request_status($id, $status)) {
        $session->msg('s', "Purchase request status updated successfully.");
        redirect('approval_purchase.php', false);
    } else {
        $session->msg('d', "Failed to update purchase request status.");
        redirect('approval_purchase.php', false);
    }
}

// Ambil data permintaan pembelian
$requests = get_purchase_requests();

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

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row mt-3">
    <div class="col-md-12">
        <div class="panel panel-info">
            <div class="panel-heading">Purchase Requests Approval</div>
            <div class="panel-body">
                <table class="table table-bordered">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Part Number</th>
                            <th>Product Name</th>
                            <th>Quantity Needed</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($requests as $request) : ?>
                            <tr>
                                <td><?php echo remove_junk($request['id']); ?></td>
                                <td><?php echo remove_junk($request['part_number']); ?></td>
                                <td><?php echo remove_junk($request['product_name']); ?></td>
                                <td><?php echo remove_junk($request['quantity_needed']); ?></td>
                                <td><?php echo $request['status'] ? remove_junk($request['status']) : 'Pending'; ?></td>
                                <td>
                                    <?php if ($request['status'] === NULL) : ?>
                                        <form action="approval_purchase.php" method="post" style="display:inline;">
                                            <input type="hidden" name="id" value="<?php echo remove_junk($request['id']); ?>">
                                            <input type="hidden" name="status" value="Approved">
                                            <button type="submit" name="update_status" class="btn btn-success">Approve</button>
                                        </form>
                                        <form action="approval_purchase.php" method="post" style="display:inline;">
                                            <input type="hidden" name="id" value="<?php echo remove_junk($request['id']); ?>">
                                            <input type="hidden" name="status" value="Rejected">
                                            <button type="submit" name="update_status" class="btn btn-danger">Reject</button>
                                        </form>
                                    <?php else: ?>
                                        <span class="badge <?php echo $request['status'] === 'Approved' ? 'badge-success' : 'badge-danger'; ?>">
                                            <?php echo remove_junk($request['status']); ?>
                                        </span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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