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

// 1. Update Handler logic
$handlerOld = '$terrestrial_dominance = isset($_POST[\'terrestrial_dominance\']) ? implode(\'\',\',(array)$_POST[\'terrestrial_dominance\']) : null;
    $is_empty_case = isset($_POST[\'is_empty_case\']) ? 1 : 0;

    if ($id > 0) {
        $stmt = $db->prepare("UPDATE celestial_object_status_rules SET name = ?, status_id = ?, profile_id = ?, priority = ?, orbital_count_op = ?, orbital_count_val = ?, terrestrial_count_op = ?, terrestrial_count_val = ?, orbital_dominance = ?, terrestrial_dominance = ?, is_empty_case = ? WHERE id = ?");
        $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case, $id]);
    } else {
        $stmt = $db->prepare("INSERT INTO celestial_object_status_rules (name, status_id, profile_id, priority, orbital_count_op, orbital_count_val, terrestrial_count_op, terrestrial_count_val, orbital_dominance, terrestrial_dominance, is_empty_case) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case]);
    }';

$handlerNew = '$terrestrial_dominance = isset($_POST[\'terrestrial_dominance\']) ? implode(\'\',\',(array)$_POST[\'terrestrial_dominance\']) : null;
    $is_empty_case = isset($_POST[\'is_empty_case\']) ? 1 : 0;
    $combine_mode = $_POST[\'combine_mode\'] ?? \'OR\';

    if ($id > 0) {
        $stmt = $db->prepare("UPDATE celestial_object_status_rules SET name = ?, status_id = ?, profile_id = ?, priority = ?, orbital_count_op = ?, orbital_count_val = ?, terrestrial_count_op = ?, terrestrial_count_val = ?, orbital_dominance = ?, terrestrial_dominance = ?, is_empty_case = ?, combine_mode = ? WHERE id = ?");
        $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case, $combine_mode, $id]);
    } else {
        $stmt = $db->prepare("INSERT INTO celestial_object_status_rules (name, status_id, profile_id, priority, orbital_count_op, orbital_count_val, terrestrial_count_op, terrestrial_count_val, orbital_dominance, terrestrial_dominance, is_empty_case, combine_mode) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        $stmt->execute([$name, $status_id, $profile_id, $priority, $orbital_count_op, $orbital_count_val, $terrestrial_count_op, $terrestrial_count_val, $orbital_dominance, $terrestrial_dominance, $is_empty_case, $combine_mode]);
    }';

$content = str_replace($handlerOld, $handlerNew, $content);

// 2. Update UI
$uiOld = '<div style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 4px; border: 1px solid #334155; margin-bottom: 15px;">
                                <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;"> NOMBRE DE FACTIONS :</div>
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">En Orbite</label>
                                        <div style="display: flex; gap: 5px;">
                                            <select name="orbital_count_op" id="rule_orb_op" style="width: 70px;">
                                                <option value="">-</option>
                                                <option value="=">=</option><option value=">">></option><option value="<"><</option>
                                                <option value=">=">>=</option><option value="<="><=</option>
                                            </select>
                                            <input type="number" name="orbital_count_val" id="rule_orb_val" placeholder="0">
                                        </div>
                                    </div>
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">Au Sol</label>
                                        <div style="display: flex; gap: 5px;">
                                            <select name="terrestrial_count_op" id="rule_terr_op" style="width: 70px;">
                                                <option value="">-</option>
                                                <option value="=">=</option><option value=">">></option><option value="<"><</option>
                                                <option value=">=">>=</option><option value="<="><=</option>
                                            </select>
                                            <input type="number" name="terrestrial_count_val" id="rule_terr_val" placeholder="0">
                                        </div>
                                    </div>
                                </div>

                                <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>
                                    
                                    <!-- Orbite -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">En Orbite</label>
                                        <div class="ms-container" id="ms_orb">
                                            <div class="ms-display" onclick="toggleMS(\'ms_orb_list\')">Toutes / Peu importe</div>
                                            <div class="ms-dropdown" id="ms_orb_list">
                                                <label class="ms-item"><input type="checkbox" value="none" onchange="updateMSLabel(\'ms_orb\')"> Aucune (Vide)</label>
                                                <?php foreach($factions_list as $f): if($f[\'name\'] !== \'Aucune\'): ?>
                                                    <label class="ms-item"><input type="checkbox" value="<?php echo $f[\'id\']; ?>" name="orbital_dominance[]" onchange="updateMSLabel(\'ms_orb\')"> <?php echo htmlspecialchars($f[\'name\']); ?></label>
                                                <?php endif; endforeach; ?>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- Sol -->
                                    <div class="form-group" style="flex: 1; margin-bottom: 0;">
                                        <label style="font-size: 10px;">Au Sol</label>
                                        <div class="ms-container" id="ms_terr">
                                            <div class="ms-display" onclick="toggleMS(\'ms_terr_list\')">Toutes / Peu importe</div>
                                            <div class="ms-dropdown" id="ms_terr_list">
                                                <label class="ms-item"><input type="checkbox" value="none" onchange="updateMSLabel(\'ms_terr\')"> Aucune (Vide)</label>
                                                <?php foreach($factions_list as $f): if($f[\'name\'] !== \'Aucune\'): ?>
                                                    <label class="ms-item"><input type="checkbox" value="<?php echo $f[\'id\']; ?>" name="terrestrial_dominance[]" onchange="updateMSLabel(\'ms_terr\')"> <?php echo htmlspecialchars($f[\'name\']); ?></label>
                                                <?php endif; endforeach; ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div style="display: flex; align-items: center; gap: 10px; padding-top: 10px; border-top: 1px solid #334155;">
                                    <input type="checkbox" name="is_empty_case" id="rule_empty" style="width: auto;">
                                    <label for="rule_empty" style="margin-bottom: 0; color: #ebcb8b;">Cas "CASE VIDE" (Aucune faction nulle part)</label>
                                </div>
                            </div>';

$uiNew = '<div style="background: rgba(0,0,0,0.2); padding: 15px; border-radius: 4px; border: 1px solid #334155; margin-bottom: 15px;">
                                <div style="display: flex; gap: 20px; align-items: stretch;">
                                    <!-- ORBITE -->
                                    <div style="flex: 1; display: flex; flex-direction: column; gap: 10px;">
                                        <div style="font-size: 11px; color: #88c0d0; font-weight: bold; text-align: center; border-bottom: 1px solid #334155; padding-bottom: 5px;">EN ORBITE</div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label style="font-size: 10px;">Nombre de factions</label>
                                            <div style="display: flex; gap: 5px;">
                                                <select name="orbital_count_op" id="rule_orb_op" style="width: 70px;">
                                                    <option value="">-</option>
                                                    <option value="=">=</option><option value=">">></option><option value="<"><</option>
                                                    <option value=">=">>=</option><option value="<="><=</option>
                                                </select>
                                                <input type="number" name="orbital_count_val" id="rule_orb_val" placeholder="0">
                                            </div>
                                        </div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label style="font-size: 10px;">Dominance (Factions)</label>
                                            <div class="ms-container" id="ms_orb">
                                                <div class="ms-display" onclick="toggleMS(\'ms_orb_list\')">Toutes / Peu importe</div>
                                                <div class="ms-dropdown" id="ms_orb_list">
                                                    <label class="ms-item"><input type="checkbox" value="none" onchange="updateMSLabel(\'ms_orb\')"> Aucune (Vide)</label>
                                                    <?php foreach($factions_list as $f): if($f[\'name\'] !== \'Aucune\'): ?>
                                                        <label class="ms-item"><input type="checkbox" value="<?php echo $f[\'id\']; ?>" name="orbital_dominance[]" onchange="updateMSLabel(\'ms_orb\')"> <?php echo htmlspecialchars($f[\'name\']); ?></label>
                                                    <?php endif; endforeach; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                    <!-- COMBINAISON -->
                                    <div style="flex: 0 0 100px; display: flex; flex-direction: column; justify-content: center; align-items: center; background: rgba(136, 192, 208, 0.1); border: 1px solid #88c0d0; border-radius: 4px; padding: 10px;">
                                        <label style="font-size: 10px; color: #88c0d0; font-weight: bold; margin-bottom: 8px;">COMBINAISON</label>
                                        <select name="combine_mode" id="rule_combine" style="width: 100%; text-align: center; font-weight: bold; color: #88c0d0; background: #2e3440; border-color: #88c0d0;">
                                            <option value="OR">OU</option>
                                            <option value="AND">ET</option>
                                        </select>
                                    </div>

                                    <!-- SOL -->
                                    <div style="flex: 1; display: flex; flex-direction: column; gap: 10px;">
                                        <div style="font-size: 11px; color: #a3be8c; font-weight: bold; text-align: center; border-bottom: 1px solid #334155; padding-bottom: 5px;">AU SOL</div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label style="font-size: 10px;">Nombre de factions</label>
                                            <div style="display: flex; gap: 5px;">
                                                <select name="terrestrial_count_op" id="rule_terr_op" style="width: 70px;">
                                                    <option value="">-</option>
                                                    <option value="=">=</option><option value=">">></option><option value="<"><</option>
                                                    <option value=">=">>=</option><option value="<="><=</option>
                                                </select>
                                                <input type="number" name="terrestrial_count_val" id="rule_terr_val" placeholder="0">
                                            </div>
                                        </div>
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <label style="font-size: 10px;">Dominance (Factions)</label>
                                            <div class="ms-container" id="ms_terr">
                                                <div class="ms-display" onclick="toggleMS(\'ms_terr_list\')">Toutes / Peu importe</div>
                                                <div class="ms-dropdown" id="ms_terr_list">
                                                    <label class="ms-item"><input type="checkbox" value="none" onchange="updateMSLabel(\'ms_terr\')"> Aucune (Vide)</label>
                                                    <?php foreach($factions_list as $f): if($f[\'name\'] !== \'Aucune\'): ?>
                                                        <label class="ms-item"><input type="checkbox" value="<?php echo $f[\'id\']; ?>" name="terrestrial_dominance[]" onchange="updateMSLabel(\'ms_terr\')"> <?php echo htmlspecialchars($f[\'name\']); ?></label>
                                                    <?php endif; endforeach; ?>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div style="display: flex; align-items: center; gap: 10px; padding-top: 15px; margin-top: 15px; border-top: 1px solid #334155;">
                                    <input type="checkbox" name="is_empty_case" id="rule_empty" style="width: auto;">
                                    <label for="rule_empty" style="margin-bottom: 0; color: #ebcb8b;">Cas "CASE VIDE" (Aucune faction nulle part)</label>
                                </div>
                            </div>';

$content = str_replace($uiOld, $uiNew, $content);

// 3. Update JS (editRule)
$jsOld = '            document.getElementById(\'rule_empty\').checked = data.is_empty_case == 1;
            window.scrollTo(0,0);
        }';

$jsNew = '            document.getElementById(\'rule_empty\').checked = data.is_empty_case == 1;
            document.getElementById(\'rule_combine\').value = data.combine_mode || "OR";
            window.scrollTo(0,0);
        }';

$content = str_replace($jsOld, $jsNew, $content);

file_put_contents($file, $content);
echo "admin.php updated successfully.\n";

