<?php
/**
 * @var array $products
 * @var array $categories
 * @var int|null $categoryId
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-boxes me-2 text-gold"></i>Inventory Report</h5>
</div>

<div class="glass-card p-3 mb-4">
    <form class="row g-3 align-items-end" method="GET" action="<?= url('reports/inventory') ?>">
        <div class="col-md-3">
            <label class="form-label">Category</label>
            <select name="category_id" class="form-select">
                <option value="">All Categories</option>
                <?php foreach ($categories as $cat): ?>
                    <option value="<?= $cat->id ?>" <?= $categoryId == $cat->id ? 'selected' : '' ?>><?= e($cat->name) ?></option>
                <?php endforeach; ?>
            </select>
        </div>
        <div class="col-md-4">
            <button type="submit" class="btn btn-gold"><i class="fas fa-search me-1"></i>Filter</button>
            <a href="<?= url('reports/export/inventory?category_id=' . ($categoryId ?? '')) ?>" class="btn btn-outline-gold ms-2">
                <i class="fas fa-download me-1"></i>CSV
            </a>
        </div>
    </form>
</div>

<?php
$totalValue = array_sum(array_column($products, 'total_value'));
$totalItems = array_sum(array_column($products, 'quantity'));
?>
<div class="row g-4 mb-4">
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(200,164,78,0.15);border-color:rgba(200,164,78,0.3);color:var(--gold-primary);"><i class="fas fa-box"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Products</span>
                <span class="stats-value"><?= count($products) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(13,202,240,0.15);border-color:rgba(13,202,240,0.3);color:#0dcaf0;"><i class="fas fa-cubes"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Quantity</span>
                <span class="stats-value"><?= $totalItems ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(25,135,84,0.15);border-color:rgba(25,135,84,0.3);color:#198754;"><i class="fas fa-dollar-sign"></i></div>
            <div class="stats-info">
                <span class="stats-label">Total Value</span>
                <span class="stats-value"><?= currency($totalValue) ?></span>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="glass-card stats-card">
            <div class="stats-icon" style="background:rgba(220,53,69,0.15);border-color:rgba(220,53,69,0.3);color:#dc3545;"><i class="fas fa-exclamation-triangle"></i></div>
            <div class="stats-info">
                <span class="stats-label">Low Stock</span>
                <span class="stats-value"><?= count(array_filter($products, fn($p) => $p->quantity <= $p->reorder_level)) ?></span>
            </div>
        </div>
    </div>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">Inventory List</h6>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-dark table-hover mb-0">
            <thead>
                <tr>
                    <th>Product</th>
                    <th>Category</th>
                    <th>SKU</th>
                    <th class="text-center">Quantity</th>
                    <th class="text-center">Reorder Level</th>
                    <th class="text-end">Unit Cost</th>
                    <th class="text-end">Total Value</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($products)): ?>
                    <tr><td colspan="7" class="text-center text-muted py-4">No products found</td></tr>
                <?php else: ?>
                    <?php foreach ($products as $p): ?>
                        <?php $isLow = $p->quantity <= $p->reorder_level; ?>
                        <tr>
                            <td><?= e($p->name) ?> <?= $isLow ? '<i class="fas fa-exclamation-triangle text-danger ms-1" title="Low Stock"></i>' : '' ?></td>
                            <td><?= e($p->category_name ?? '-') ?></td>
                            <td><?= e($p->sku ?? '-') ?></td>
                            <td class="text-center"><span class="badge bg-<?= $isLow ? 'danger' : 'success' ?>"><?= $p->quantity ?></span></td>
                            <td class="text-center"><?= $p->reorder_level ?></td>
                            <td class="text-end"><?= currency($p->unit_cost ?? 0) ?></td>
                            <td class="text-end"><?= currency($p->total_value ?? 0) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
