<div class="card-box">
    <div class="card-box-header">
        <h5><i class="fa-solid fa-chart-bar" style="color:#d97706;"></i> Stock Report</h5>
        <button onclick="window.print()" class="btn btn-sm no-print" style="background:#f1f5f9;color:#374151;">
            <i class="fa-solid fa-print me-1"></i> Print
        </button>
    </div>

    <!-- Category + Product Filter -->
    <form method="GET" action="<?= site_url('reports/stock') ?>" class="no-print">
        <div class="row align-items-end g-3">
            <div class="col-md-6">
                <label class="form-label">Product Category</label>
                <select name="category_id[]" id="category_id" class="form-select select2" multiple>
                    <?php foreach($categories as $c): ?>
                    <option value="<?php echo $c->id; ?>"
                        <?php if(in_array($c->id, $selected_categories)) echo 'selected'; ?>>
                        <?php echo htmlspecialchars($c->category_name); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-6">
                <label class="form-label">Product</label>
                <select name="product_id[]" id="product_id" class="form-select select2" multiple>
                    <?php foreach($products as $p): ?>
                    <option value="<?php echo $p->id; ?>"
                        <?php if(in_array($p->id, $selected_products)) echo 'selected'; ?>>
                        <?php echo htmlspecialchars($p->product_name); ?>
                    </option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>
        <div class="row g-3 mb-4 mt-1">
            <div class="col-md-2">
                <button type="submit" class="btn btn-primary w-100"><i class="fa-solid fa-search me-1"></i>Search</button>
            </div>
            <div class="col-md-2">
                <a href="<?= site_url('reports/stock') ?>" class="btn w-100" style="background:#f1f5f9;color:#374151;">Reset</a>
            </div>
        </div>
    </form>

    <script>
        $(document).ready(function(){
            $('#category_id, #product_id').on('select2:open', function(){
                setTimeout(function(){
                    $('.select2-search__field').attr('placeholder', 'Search...');
                }, 0);
            });
        });
    </script>

    <div class="print-title" style="display:none;">Stock Report</div>
    <div class="table-responsive">
        <table class="table table-bordered table-hover dt-table mb-0" style="font-size:13px;" data-empty-text="No products found.">
            <thead>
                <tr>
                    <th>Product Category</th>
                    <th>Product Name</th>
                    <th class="text-end">Stock</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($rows)): foreach($rows as $row):
                    $balance = $row->total_purchased - $row->total_sold;
                ?>
                <tr>
                    <td><?= htmlspecialchars($row->category_name ?: '—') ?></td>
                    <td><strong><?= htmlspecialchars($row->product_name) ?></strong></td>
                    <td class="text-end fw-bold"><?= (int)($balance) ?></td>
                </tr>
                <?php endforeach; endif; ?>
            </tbody>
        </table>
    </div>
</div>