<?php require_once dirname(__DIR__, 2) . '/dripend/config/config.php'; ?>
<!DOCTYPE html><html lang="en"><head><title>Create PO | RiMax</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css"><style>body{background:#f8fafc;} .main-content{margin-left:300px;padding:20px;}</style></head>
<body>
    <?php include(dirname(__DIR__, 2) . '/dripend/statpages/sidebar.php'); ?>
    <div class="main-content"><div class="container-fluid py-2">
        <div class="d-flex justify-content-between mb-4 border-bottom pb-3"><h2 class="h4 fw-bold m-0"><i class="fa-solid fa-file-contract text-primary me-2"></i>Create Purchase Order</h2><a href="listPurchaseOrder.php" class="btn btn-light border fw-bold text-secondary">View Purchase Orders</a></div>
        <div id="poMessage"></div>
        
        <form id="poForm" class="bg-white p-4 rounded-3 border shadow-sm" autocomplete="off">
            <h6 class="fw-bold text-secondary mb-3">1. Sourcing Method</h6>
            <div class="mb-4 bg-light p-3 rounded border d-flex gap-4">
                <div class="form-check"><input class="form-check-input" type="radio" name="po_type" id="t_pr" value="pr" checked><label class="form-check-label fw-bold" for="t_pr">Convert from Approved Requisition</label></div>
                <div class="form-check"><input class="form-check-input" type="radio" name="po_type" id="t_dir" value="direct"><label class="form-check-label fw-bold" for="t_dir">Direct / Contract Purchase</label></div>
            </div>

            <h6 class="fw-bold text-secondary mb-3">2. Commercial Details</h6>
            <div class="row g-3 mb-4 bg-light-subtle p-3 rounded border">
                <div class="col-md-3" id="pr_selector_div"><label class="small fw-bold text-uppercase">Requisition *</label><select name="requisition_id" id="po_requisition_id" class="form-select border-primary"></select></div>
                <div class="col-md-3"><label class="small fw-bold text-uppercase">Supplier *</label><select name="supplier_id" id="po_supplier_id" class="form-select" required></select></div>
                <div class="col-md-3"><label class="small fw-bold text-uppercase">Delivery Store *</label><select name="store_id" id="po_store_id" class="form-select" required></select></div>
                <div class="col-md-3"><label class="small fw-bold text-uppercase">Expected Delivery *</label><input type="date" name="expected_delivery_date" class="form-control" required></div>
            </div>

            <h6 class="fw-bold text-secondary mb-3">3. Line Items</h6>
            <div class="table-responsive mb-4"><table class="table table-bordered align-middle table-sm"><thead class="table-dark small text-center"><tr><th class="text-start">Product Catalog Item</th><th>Qty</th><th>Unit Price</th><th>Line Total</th><th>Act</th></tr></thead><tbody id="poItemsBody"></tbody></table><button type="button" id="btnAddRow" class="btn btn-sm btn-outline-primary fw-bold" onclick="window.addPoRow()" disabled><i class="fa-solid fa-plus"></i> Add Line</button></div>

            <div class="row justify-content-end mb-4 border-top pt-3">
                <div class="col-md-4 bg-light p-3 rounded border text-end">
                    <div class="d-flex justify-content-between mb-2"><span class="small fw-bold text-muted text-uppercase">Subtotal:</span><strong>GH₵ <span id="lbl_subtotal">0.00</span></strong><input type="hidden" name="subtotal" id="po_subtotal" value="0"></div>
                    <div class="d-flex justify-content-between align-items-center mb-2"><span class="small fw-bold text-muted text-uppercase">Tax Amount:</span><input type="number" step="0.01" name="tax_amount" id="po_tax" class="form-control form-control-sm w-50 text-end" value="0" oninput="window.calcPoTotals()"></div>
                    <div class="d-flex justify-content-between align-items-center mb-3"><span class="small fw-bold text-muted text-uppercase">Discount:</span><input type="number" step="0.01" name="discount_amount" id="po_disc" class="form-control form-control-sm w-50 text-end" value="0" oninput="window.calcPoTotals()"></div>
                    <div class="d-flex justify-content-between border-top pt-2"><span class="fw-bold">Grand Total:</span><h4 class="text-danger fw-bold m-0">GH₵ <span id="lbl_grand">0.00</span></h4><input type="hidden" name="grand_total" id="po_grand" value="0"></div>
                </div>
            </div>
            <div class="text-end"><button type="submit" id="btnSubmitPo" class="btn btn-success px-5 fw-bold"><i class="fa-solid fa-paper-plane me-2"></i> Route for Approval</button></div>
        </form>
    </div></div>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script><script src="<?php echo BASE_URL; ?>application/assets/js/purchaseOrder.js?v=<?= time(); ?>"></script>
</body></html>