<section class="page-header">
    <h2>Relatórios operacionais</h2>
    <p>Filtre e acompanhe a produtividade da operação por período, setor, categoria e SLA.</p>
</section>

<?php $relatoriosFilterAction = base_url('/admin/relatorios'); ?>
<form method="GET" action="<?= e($relatoriosFilterAction) ?>" class="card filter-grid">
    <div>
        <label>Protocolo</label>
        <input type="text" name="protocolo" value="<?= e($filters['protocolo']) ?>">
    </div>
    <div>
        <label>Status</label>
        <select name="status">
            <option value="">Todos</option>
            <?php foreach (['ABERTA','RECEBIDA','EM_ANALISE','ENCAMINHADA','EM_ATENDIMENTO','CONCLUIDA','NAO_PROCEDE','DUPLICADA'] as $status): ?>
                <option value="<?= e($status) ?>" <?= $filters['status'] === $status ? 'selected' : '' ?>><?= e($status) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div>
        <label>Categoria</label>
        <select name="categoria_id">
            <option value="">Todas</option>
            <?php foreach ($categorias as $categoria): ?>
                <option value="<?= (int)$categoria['id'] ?>" <?= (string)$filters['categoria_id'] === (string)$categoria['id'] ? 'selected' : '' ?>><?= e($categoria['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div>
        <label>Setor</label>
        <select name="setor_id">
            <option value="">Todos</option>
            <?php foreach ($setores as $setor): ?>
                <option value="<?= (int)$setor['id'] ?>" <?= (string)$filters['setor_id'] === (string)$setor['id'] ? 'selected' : '' ?>><?= e($setor['nome']) ?></option>
            <?php endforeach; ?>
        </select>
    </div>
    <div>
        <label>Data inicial</label>
        <input type="date" name="data_inicio" value="<?= e($filters['data_inicio']) ?>">
    </div>
    <div>
        <label>Data final</label>
        <input type="date" name="data_fim" value="<?= e($filters['data_fim']) ?>">
    </div>
    <div class="filter-check">
        <label class="checkbox-inline"><input type="checkbox" name="somente_atrasadas" value="1" <?= !empty($filters['somente_atrasadas']) ? 'checked' : '' ?>> Somente atrasadas</label>
    </div>
    <div class="filter-actions">
        <button type="submit" class="btn">Aplicar filtros</button>
        <a href="<?= e($relatoriosFilterAction) ?>" class="btn btn-outline">Limpar</a>
    </div>
</form>

<div class="card">
    <div class="card-head">
        <div>
            <h3>Resultado</h3>
            <p class="muted"><?= count($itens) ?> ocorrência(s) encontradas.</p>
        </div>
    </div>
    <table class="table responsive-table">
        <thead>
            <tr>
                <th>Protocolo</th>
                <th>Categoria</th>
                <th>Status</th>
                <th>Setor</th>
                <th>SLA</th>
                <th>Data</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($itens)): ?>
                <tr><td colspan="6">Nenhum resultado com os filtros informados.</td></tr>
            <?php else: ?>
                <?php foreach ($itens as $item): ?>
                    <tr>
                        <td><?= e($item['protocolo']) ?></td>
                        <td><span class="category-badge" style="background: <?= e($item['categoria_cor'] ?: '#1351b4') ?>22; color: <?= e($item['categoria_cor'] ?: '#1351b4') ?>; border-color: <?= e($item['categoria_cor'] ?: '#1351b4') ?>55;"><?= e($item['categoria_nome']) ?></span></td>
                        <td><span class="status-badge status-<?= strtolower(str_replace('_', '-', $item['status'])) ?>"><?= e($item['status']) ?></span></td>
                        <td><?= e($item['setor_nome'] ?: '-') ?></td>
                        <td><?= !empty($item['sla_atrasado']) ? '<span class="sla-badge sla-badge-danger">ATRASADO</span>' : '<span class="sla-badge sla-badge-ok">NO PRAZO</span>' ?><div class="muted small-text">SLA: <?= (int)$item['sla_dias'] ?> dia(s)</div></td>
                        <td><?= e($item['criado_em']) ?></td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
</div>
