<?php
$content = file_get_contents('admin.php');

$newUi = <<<'HTML'
                                <div style="display: flex; gap: 15px; align-items: flex-start; margin-bottom: 10px;">
                                    <div style="flex: 0 0 180px; font-size: 11px; color: #88c0d0; font-weight: bold; padding-top: 5px;">FILTRE DOMINANCE :</div>
                                    
                                    <!-- ORBITAL MULTI-SELECT -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">En Orbite</label>
                                        <div class="multi-select-dropdown" id="orb_ms">
                                            <div class="multi-select-btn" id="orb_btn" onclick="toggleMS('orb')">N'importe laquelle <i class="fa-solid fa-chevron-down" style="font-size: 10px;"></i></div>
                                            <div class="multi-select-menu" id="orb_menu">
                                                <div class="multi-select-header">
                                                    <button type="button" onclick="msAll('orb', true)">Tout sélectionner</button>
                                                    <button type="button" onclick="msAll('orb', false)">Tout désélectionner</button>
                                                </div>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="orbital_dominant_factions[]" value="any" id="orb_any" checked onchange="handleMSChange(this, 'orb', true)">
                                                    <span>N'importe laquelle</span>
                                                </label>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="orbital_dominant_factions[]" value="none" class="orb-cb" data-name="Aucune" onchange="handleMSChange(this, 'orb', false)">
                                                    <span>Aucune (Vide)</span>
                                                </label>
                                                <hr style="border:0; border-top:1px solid #334155; margin: 5px 0;">
                                                <?php foreach($factions_list as $f): ?>
                                                    <label class="multi-select-option">
                                                        <input type="checkbox" name="orbital_dominant_factions[]" value="<?php echo $f['id']; ?>" class="orb-cb" data-name="<?php echo htmlspecialchars($f['name']); ?>" onchange="handleMSChange(this, 'orb', false)">
                                                        <span><?php echo htmlspecialchars($f['name']); ?></span>
                                                    </label>
                                                <?php endforeach; ?>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- GROUND MULTI-SELECT -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">Au Sol</label>
                                        <div class="multi-select-dropdown" id="terr_ms">
                                            <div class="multi-select-btn" id="terr_btn" onclick="toggleMS('terr')">N'importe laquelle <i class="fa-solid fa-chevron-down" style="font-size: 10px;"></i></div>
                                            <div class="multi-select-menu" id="terr_menu">
                                                <div class="multi-select-header">
                                                    <button type="button" onclick="msAll('terr', true)">Tout sélectionner</button>
                                                    <button type="button" onclick="msAll('terr', false)">Tout désélectionner</button>
                                                </div>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="ground_dominant_factions[]" value="any" id="terr_any" checked onchange="handleMSChange(this, 'terr', true)">
                                                    <span>N'importe laquelle</span>
                                                </label>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="ground_dominant_factions[]" value="none" class="terr-cb" data-name="Aucune" onchange="handleMSChange(this, 'terr', false)">
                                                    <span>Aucune (Vide)</span>
                                                </label>
                                                <hr style="border:0; border-top:1px solid #334155; margin: 5px 0;">
                                                <?php foreach($factions_list as $f): ?>
                                                    <label class="multi-select-option">
                                                        <input type="checkbox" name="ground_dominant_factions[]" value="<?php echo $f['id']; ?>" class="terr-cb" data-name="<?php echo htmlspecialchars($f['name']); ?>" onchange="handleMSChange(this, 'terr', false)">
                                                        <span><?php echo htmlspecialchars($f['name']); ?></span>
                                                    </label>
                                                <?php endforeach; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div style="display: flex; align-items: center; gap: 20px; padding-top: 10px; border-top: 1px solid #334155;">
                                    <div style="display: flex; align-items: center; gap: 5px;">
                                        <input type="checkbox" name="is_empty_case" id="rule_empty" style="width: auto;">
                                        <label for="rule_empty" style="margin-bottom: 0; color: #ebcb8b; font-size: 11px;">Cas "CASE VIDE"</label>
                                    </div>
                                    <div style="display: flex; align-items: center; gap: 5px;">
                                        <input type="checkbox" name="dominance_diff_required" id="rule_diff" style="width: auto;">
                                        <label for="rule_diff" style="margin-bottom: 0; color: #a3be8c; font-size: 11px;">Dominante orbite ≠ Dominante sol</label>
                                    </div>
                                </div>
HTML;

