<?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 status != 'Paid'";
    $qry = PurchaseSetup::GetPurchaseSetupList($filter);
	print_r($qry);

    echo '<option></option>'; // Blank option

    if ($qry) {
        foreach ($qry as $row) {
            // Include all pending purchases for the selected supplier
            if ($row->status !== 'Paid' || $row->id == $selectedPurchaseId) {
                echo '<option value="' . htmlspecialchars($row->id) . '"';
                // Mark the saved purchase reference as selected
                if ($row->id == $selectedPurchaseId) {
                    echo ' selected';
                }
                echo '>' . htmlspecialchars($row->purchase_invoice_no) . ' | ' 
                    . htmlspecialchars(date('d-m-Y', strtotime($row->purchase_invoice_date))) . ' | ' 
                    . htmlspecialchars($row->total_amount) . '</option>';
            }
        }
    }
}
?>
