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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Procurement',
    'workspaceTitle'        => 'Procurement Reports',
    'workspaceDesc'         => 'Counts and totals across purchasing documents 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 supplier scope for the procurement rollup.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('procurement/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">Supplier</label>
                    <select name="supplier_id" class="form-select form-select-sm">
                        <option value="">Any</option>
                        <?php foreach ($suppliers as $s): ?>
                            <option value="<?= $s['id'] ?>" <?= (string) ($filters['supplier_id'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                                <?= esc($s['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' => 'procurement/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">Purchase Order Status</h2>
                <p class="enterprise-data-panel__subtitle">Status mix for POs in the selected period.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Status</th>
                                <th class="text-end">Count</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['poStatusMix']['rows'] ?? [] as $row): ?>
                                <tr>
                                    <td><?= esc(ucwords(str_replace('_', ' ', $row['status']))) ?></td>
                                    <td class="text-end"><?= (int) $row['total'] ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['poStatusMix']['rows'])): ?>
                                <tr><td colspan="2" class="text-center text-muted py-4">No records.</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">Vendor Bill Status</h2>
                <p class="enterprise-data-panel__subtitle">Status mix for vendor bills in the selected period.</p>
            </div>
            <div class="enterprise-data-panel__body">
                <div class="table-responsive">
                    <table class="enterprise-data-table">
                        <thead>
                            <tr>
                                <th>Status</th>
                                <th class="text-end">Count</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($report['billStatusMix']['rows'] ?? [] as $row): ?>
                                <tr>
                                    <td><?= esc(ucwords(str_replace('_', ' ', $row['status']))) ?></td>
                                    <td class="text-end"><?= (int) $row['total'] ?></td>
                                </tr>
                            <?php endforeach; ?>
                            <?php if (empty($report['billStatusMix']['rows'])): ?>
                                <tr><td colspan="2" class="text-center text-muted py-4">No records.</td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

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