function handleFieldUpdate()

in script.js [133:214]


  function handleFieldUpdate(e) {
    if (!selectedMgId || !currentData || isUpdating) return;

    // Skip handling the ID field, it's handled in handleIdUpdate()
    if (e.target === mgIdInput) return;

    const index = currentData.management_groups.findIndex(mg => mg.id === selectedMgId);
    if (index === -1) return;

    // Get current management group
    const currentMg = currentData.management_groups[index];

    // Get updated values from form
    const newDisplayName = mgDisplayNameInput.value.trim();
    const newParentId = mgParentIdSelect.value === 'null' ? null : mgParentIdSelect.value;
    const newExists = mgExistsCheckbox.checked;

    // Handle validation for existence status
    if (e.target === mgExistsCheckbox && newExists) {
      // Check if parent exists
      if (newParentId !== null) {
        const parentMg = currentData.management_groups.find(mg => mg.id === newParentId);
        if (parentMg && !parentMg.exists) {
          alert('A management group cannot exist if its parent is planned. Please set the parent to "exists" first.');
          isUpdating = true;
          mgExistsCheckbox.checked = false;
          isUpdating = false;
          return;
        }
      }
    }

    if (e.target === mgExistsCheckbox && !newExists) {
      // Check if setting this MG to non-existent would conflict with existing children
      const hasExistingChildren = currentData.management_groups.some(
        mg => mg.parent_id === selectedMgId && mg.exists
      );

      if (hasExistingChildren) {
        alert('Cannot mark this management group as planned when it has existing children. Please update the children first.');
        isUpdating = true;
        mgExistsCheckbox.checked = true;
        isUpdating = false;
        return;
      }
    }

    if (e.target === mgParentIdSelect) {
      // Check for cycles when changing parent
      if (wouldCreateCycle(selectedMgId, newParentId)) {
        alert("Cannot move a management group to one of its descendants.");
        isUpdating = true;
        mgParentIdSelect.value = currentMg.parent_id === null ? 'null' : currentMg.parent_id;
        isUpdating = false;
        return;
      }

      // Check if new parent exists when this MG exists
      if (currentMg.exists && newParentId !== null) {
        const parentMg = currentData.management_groups.find(mg => mg.id === newParentId);
        if (parentMg && !parentMg.exists) {
          alert('A management group cannot exist if its parent is planned. Please set the parent to "exists" first.');
          isUpdating = true;
          mgParentIdSelect.value = currentMg.parent_id === null ? 'null' : currentMg.parent_id;
          isUpdating = false;
          return;
        }
      }
    }

    // Update values (removed archetypes as they're now handled separately)
    currentMg.display_name = newDisplayName;
    currentMg.parent_id = newParentId;
    currentMg.exists = newExists;

    // Update tree and JSON preview
    renderManagementGroups();
    updateJsonPreview();

    // Re-select the current management group
    selectManagementGroup(selectedMgId);
  }