<?php
include('config.php');
$regulators = $_GET['regulators'] ?? [];

$sql = "SELECT DISTINCT regulator FROM brokers WHERE regulator IS NOT NULL AND regulator != ''";
$regStmt = $dbh->query($sql);
$availableRegulators = $regStmt->fetchAll(PDO::FETCH_COLUMN);

// Mapping full regulator names to short forms (customize this)
$regShortForms = [
    'Financial Conduct Authority' => 'FCA',
    'Australian Securities and Investments Commission' => 'ASIC',
    'Cyprus Securities and Exchange Commission' => 'CySEC',
    'Securities and Exchange Commission' => 'SEC',
    'Financial Services Authority' => 'FSA',
    'Monetary Authority of Singapore' => 'MAS',
    'Commodity Futures Trading Commission' => 'CFTC',
    // Add more mappings here
];

// Prepare filter query
if (!empty($regulators)) {
    $placeholders = implode(',', array_fill(0, count($regulators), '?'));
    $sql = "SELECT * FROM brokers WHERE regulator IN ($placeholders)";
    $stmt = $dbh->prepare($sql);
    $stmt->execute($regulators);
} else {
    $stmt = $dbh->prepare("SELECT * FROM brokers");
    $stmt->execute();
}

$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">

<style>
/* Filter box styles */
.filter-box-container {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 25px;
}

.filter-box {
    cursor: pointer;
    user-select: none;
    border: 2px solid #ddd;
    border-radius: 12px;
    padding: 10px 20px;
    font-weight: 600;
    color: #555;
    background: #f9f9f9;
    transition: all 0.3s ease;
    min-width: 100px;
    text-align: center;
    box-shadow: 0 0 5px rgba(0,0,0,0.05);
}

.filter-box:hover {
    border-color: #0d6efd;
    color: #0d6efd;
    background: #e7f1ff;
}

.filter-box input[type="checkbox"] {
    display: none;
}

.filter-box.selected {
    background: #0d6efd;
    color: white;
    border-color: #0a58ca;
    box-shadow: 0 0 10px rgba(13, 110, 253, 0.5);
}

/* Broker list item adjustments (optional to keep your style) */
.list-box-item {
    display: flex;
    align-items: center;
    border: 1px solid #eee;
    border-radius: 10px;
    padding: 15px;
    margin-bottom: 20px;
    background: #fff;
    text-decoration: none;
    color: inherit;
    transition: box-shadow 0.3s ease;
}
.list-box-item:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}
</style>

<div class="container py-4">
    <h5 class="mb-3">Filter by Regulator</h5>
    <form id="filterForm" method="GET">
        <div class="filter-box-container">
            <?php foreach ($availableRegulators as $reg): 
                $short = $regShortForms[$reg] ?? $reg;
                $checked = in_array($reg, $regulators);
            ?>
            <label class="filter-box <?= $checked ? 'selected' : '' ?>">
                <input type="checkbox" name="regulators[]" value="<?= htmlentities($reg) ?>" <?= $checked ? 'checked' : '' ?>>
                <?= htmlentities($short) ?>
            </label>
            <?php endforeach; ?>
        </div>
    </form>

    <?php if (count($results) > 0): ?>
        <?php foreach ($results as $row): ?>
            <a href="/avatrade.php?name=<?= strtolower(htmlentities($row['name'])) ?>" class="list-box-item">
                <div class="item-left">
                    <div class="img-box">
                        <img src="./uploads/<?= htmlentities($row['logo']) ?>" alt="<?= htmlentities($row['name']) ?>">
                        <div class="tag">Regulated</div>
                    </div>
                </div>
                <div class="text-box">
                    <div class="title"><?= htmlentities($row['name']) ?></div>
                    <div class="flag-box">
                        <img src="https://flagcdn.com/w40/ie.png" alt="flag">
                    </div>
                    <div class="mobile-flag-box d-md-none d-block">
                        <div>Average Spread</div>
                        <div class="num"><?= htmlentities($row['spread']) ?></div>
                    </div>
                    <div class="tag overhide">
                        <section class="label-common labelmore">
                            <div class="d-flex flex-wrap">
                                <span class="list-add-label me-2 mb-1">10-15 years</span>
                                <span class="list-add-label me-2 mb-1"><?= htmlentities($row['regulator']) ?></span>
                                <span class="list-add-label me-2 mb-1">MT5 Full License</span>
                            </div>
                        </section>
                    </div>
                </div>
                <div class="item-right d-none d-md-block">
                    <div class="text">Average Spread</div>
                    <div class="num"><?= htmlentities($row['spread']) ?></div>
                </div>
            </a>
        <?php endforeach; ?>
    <?php else: ?>
        <p>No brokers found for the selected filters.</p>
    <?php endif; ?>
</div>

<script>
document.querySelectorAll('#filterForm input[type="checkbox"]').forEach(checkbox => {
    checkbox.addEventListener('change', () => {
        // Toggle selected class for label
        const label = checkbox.closest('label');
        if (checkbox.checked) {
            label.classList.add('selected');
        } else {
            label.classList.remove('selected');
        }

        // Submit the form automatically on change
        document.getElementById('filterForm').submit();
    });
});
</script>
