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

<?= $this->section('content') ?>
<section class="qb-page-hero">
    <div class="flex flex-col gap-4 lg:flex-row lg:items-end lg:justify-between">
        <div class="qb-page-hero-copy">
            <span class="qb-label">Operações técnicas</span>
            <h1 class="mt-5 text-white">Manutencoes</h1>
            <p class="mt-4 text-sm leading-7 text-slate-300">Intervenções técnicas, auditoria operacional e rastreabilidade do ciclo de manutenção.</p>
        </div>
        <div class="flex flex-wrap gap-3">
            <span class="qb-chip qb-chip--soft"><?= count($interventions) ?> intervenções</span>
            <a href="<?= site_url('maintenance/new') ?>" class="qb-button-primary">Nova intervenção</a>
        </div>
    </div>
</section>

<section class="qb-table-shell">
    <div class="overflow-x-auto">
        <table class="min-w-full text-sm">
            <thead>
                <tr>
                    <th class="px-4 py-4 text-left">Codigo</th>
                    <th class="px-4 py-4 text-left">Titulo</th>
                    <th class="px-4 py-4 text-left">Site</th>
                    <th class="px-4 py-4 text-left">Turbina</th>
                    <th class="px-4 py-4 text-left">Tipo</th>
                    <th class="px-4 py-4 text-left">Estado</th>
                    <th class="px-4 py-4 text-left">Acoes</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-white/5">
                <?php foreach ($interventions as $intervention): ?>
                    <tr>
                        <td class="px-4 py-4 font-semibold text-white"><span class="qb-code"><?= esc($intervention['intervention_code']) ?></span></td>
                        <td class="px-4 py-4"><?= esc($intervention['title']) ?></td>
                        <td class="px-4 py-4"><?= esc($intervention['site_name'] ?? '-') ?></td>
                        <td class="px-4 py-4"><?= esc($intervention['turbine_code'] ?? '-') ?></td>
                        <td class="px-4 py-4"><?= esc($intervention['intervention_type']) ?></td>
                        <td class="px-4 py-4"><span class="qb-status-pill"><?= esc($intervention['status']) ?></span></td>
                        <td class="px-4 py-4">
                            <div class="qb-table-actions">
                                <a href="<?= site_url('maintenance/' . rawurlencode($intervention['intervention_code'])) ?>" class="qb-button-secondary text-xs">Abrir</a>
                                <a href="<?= site_url('maintenance/' . rawurlencode($intervention['intervention_code']) . '/edit') ?>" class="qb-button-secondary text-xs">Editar</a>
                                <form action="<?= site_url('maintenance/' . rawurlencode($intervention['intervention_code']) . '/delete') ?>" method="post" onsubmit="return confirm('Remover esta intervenção?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="qb-button-danger text-xs">Remover</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if ($interventions === []): ?>
                    <tr>
                        <td colspan="7" class="px-4 py-6">
                            <div class="qb-empty-state">Ainda não existem intervenções técnicas registadas neste tenant.</div>
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
<?= $this->endSection() ?>
