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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Completion Reports',
    'workspaceTitle'        => 'Completion Reports',
    'workspaceDesc'         => 'Review completed work orders and closeout summaries.',
]) ?>

<section class="enterprise-kpi-grid mb-4">
    <article class="enterprise-kpi-card">
        <div class="enterprise-kpi-header">
            <span class="enterprise-kpi-label">Completed in range</span>
            <span class="enterprise-kpi-icon"><i class="fa-solid fa-file-circle-check"></i></span>
        </div>
        <div class="enterprise-kpi-value"><?= (int) $totalCompleted ?></div>
        <div class="enterprise-kpi-footer">
            <span class="enterprise-kpi-badge enterprise-kpi-badge--neutral"><?= esc($filters['from']) ?> → <?= esc($filters['to']) ?></span>
            <span class="enterprise-kpi-dept">Work Orders</span>
        </div>
    </article>
</section>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Completed Work Orders</h2>
        <p class="enterprise-data-panel__subtitle">Filter by completion date range.</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('operations/work-orders/completion-reports') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">From</label>
                    <input type="date" name="from" class="form-control form-control-sm" value="<?= esc($filters['from']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">To</label>
                    <input type="date" name="to" class="form-control form-control-sm" value="<?= esc($filters['to']) ?>">
                </div>
                <div>
                    <label class="enterprise-field-label">Search</label>
                    <input type="text" name="q" class="form-control form-control-sm" placeholder="Number, title, location"
                           value="<?= esc($filters['search'] ?? '') ?>">
                </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' => 'operations/work-orders/completion-reports',
                        'query'      => ['from' => $filters['from'], 'to' => $filters['to'], 'q' => $filters['search'] ?? ''],
                    ]) ?>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Number</th>
                        <th>Title</th>
                        <th>Priority</th>
                        <th>Location</th>
                        <th>Completed</th>
                        <th>Staff</th>
                        <th>Equipment</th>
                        <th>Notes</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($orders as $o): ?>
                        <tr>
                            <td>
                                <a href="<?= base_url('operations/work-orders/' . $o['id']) ?>" class="enterprise-data-table__primary">
                                    <code><?= esc($o['number']) ?></code>
                                </a>
                            </td>
                            <td><?= esc($o['title']) ?></td>
                            <td><?= esc(ucfirst($o['priority'])) ?></td>
                            <td><?= esc($o['location'] ?: '—') ?></td>
                            <td><?= esc($o['completed_at'] ?: '—') ?></td>
                            <td><?= (int) ($o['staff_count'] ?? 0) ?></td>
                            <td><?= (int) ($o['equipment_count'] ?? 0) ?></td>
                            <td class="text-muted small"><?= esc($o['completion_notes'] ?: '—') ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($orders) === 0): ?>
                        <tr><td colspan="8" class="text-center text-muted py-5">No completed work orders in this range.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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