$content = preg_replace('/<div style="display: flex; gap: 15px; align-items: center; margin-bottom: 10px;">
                                    <div style="flex: 0 0 180px; font-size: 11px; color: #88c0d0; font-weight: bold;">FACTION DOMINANTE :</div>
                                    
                                    <!-- ORBITAL MULTI-SELECT -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">En Orbite</label>
                                        <div class="multi-select-dropdown" id="orb_ms">
                                            <div class="multi-select-btn" id="orb_btn" onclick="toggleMS('orb')">N\'importe laquelle <i class="fa-solid fa-chevron-down" style="font-size: 10px;"></i></div>
                                            <div class="multi-select-menu" id="orb_menu">
                                                <div class="multi-select-header">
                                                    <button type="button" onclick="msAll('orb', true)">Tout sélectionner</button>
                                                    <button type="button" onclick="msAll('orb', false)">Tout désélectionner</button>
                                                </div>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="orbital_dominant_factions[]" value="any" id="orb_any" checked onchange="handleMSChange(this, 'orb', true)">
                                                    <span>N\'importe laquelle</span>
                                                </label>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="orbital_dominant_factions[]" value="none" class="orb-cb" data-name="Aucune" onchange="handleMSChange(this, 'orb', false)">
                                                    <span>Aucune (Vide)</span>
                                                </label>
                                                <hr style="border:0; border-top:1px solid #334155; margin: 5px 0;">
                                                <?php foreach($factions_list as $f):
                                                    ?><label class="multi-select-option">
                                                        <input type="checkbox" name="orbital_dominant_factions[]" value="<?php echo $f['id']; ?>" class="orb-cb" data-name="<?php echo htmlspecialchars($f['name']); ?>" onchange="handleMSChange(this, 'orb', false)">
                                                        <span><?php echo htmlspecialchars($f['name']); ?></span>
                                                    </label>
                                                <?php endforeach; ?>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- GROUND MULTI-SELECT -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">Au Sol</label>
                                        <div class="multi-select-dropdown" id="terr_ms">
                                            <div class="multi-select-btn" id="terr_btn" onclick="toggleMS('terr')">N\'importe laquelle <i class="fa-solid fa-chevron-down" style="font-size: 10px;"></i></div>
                                            <div class="multi-select-menu" id="terr_menu">
                                                <div class="multi-select-header">
                                                    <button type="button" onclick="msAll('terr', true)">Tout sélectionner</button>
                                                    <button type="button" onclick="msAll('terr', false)">Tout désélectionner</button>
                                                </div>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="ground_dominant_factions[]" value="any" id="terr_any" checked onchange="handleMSChange(this, 'terr', true)">
                                                    <span>N\'importe laquelle</span>
                                                </label>
                                                <label class="multi-select-option">
                                                    <input type="checkbox" name="ground_dominant_factions[]" value="none" class="terr-cb" data-name="Aucune" onchange="handleMSChange(this, 'terr', false)">
                                                    <span>Aucune (Vide)</span>
                                                </label>
                                                <hr style="border:0; border-top:1px solid #334155; margin: 5px 0;">
                                                <?php foreach($factions_list as $f):
                                                    ?><label class="multi-select-option">
                                                        <input type="checkbox" name="ground_dominant_factions[]" value="<?php echo $f['id']; ?>" class="terr-cb" data-name="<?php echo htmlspecialchars($f['name']); ?>" onchange="handleMSChange(this, 'terr', false)">
                                                        <span><?php echo htmlspecialchars($f['name']); ?></span>
                                                    </label>
                                                <?php endforeach; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div style="display: flex; align-items: center; gap: 20px; padding-top: 10px; border-top: 1px solid #334155;">
                                    <div style="display: flex; align-items: center; gap: 5px;">
                                        <input type="checkbox" name="is_empty_case" id="rule_empty" style="width: auto;">
                                        <label for="rule_empty" style="margin-bottom: 0; color: #ebcb8b; font-size: 11px;">Cas "CASE VIDE"</label>
                                    </div>
                                    <div style="display: flex; align-items: center; gap: 5px;">
                                        <input type="checkbox" name="dominance_diff_required" id="rule_diff" style="width: auto;">
                                        <label for="rule_diff" style="margin-bottom: 0; color: #a3be8c; font-size: 11px;">Dominante orbite ≠ Dominante sol</label>
                                    </div>
                                </div>/s', $content);

file_put_contents('admin.php', $content);
echo "UI updated.\n";
