<?php
// Core config and database connection
require_once dirname(__DIR__, 3) . '/dripend/config/config.php';
require_once dirname(__DIR__, 3) . '/dripend/includes/db.php';

// Dynamically capture the current category to preserve routing
$current_cat = $_GET['cat'] ?? 'stores';
?>
<div class="container-fluid py-4 px-4">
    <div class="d-flex justify-content-between align-items-center mb-4 pb-3 border-bottom">
        <div>
            <h2 class="h4 text-dark fw-bold m-0"><i class="fa-solid fa-boxes-packing text-primary me-2"></i>Opening Inventory Allocation</h2>
            <p class="text-muted small m-0 mt-1">Allocate existing physical stock to autonomous database locations (Stores, Departments, or Dispensaries).</p>
        </div>
        <!-- CRITICAL FIX: Dynamic routing parameter to return to ledger safely -->
        <a href="systemSetup.php?cat=<?= htmlspecialchars($current_cat) ?>&page=inventory/listOpeningAllocations.php" class="btn btn-light border fw-bold text-secondary shadow-sm">
            <i class="fa-solid fa-list me-2"></i> Allocation Ledger
        </a>
    </div>

    <div id="allocationResponseMessage" class="d-none alert shadow-sm mb-4 rounded-3 border-0"></div>

    <form id="openingAllocationForm" class="bg-white p-4 rounded-3 border shadow-sm" novalidate>
        
        <h6 class="fw-bold text-secondary mb-3"><i class="fa-solid fa-location-dot me-2"></i>1. Destination Context</h6>
        <div class="row g-3 mb-4 bg-light-subtle p-3 rounded border">
            <div class="col-md-3">
                <label class="form-label small fw-bold text-uppercase">Allocation Date</label>
                <input type="date" name="allocation_date" class="form-control" value="<?= date('Y-m-d') ?>" required>
            </div>
            <div class="col-md-5">
                <label class="form-label small fw-bold text-uppercase">Inventory Location Type <span class="text-danger">*</span></label>
                <div class="d-flex gap-3 mt-1">
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="location_type" id="loc_store" value="Store" checked onchange="window.fetchLocations()">
                        <label class="form-check-label fw-bold" for="loc_store">Store Hierarchy</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="location_type" id="loc_dept" value="Department" onchange="window.fetchLocations()">
                        <label class="form-check-label fw-bold" for="loc_dept">Department</label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="location_type" id="loc_disp" value="Dispensary" onchange="window.fetchLocations()">
                        <label class="form-check-label fw-bold" for="loc_disp">Dispensary</label>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <label class="form-label small fw-bold text-uppercase">Select Destination <span class="text-danger">*</span></label>
                <select name="location_id" id="dynamic_location_select" class="form-select border-primary" required>
                    <option value="" selected disabled>-- Fetching Database Nodes --</option>
                </select>
            </div>
            <div class="col-12 mt-2">
                <input type="text" name="remarks" class="form-control" placeholder="Optional allocation remarks or reference notes...">
            </div>
        </div>

        <h6 class="fw-bold text-secondary mb-3"><i class="fa-solid fa-cart-flatbed me-2"></i>2. Inventory Payload</h6>
        <div class="table-responsive mb-4">
            <table class="table table-bordered align-middle table-sm" id="allocationItemsTable">
                <thead class="table-dark small text-center">
                    <tr>
                        <th style="width: 25%">Product Master</th>
                        <th style="width: 20%">Brand / Variant</th>
                        <th style="width: 15%">Batch & Expiry</th>
                        <th style="width: 10%">Quantity</th>
                        <th style="width: 12%">Unit Cost (GH₵)</th>
                        <th style="width: 12%">Selling (GH₵)</th>
                        <th style="width: 6%">Action</th>
                    </tr>
                </thead>
                <tbody id="allocationItemsBody">
                    </tbody>
            </table>
            <button type="button" class="btn btn-sm btn-outline-primary fw-bold" onclick="window.addAllocationRow()">
                <i class="fa-solid fa-plus me-1"></i> Add Inventory Line
            </button>
        </div>

        <div class="row g-3 mb-4 border-top pt-4">
            <div class="col-md-3">
                <div class="p-3 bg-light rounded border text-center">
                    <span class="d-block small text-muted text-uppercase fw-bold">Total Items</span>
                    <h4 class="m-0 text-dark fw-bold" id="summary_items">0</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="p-3 bg-light rounded border text-center">
                    <span class="d-block small text-muted text-uppercase fw-bold">Total Quantity</span>
                    <h4 class="m-0 text-info fw-bold" id="summary_qty">0</h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="p-3 bg-light rounded border text-center">
                    <span class="d-block small text-muted text-uppercase fw-bold">Total Capital Cost</span>
                    <h4 class="m-0 text-danger fw-bold">GH₵ <span id="summary_cost">0.00</span></h4>
                </div>
            </div>
            <div class="col-md-3">
                <div class="p-3 bg-light rounded border text-center">
                    <span class="d-block small text-muted text-uppercase fw-bold">Projected Revenue</span>
                    <h4 class="m-0 text-success fw-bold">GH₵ <span id="summary_revenue">0.00</span></h4>
                </div>
            </div>
        </div>

        <div class="d-flex justify-content-end pt-3 border-top">
            <button type="submit" id="btnPostAllocation" class="btn btn-success px-5 fw-bold shadow-sm">
                <i class="fa-solid fa-check-double me-2"></i> Post Opening Allocation
            </button>
        </div>
    </form>
</div>

<!-- Scripts loaded strictly after HTML -->
<script src="<?php echo BASE_URL; ?>application/assets/js/openingAllocation.js?v=<?= time() ?>"></script>