<?php
require_once __DIR__ . '/auth.php';
require_admin();
require_once __DIR__ . '/includes/header.php';

$orphaned_deleted = delete_orphaned_reseller_clients();
$reseller_filter = (int)($_GET['reseller_id'] ?? 0);
$resellers = get_resellers();
$clients = get_all_clients_for_admin($reseller_filter > 0 ? $reseller_filter : null);
?>

<style>
    .page-header {
        margin-bottom: 30px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        flex-wrap: wrap;
        gap: 15px;
    }

    .page-header h2 {
        font-size: 28px;
        margin: 0;
        background: var(--primary-bg);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }

    .add-btn {
        background: var(--primary-bg);
        color: #fff;
        padding: 10px 20px;
        border-radius: 6px;
        text-decoration: none;
        font-size: 14px;
        font-weight: 600;
        transition: all 0.2s ease;
        display: inline-block;
    }

    .add-btn:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    }

    .filter-card,
    .table-card {
        background: #682323;
        padding: 24px;
        border-radius: 12px;
        border: 1px solid rgba(255, 255, 255, 0.1);
        margin-bottom: 20px;
    }

    .filter-row {
        display: flex;
        gap: 12px;
        align-items: end;
        flex-wrap: wrap;
    }

    .form-group {
        flex: 1;
        min-width: 240px;
    }

    .form-group label {
        display: block;
        margin-bottom: 8px;
        color: #fff;
        font-weight: 500;
        font-size: 14px;
    }

    .form-group select {
        width: 100%;
        padding: 12px 15px;
        background: #111;
        border: 1px solid rgba(255, 255, 255, 0.2);
        border-radius: 6px;
        color: #fff;
        font-size: 14px;
    }

    .form-group select option {
        background-color: #111 !important;
        color: #fff !important;
    }

    .form-group select option:checked {
        background-color: #d92828 !important;
        color: #fff !important;
    }

    .table-scroll {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .clients-table {
        width: 100%;
        min-width: 860px;
        border-collapse: collapse;
    }

    .clients-table thead {
        background: #0a0a0a;
    }

    .clients-table th,
    .clients-table td {
        padding: 15px;
        text-align: left;
        font-size: 14px;
        color: #fff;
        border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    }

    .code-cell {
        font-family: 'Courier New', monospace;
        font-weight: 700;
        letter-spacing: 1px;
        color: #f6c7c7;
        white-space: nowrap;
    }

    .muted-small {
        color: #dda7a7;
        font-size: 12px;
        margin-top: 3px;
    }

    .status-badge {
        display: inline-block;
        padding: 4px 12px;
        border-radius: 20px;
        font-size: 12px;
        font-weight: 600;
    }

    .status-active {
        background: rgba(46, 204, 113, 0.2);
        color: #2ecc71;
    }

    .status-inactive {
        background: rgba(231, 76, 60, 0.2);
        color: #e74c3c;
    }

    @media (max-width: 768px) {
        .filter-card,
        .table-card {
            padding: 18px;
        }

        .clients-table {
            min-width: 760px;
            font-size: 12px;
        }

        .clients-table th,
        .clients-table td {
            padding: 10px 8px;
        }
    }
</style>

<div class="page-header">
    <h2>Todos clientes</h2>
    <a href="list_dns.php" class="add-btn">Meus clientes</a>
</div>

<div class="filter-card">
    <form method="get" class="filter-row">
        <div class="form-group">
            <label for="reseller_id">Escolher Revenda</label>
            <select id="reseller_id" name="reseller_id" onchange="this.form.submit()">
                <option value="0">Todas</option>
                <?php foreach ($resellers as $reseller): ?>
                    <option value="<?php echo (int)$reseller['id']; ?>" <?php echo $reseller_filter === (int)$reseller['id'] ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars(user_display_name($reseller)); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
    </form>
</div>

<div class="table-card">
    <div class="table-scroll">
        <table class="clients-table">
            <thead>
                <tr>
                    <th>Nome</th>
                    <th>Dono</th>
                    <th>Vencimento</th>
                    <th>Codigo</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($clients)): ?>
                    <tr>
                        <td colspan="5" style="text-align:center; padding:40px; color:#666;">Nenhum cliente encontrado.</td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($clients as $client): ?>
                        <?php
                            $expiry = format_client_expiry_for_panel($client);
                            $blocked_by_reseller = client_blocked_by_reseller($client);
                            $client_active = !empty($client['is_active']) && !$blocked_by_reseller;
                        ?>
                        <tr>
                            <td><?php echo htmlspecialchars($client['name'] ?? ''); ?></td>
                            <td>
                                <?php if (!empty($client['reseller_user_id'])): ?>
                                    <?php echo htmlspecialchars(trim((string)($client['reseller_name'] ?? '')) ?: (string)($client['reseller_username'] ?? 'Revenda')); ?>
                                <?php else: ?>
                                    Admin
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php echo htmlspecialchars($expiry['date']); ?>
                                <div class="muted-small"><?php echo htmlspecialchars($expiry['source']); ?></div>
                            </td>
                            <td class="code-cell"><?php echo htmlspecialchars(format_activation_code($client['activation_code'] ?? '')); ?></td>
                            <td>
                                <span class="status-badge <?php echo $client_active ? 'status-active' : 'status-inactive'; ?>">
                                    <?php echo $blocked_by_reseller ? 'Bloqueado' : (!empty($client['is_active']) ? 'Ativo' : 'Inativo'); ?>
                                </span>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

</main>
</body>
</html>
