<?php
include("../classes/manager.class.php");
$m = new Manager();
$supplierId = $_REQUEST['id']; // Supplier ID
$selectedPurchaseId = isset($_REQUEST['selected_id']) ? $_REQUEST['selected_id'] : null; // Selected purchase reference (if editing)

if ($supplierId) {
    $filter = " WHERE supplier_id = '$supplierId' AND supplier_payment_type = 'supplier' AND payment_status = 'pending'";
    $qry = Purpayment::GetPurpaymentList($filter);

    echo '<option value="">Select Payment Reference</option>'; // Blank default option

    if ($qry) {
        foreach ($qry as $row) {
            $purchaseFilter = " WHERE id = '$row->purchase_reference_code'";
            $purchaseReferenceCode = PurchaseSetup::GetPurchaseSetupList($purchaseFilter);
            if (is_array($purchaseReferenceCode) && count($purchaseReferenceCode) > 0) {
                $purchaseReferenceCode = $purchaseReferenceCode[0]; // Get the first result
            }

            if ($row->status == 'active' || $row->id == $selectedPurchaseId) {
                echo '<option value="' . htmlspecialchars($row->id) . '"';
                if ($row->id == $selectedPurchaseId) {
                    echo ' selected';
                }
                echo '>' . htmlspecialchars($purchaseReferenceCode->purchase_invoice_no) . ' | ' 
                    . htmlspecialchars($row->amount) . ' | ' 
                    . htmlspecialchars($row->withholding_tax_amount) . '</option>';
            }
        }
    }
}
?>
