<?php
$protocoloSelecionado  = $_GET['protocolo'] ?? '';
$statusSelecionado     = $_GET['status'] ?? '';
$categoriaSelecionada  = $_GET['categoria_id'] ?? '';
$setorSelecionado      = $_GET['setor_id'] ?? '';
$dataInicioSelecionada = $_GET['data_inicio'] ?? '';
$dataFimSelecionada    = $_GET['data_fim'] ?? '';
?>

<section class="page-header">
    <h2>Ocorrências</h2>
    <p>Listagem geral das solicitações registradas no sistema.</p>
</section>

<?php if ($msg = Session::getFlash('sucesso')): ?>
    <div class="alert success"><?= e($msg) ?></div>
<?php endif; ?>

<?php if ($msg = Session::getFlash('erro')): ?>
    <div class="alert error"><?= e($msg) ?></div>
<?php endif; ?>

<div class="card" style="margin-bottom: 20px;">
    <form method="GET" action="<?= base_url('index.php') ?>">
        <input type="hidden" name="route" value="/admin/ocorrencias">

        <div class="grid-2">
            <div>
                <label for="protocolo">Protocolo</label>
                <input type="text" id="protocolo" name="protocolo" value="<?= e($protocoloSelecionado) ?>" placeholder="Ex.: PR-2026-000001">
            </div>

            <div>
                <label for="status">Status</label>
                <select id="status" 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) ?>" <?= $statusSelecionado === $status ? 'selected' : '' ?>>
                            <?= e($status) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label for="categoria_id">Categoria</label>
                <select id="categoria_id" name="categoria_id">
                    <option value="">Todas</option>
                    <?php foreach (($categorias ?? []) as $categoria): ?>
                        <option value="<?= (int)$categoria['id'] ?>" <?= (string)$categoriaSelecionada === (string)$categoria['id'] ? 'selected' : '' ?>>
                            <?= e($categoria['nome']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label for="setor_id">Setor</label>
                <select id="setor_id" name="setor_id">
                    <option value="">Todos</option>
                    <?php foreach (($setores ?? []) as $setor): ?>
                        <option value="<?= (int)$setor['id'] ?>" <?= (string)$setorSelecionado === (string)$setor['id'] ? 'selected' : '' ?>>
                            <?= e($setor['nome']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div>
                <label for="data_inicio">Data inicial</label>
                <input type="date" id="data_inicio" name="data_inicio" value="<?= e($dataInicioSelecionada) ?>">
            </div>

            <div>
                <label for="data_fim">Data final</label>
                <input type="date" id="data_fim" name="data_fim" value="<?= e($dataFimSelecionada) ?>">
            </div>
        </div>

        <div style="margin-top: 16px; display: flex; gap: 8px; flex-wrap: wrap;">
            <button type="submit" class="btn">Filtrar</button>
            <a href="<?= base_url('index.php?route=/admin/ocorrencias') ?>" class="btn btn-outline">Limpar</a>
        </div>
    </form>
</div>

<div class="card">
    <?php if (empty($ocorrencias ?? [])): ?>
        <p>Nenhuma ocorrência encontrada.</p>
    <?php else: ?>
        <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>
                    <th>Ação</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($ocorrencias 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>
                            <?php if (($item['sla_status'] ?? '') === 'ATRASADO'): ?>
                                <span class="sla-badge sla-badge-danger">ATRASADO</span>
                            <?php else: ?>
                                <span class="sla-badge sla-badge-ok">NO PRAZO</span>
                            <?php endif; ?>
                            <div class="muted small-text">SLA: <?= (int)($item['sla_dias'] ?? 0) ?> dia(s)</div>
                        </td>
                        <td><?= e($item['criado_em']) ?></td>
                        <td>
                            <a class="btn btn-sm" href="<?= base_url('index.php?route=/admin/ocorrencias/show&id=' . (int)$item['id']) ?>">Abrir</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
