<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Inventory',
    'workspaceTitle'        => 'Inventory Reports',
    'workspaceDesc'         => 'KPIs, low stock alerts, and recent adjustments for the selected period.',
]) ?>

<?php $kpis = $report['kpis'] ?? []; ?>
<?php if ($kpis !== []): ?>
<section class="enterprise-kpi-grid mb-4">
    <?php foreach ($kpis as $kpi): ?>
        <article class="enterprise-kpi-card">
            <div class="enterprise-kpi-header">
                <span class="enterprise-kpi-label"><?= esc($kpi['label']) ?></span>
                <span class="enterprise-kpi-icon"><i class="fa-solid <?= esc($kpi['icon']) ?>"></i></span>
            </div>
            <div class="enterprise-kpi-value"><?= esc($kpi['value']) ?></div>
            <div class="enterprise-kpi-footer">
                <span class="enterprise-kpi-badge enterprise-kpi-badge--<?= esc($kpi['badgeType']) ?>">
                    <i class="fa-solid <?= $kpi['badgeType'] === 'positive' ? 'fa-arrow-trend-up' : 'fa-arrow-trend-down' ?>"></i>
                    <?= esc($kpi['badge']) ?>
                </span>
                <span class="enterprise-kpi-dept"><?= esc($kpi['dept']) ?></span>
            </div>
        </article>
    <?php endforeach; ?>
</section>
<?php endif; ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Filters</h2>
        <p class="enterprise-data-panel__subtitle">Period and warehouse scope for the inventory rollup.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('inventory/reports') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">From</label>
                    <input type="date" name="date_from" class="form-control form-control-sm"
                           value="<?= esc($filters['date_from']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To</label>
                    <input type="date" name="date_to" class="form-control form-control-sm"
                           value="<?= esc($filters['date_to']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Warehouse</label>
                    <select name="warehouse_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($warehouses as $w): ?>
                            <option value="<?= (int) $w['id'] ?>" <?= (string) ($filters['warehouse_id'] ?? '') === (string) $w['id'] ? 'selected' : '' ?>>
                                <?= esc($w['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply Filters</button>
                    <?= view('Modules\Reports\Views\partials\export_buttons', [
                        'actionPath' => 'inventory/reports',
                        'query'      => $filters,
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="enterprise-data-panel h-100">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Low Stock Items</h2>
                <p class="enterprise-data-panel__subtitle">Products at or below reorder level.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>Warehouse</th>
                                <th class="text-end">Qty</th>
                                <th class="text-end">Reorder</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['lowStockRows'] ?? [] as $row): ?>
                                <tr>
                                    <td>
                                        <?= esc($row['product_name'] ?: '—') ?>
                                        <div class="text-muted small"><code><?= esc($row['sku'] ?? '') ?></code></div>
                                    </td>
                                    <td><?= esc($row['warehouse_name'] ?: '—') ?></td>
                                    <td class="text-end"><?= number_format((float) $row['quantity'], 3) ?></td>
                                    <td class="text-end"><?= number_format((float) $row['reorder_level'], 3) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['lowStockRows'])): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No low stock items.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="enterprise-data-panel h-100">
            <div class="enterprise-data-panel__head">
                <h2 class="enterprise-data-panel__title">Recent Adjustments</h2>
                <p class="enterprise-data-panel__subtitle">Latest stock adjustments in the selected period.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Number</th>
                                <th>Product</th>
                                <th class="text-end">Delta</th>
                                <th>Date</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['recentAdjustments'] ?? [] as $row): ?>
                                <tr>
                                    <td><code><?= esc($row['number']) ?></code></td>
                                    <td class="small"><?= esc($row['product_name'] ?: '—') ?></td>
                                    <td class="text-end fw-semibold"><?= number_format((float) $row['delta'], 3) ?></td>
                                    <td><?= esc($row['adjustment_date']) ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['recentAdjustments'])): ?>
                                <tr><td colspan="4" class="text-center text-muted py-4">No adjustments in period.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
