<h2>Ocorrência <?= e($ocorrencia['protocolo']) ?></h2>

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

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

<div class="detail-grid">
    <div>
        <div class="card">
            <p><strong>Categoria:</strong> <span class="category-badge" style="background: <?= e($ocorrencia['categoria_cor'] ?: '#1351b4') ?>22; color: <?= e($ocorrencia['categoria_cor'] ?: '#1351b4') ?>; border-color: <?= e($ocorrencia['categoria_cor'] ?: '#1351b4') ?>55;"><?= e($ocorrencia['categoria_nome']) ?></span></p>
            <p><strong>Status:</strong> <?= e($ocorrencia['status']) ?></p>
            <p><strong>SLA:</strong>
                <?php if (($ocorrencia['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; ?>
                <span class="muted small-text">Prazo: <?= (int)($ocorrencia['sla_dias'] ?? 0) ?> dia(s)</span>
            </p>
            <p><strong>Setor:</strong> <?= e($ocorrencia['setor_nome'] ?? '-') ?></p>
            <p><strong>Descrição:</strong> <?= e($ocorrencia['descricao']) ?></p>
            <p><strong>Latitude:</strong> <?= e($ocorrencia['latitude']) ?></p>
            <p><strong>Longitude:</strong> <?= e($ocorrencia['longitude']) ?></p>
            <p><strong>Referência:</strong> <?= e($ocorrencia['endereco_referencia']) ?></p>
            <p><strong>Cidadão:</strong> <?= $ocorrencia['anonima'] ? 'ANÔNIMO' : e($ocorrencia['cidadao_nome'] ?: '-') ?></p>
            <?php if (!empty($ocorrencia['imagem'])): ?>
                <p><strong>Imagem enviada:</strong></p>
                <img class="preview-image" src="<?= storage_url('uploads/ocorrencias/' . $ocorrencia['imagem']) ?>" alt="Imagem da ocorrência">
            <?php endif; ?>
        </div>

        <div class="card">
            <h3>Localização</h3>
            <div id="mapa-admin" class="map-box"></div>
        </div>

        <div class="card">
            <h3>Histórico</h3>
            <?php if (empty($historico)): ?>
                <p>Nenhum histórico registrado.</p>
            <?php else: ?>
                <ul class="timeline">
                    <?php foreach ($historico as $item): ?>
                        <li>
                            <strong><?= e($item['status_novo']) ?></strong>
                            <div><?= e($item['observacao'] ?? '') ?></div>
                            <small><?= e($item['usuario_nome'] ?: 'Sistema') ?> • <?= e($item['criado_em']) ?></small>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div>

    <div>
        <div class="card narrow-card">
            <form method="POST" action="<?= base_url('/admin/ocorrencias/status') ?>">
                <input type="hidden" name="id" value="<?= (int)$ocorrencia['id'] ?>">

                <label>Alterar status</label>
                <select name="status" required>
                    <?php foreach (['ABERTA','RECEBIDA','EM_ANALISE','ENCAMINHADA','EM_ATENDIMENTO','CONCLUIDA','NAO_PROCEDE','DUPLICADA'] as $status): ?>
                        <option value="<?= $status ?>" <?= $ocorrencia['status'] === $status ? 'selected' : '' ?>><?= $status ?></option>
                    <?php endforeach; ?>
                </select>

                <button type="submit" class="btn">Salvar status</button>
            </form>
        </div>

        <div class="card narrow-card">
            <form method="POST" action="<?= base_url('/admin/ocorrencias/setor') ?>">
                <input type="hidden" name="id" value="<?= (int)$ocorrencia['id'] ?>">

                <label>Encaminhar para setor</label>
                <select name="setor_id">
                    <option value="">Sem setor definido</option>
                    <?php foreach ($setores as $setor): ?>
                        <option value="<?= (int)$setor['id'] ?>" <?= (int)$ocorrencia['setor_id'] === (int)$setor['id'] ? 'selected' : '' ?>><?= e($setor['nome']) ?></option>
                    <?php endforeach; ?>
                </select>

                <button type="submit" class="btn">Salvar setor</button>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function () {
    if (typeof renderReadonlyMap === 'function') {
        renderReadonlyMap('mapa-admin', <?= json_encode((float)$ocorrencia['latitude']) ?>, <?= json_encode((float)$ocorrencia['longitude']) ?>, <?= json_encode($ocorrencia['categoria_cor'] ?: '#1351b4') ?>);
    }
});
</script>
