<section class="page-header page-header-flex">
    <div>
        <h2>Serviços</h2>
        <p>Cadastre e mantenha os serviços exibidos no aplicativo e usados nas ocorrências.</p>
    </div>
    <div>
        <a class="btn" href="<?= base_url('/admin/servicos/create') ?>">Novo serviço</a>
    </div>
</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">
    <?php if (empty($servicos ?? [])): ?>
        <p>Nenhum serviço cadastrado.</p>
    <?php else: ?>
        <table class="table responsive-table">
            <thead>
                <tr>
                    <th>Serviço</th>
                    <th>Cor</th>
                    <th>SLA</th>
                    <th>Status</th>
                    <th>Ícone</th>
                    <th>Ação</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($servicos as $servico): ?>
                    <?php $cor = $servico['cor'] ?: '#1351b4'; ?>
                    <tr>
                        <td>
                            <span class="category-badge" style="background: <?= e($cor) ?>22; color: <?= e($cor) ?>; border-color: <?= e($cor) ?>55;">
                                <?= e($servico['nome']) ?>
                            </span>
                        </td>
                        <td>
                            <div class="color-chip-wrap">
                                <span class="color-chip" style="background: <?= e($cor) ?>;"></span>
                                <code><?= e($cor) ?></code>
                            </div>
                        </td>
                        <td><?= (int) ($servico['sla_dias'] ?? 0) ?> dia(s)</td>
                        <td>
                            <?php if ((int) ($servico['ativo'] ?? 0) === 1): ?>
                                <span class="status-badge status-concluida">ATIVO</span>
                            <?php else: ?>
                                <span class="status-badge status-nao-procede">INATIVO</span>
                            <?php endif; ?>
                        </td>
                        <td><?= e($servico['icone'] ?: '-') ?></td>
                        <td>
                            <a class="btn btn-sm" href="<?= base_url('/admin/servicos/edit?id=' . (int) $servico['id']) ?>">Editar</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
