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

$oldJs = '        function editRule(data) {
            document.getElementById(\'rule_id\').value = data.id;
            document.getElementById(\'rule_name\').value = data.name;
            document.getElementById(\'rule_profile_id\').value = data.profile_id || "";
            document.getElementById(\'rule_status_id\').value = data.status_id;
            document.getElementById(\'rule_priority\').value = data.priority || 0;
            document.getElementById(\'rule_orb_op\').value = data.orbital_count_op || "";
            document.getElementById(\'rule_orb_val\').value = data.orbital_count_val !== null ? data.orbital_count_val : "";
            document.getElementById(\'rule_terr_op\').value = data.terrestrial_count_op || "";
            document.getElementById(\'rule_terr_val\').value = data.terrestrial_count_val !== null ? data.terrestrial_count_val : "";
            document.getElementById(\'rule_orb_dom\').value = data.orbital_dominance || "";
            document.getElementById(\'rule_terr_dom\').value = data.terrestrial_dominance || "";
            document.getElementById(\'rule_empty\').checked = data.is_empty_case == 1;
            window.scrollTo(0,0);
        }
        function resetRuleForm() { document.getElementById(\'ruleForm\').reset(); document.getElementById(\'rule_id\').value = 0; }';

$newJs = '        function editRule(data) {
            document.getElementById(\'rule_id\').value = data.id;
            document.getElementById(\'rule_name\').value = data.name;
            document.getElementById(\'rule_profile_id\').value = data.profile_id || "";
            document.getElementById(\'rule_status_id\').value = data.status_id;
            document.getElementById(\'rule_priority\').value = data.priority || 0;
            document.getElementById(\'rule_orb_op\').value = data.orbital_count_op || "";
            document.getElementById(\'rule_orb_val\').value = data.orbital_count_val !== null ? data.orbital_count_val : "";
            document.getElementById(\'rule_terr_op\').value = data.terrestrial_count_op || "";
            document.getElementById(\'rule_terr_val\').value = data.terrestrial_count_val !== null ? data.terrestrial_count_val : "";
            
            // Set multi-selects
            const orbVals = (data.orbital_dominance || "").split(",");
            document.querySelectorAll(\'#ms_orb_list input\').forEach(cb => {
                cb.checked = orbVals.includes(cb.value);
            });
            updateMSLabel(\'ms_orb\');

            const terrVals = (data.terrestrial_dominance || "").split(",");
            document.querySelectorAll(\'#ms_terr_list input\').forEach(cb => {
                cb.checked = terrVals.includes(cb.value);
            });
            updateMSLabel(\'ms_terr\');

            document.getElementById(\'rule_empty\').checked = data.is_empty_case == 1;
            window.scrollTo(0,0);
        }
        function resetRuleForm() { 
            document.getElementById(\'ruleForm\').reset(); 
            document.getElementById(\'rule_id\').value = 0; 
            updateMSLabel(\'ms_orb\');
            updateMSLabel(\'ms_terr\');
        }';

$content = str_replace($oldJs, $newJs, $content);

file_put_contents($file, $content);
echo "editRule and resetRuleForm updated";
?>
