<?= $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">Ativos territoriais</span>
            <h1 class="mt-5 text-white">Parques eólicos</h1>
            <p class="mt-4 text-sm leading-7 text-slate-300">Gestão da malha operacional de sites, hubs técnicos e parques eólicos por organização.</p>
        </div>
        <div class="flex flex-wrap gap-3">
            <span class="qb-chip qb-chip--soft"><?= count($sites) ?> registos</span>
            <a href="<?= site_url('sites/new') ?>" class="qb-button-primary">Novo parque eólico</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">Nome</th>
                    <th class="px-4 py-4 text-left">Código</th>
                    <th class="px-4 py-4 text-left">Tipo</th>
                    <th class="px-4 py-4 text-left">Organização</th>
                    <th class="px-4 py-4 text-left">Localização</th>
                    <th class="px-4 py-4 text-left">Estado</th>
                    <th class="px-4 py-4 text-left">Ações</th>
                </tr>
            </thead>
            <tbody class="divide-y divide-white/5">
                <?php foreach ($sites as $site): ?>
                    <tr>
                        <td class="px-4 py-4 font-semibold text-white"><?= esc($site['name']) ?></td>
                        <td class="px-4 py-4"><span class="qb-code"><?= esc($site['site_code']) ?></span></td>
                        <td class="px-4 py-4"><?= esc($site['site_type']) ?></td>
                        <td class="px-4 py-4"><?= esc($site['organization_name'] ?? '-') ?></td>
                        <td class="px-4 py-4"><?= esc($site['location'] ?: '-') ?></td>
                        <td class="px-4 py-4"><span class="qb-status-pill qb-status-pill--soft"><?= esc($site['status']) ?></span></td>
                        <td class="px-4 py-4">
                            <div class="qb-table-actions">
                                <a href="<?= site_url('sites/' . $site['id'] . '/edit') ?>" class="qb-button-secondary text-xs">Editar</a>
                                <form action="<?= site_url('sites/' . $site['id'] . '/delete') ?>" method="post" onsubmit="return confirm('Remover este parque eólico?');">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="qb-button-danger text-xs">Remover</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if ($sites === []): ?>
                    <tr>
                        <td colspan="7" class="px-4 py-6">
                            <div class="qb-empty-state">Ainda não existem parques eólicos registados neste tenant.</div>
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
<?= $this->endSection() ?>
