<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>

    <!-- Summary Cards -->
    <div class="row mb-4 no-print">
        <?php
        $total_products = count($rows);
        $low_stock      = 0;
        $out_of_stock   = 0;
        foreach($rows as $r) {
            $balance = $r->total_purchased - $r->total_sold;
            if ($balance <= 0) $out_of_stock++;
            elseif ($balance <= 5) $low_stock++;
        }
        ?>
        <div class="col-md-3">
            <div class="stat-card" style="border-left-color:#2563eb;">
                <div class="stat-icon" style="background:#eff6ff;color:#2563eb;"><i class="fa-solid fa-box"></i></div>
                <div><div class="stat-label">Total Products</div><div class="stat-value"><?= $total_products ?></div></div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card" style="border-left-color:#16a34a;">
                <div class="stat-icon" style="background:#f0fdf4;color:#16a34a;"><i class="fa-solid fa-circle-check"></i></div>
                <div><div class="stat-label">In Stock</div><div class="stat-value"><?= $total_products - $out_of_stock ?></div></div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card" style="border-left-color:#d97706;">
                <div class="stat-icon" style="background:#fffbeb;color:#d97706;"><i class="fa-solid fa-triangle-exclamation"></i></div>
                <div><div class="stat-label">Low Stock (≤5)</div><div class="stat-value"><?= $low_stock ?></div></div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="stat-card" style="border-left-color:#dc2626;">
                <div class="stat-icon" style="background:#fef2f2;color:#dc2626;"><i class="fa-solid fa-circle-xmark"></i></div>
                <div><div class="stat-label">Out of Stock</div><div class="stat-value"><?= $out_of_stock ?></div></div>
            </div>
        </div>
    </div>

    <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;">
            <thead>
                <tr>
                    <th width="40">#</th>
                    <th>Product Name</th>
                    <th>Category</th>
                    <th>Unit</th>
                    <th class="text-end">Purchased</th>
                    <th class="text-end">Sold</th>
                    <th class="text-end">Balance Stock</th>
                    <th class="text-end">Sale Price (₹)</th>
                    <th class="text-center">Stock Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($rows)): $i=1; foreach($rows as $row):
                    $balance = $row->total_purchased - $row->total_sold;
                    if ($balance <= 0) { $sbg = '#fee2e2'; $sc = '#dc2626'; $sl = 'Out of Stock'; }
                    elseif ($balance <= 5) { $sbg = '#fef3c7'; $sc = '#d97706'; $sl = 'Low Stock'; }
                    else { $sbg = '#dcfce7'; $sc = '#16a34a'; $sl = 'In Stock'; }
                ?>
                <tr>
                    <td><?= $i++ ?></td>
                    <td><strong><?= htmlspecialchars($row->product_name) ?></strong></td>
                    <td><?= htmlspecialchars($row->category_name ?: '—') ?></td>
                    <td><?= htmlspecialchars($row->unit_name ?: '—') ?></td>
                    <td class="text-end"><?= (int)($row->total_purchased) ?></td>
                    <td class="text-end"><?= (int)($row->total_sold) ?></td>
                    <td class="text-end fw-bold"><?= (int)($balance) ?></td>
                    <td class="text-end">₹<?= (int)($row->selling_price) ?></td>
                    <td class="text-center">
                        <span style="padding:3px 10px;font-size:11px;font-weight:700;">
                            <?= $sl ?>
                        </span>
                    </td>
                </tr>
                <?php endforeach; else: ?>
                <tr><td colspan="9" class="text-center py-4 text-muted">No products found.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>