<div class="row mb-4">
    <div class="col-12">
        <h5><i class="fa fa-cog"></i> Atualizar DNS em Massa</h5>
        <hr>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-6">
        <h6>Atualizar Listas Selecionadas</h6>
        <form method="post" class="mb-4">
            <div class="mb-3">
                <label class="form-label">DNS Primário</label>
                <input type="text" name="dns_primary" class="form-control" value="8.8.8.8" required>
            </div>
            <div class="mb-3">
                <label class="form-label">DNS Secundário</label>
                <input type="text" name="dns_secondary" class="form-control" value="8.8.4.4" required>
            </div>

            <div class="mb-3">
                <label class="form-label">Selecionar Listas</label>
                <div class="list-group" style="max-height: 300px; overflow-y: auto;">
                    <div class="list-group-item">
                        <input type="checkbox" id="select-all" onchange="toggleSelectAll(this)">
                        <label for="select-all" class="form-check-label ms-2">Selecionar Tudo</label>
                    </div>
                    <?php foreach ($m3u_lists as $list): ?>
                        <div class="list-group-item">
                            <input type="checkbox" name="list_ids[]" value="<?= intval($list['id'] ?? 0) ?>" class="list-check">
                            <label class="form-check-label ms-2">
                                <?= htmlspecialchars($list['title'] ?? '') ?>
                                <small class="text-muted">(<?= htmlspecialchars($list['device_name'] ?? 'Sem dispositivo') ?>)</small>
                            </label>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <button type="submit" name="bulk_update_dns" class="btn btn-primary" onclick="return confirm('Atualizar DNS das listas selecionadas?')">
                <i class="fa fa-check"></i> Atualizar Selecionadas
            </button>
        </form>
    </div>

    <div class="col-md-6">
        <h6>Atualizar TODAS as Listas</h6>
        <form method="post" onsubmit="return confirm('⚠️ Isso atualizará o DNS de TODAS as listas! Tem certeza?')">
            <div class="alert alert-warning" role="alert">
                <i class="fa fa-warning"></i> Esta ação afetará <strong><?= count($m3u_lists) ?></strong> lista(s)
            </div>

            <div class="mb-3">
                <label class="form-label">DNS Primário</label>
                <input type="text" name="dns_primary" class="form-control" value="8.8.8.8" required>
            </div>
            <div class="mb-3">
                <label class="form-label">DNS Secundário</label>
                <input type="text" name="dns_secondary" class="form-control" value="8.8.4.4" required>
            </div>

            <button type="submit" name="update_all_dns" class="btn btn-danger">
                <i class="fa fa-exclamation"></i> Atualizar TUDO
            </button>
        </form>
    </div>
</div>

<!-- Estatísticas -->
<div class="row mt-4">
    <div class="col-md-3">
        <div class="bg-primary bg-opacity-10 rounded p-3">
            <h6 class="text-muted mb-2"><i class="fa fa-list"></i> Total de Listas</h6>
            <h2 class="mb-0"><?= count($m3u_lists) ?></h2>
        </div>
    </div>
    <div class="col-md-3">
        <div class="bg-success bg-opacity-10 rounded p-3">
            <h6 class="text-muted mb-2"><i class="fa fa-check-circle"></i> Listas Ativas</h6>
            <h2 class="mb-0"><?= count(array_filter($m3u_lists, fn($l) => ($l['status'] ?? 'active') === 'active')) ?></h2>
        </div>
    </div>
    <div class="col-md-3">
        <div class="bg-danger bg-opacity-10 rounded p-3">
            <h6 class="text-muted mb-2"><i class="fa fa-times-circle"></i> Listas Inativas</h6>
            <h2 class="mb-0"><?= count(array_filter($m3u_lists, fn($l) => ($l['status'] ?? 'active') === 'inactive')) ?></h2>
        </div>
    </div>
    <div class="col-md-3">
        <div class="bg-info bg-opacity-10 rounded p-3">
            <h6 class="text-muted mb-2"><i class="fa fa-network-wired"></i> Com Dispositivo</h6>
            <h2 class="mb-0"><?= count(array_filter($m3u_lists, fn($l) => !empty($l['mac_address'] ?? ''))) ?></h2>
        </div>
    </div>
</div>

<script>
function toggleSelectAll(checkbox) {
    const checkboxes = document.querySelectorAll('.list-check');
    checkboxes.forEach(cb => cb.checked = checkbox.checked);
}
</script>
