addEventListener("load", function() {
movementMenu("managerDiv");
movementMenu("managerProperties");

//менеджер папок
let managerDiv = document.getElementById('managerDiv');
managerData(currentPath);

function managerFun() { //кнопки и редактируемые поля в менеджере
   document.getElementById('managerCloseFun').onclick = function() { 
      managerDiv.style.visibility = "hidden";
   };
   document.getElementById('managerHistoryBackFun').onclick = function() {
      if (managerHistoryIndex > 0) {
         managerHistoryIndex--;
         managerData(managerHistoryPaths[managerHistoryIndex]);
      }
   };
   document.getElementById('managerHistoryForwFun').onclick = function() {
      if (managerHistoryIndex < managerHistoryPaths.length - 1) {
         managerHistoryIndex++;
         managerData(managerHistoryPaths[managerHistoryIndex]);
      }
   };
   document.getElementById('managerBackFun').onclick = function() {
      managerData(removeLastSegment(currentPath));
   };
   
   document.getElementById('managerSettingsCopy').onclick = function() {
      managerClipboard("copy", "{{copy}}");
   };
   document.getElementById('managerSettingsCut').onclick = function() {
      managerClipboard("cut", "{{cut}}");
   };
   function managerClipboard(action, messageText) {
      if (managerTableDivFilePath) {
         createAjaxRequest({ managerClipboard: managerTableDivFilePath, action: action, handleRequestAction: "managerClipboard" }, function(data) {
            if (data.message === 1) {
               messageFunction("{{file}}" + messageText + " {{to clipboard}}!");
            } else {
               messageFunction("{{error when}}" + messageText + " {{file}}!");
            }
            /* managerData(currentPath); */
         });
      } else {
         messageFunction("{{right_click_to_select_file}}");
      }
   }
   
   let managerSettingsInsertId = document.getElementById('managerSettingsInsert');
   managerSettingsInsertId.onclick = function() {
      let title;
      createAjaxRequest({ name: title, currentPath: currentPath, managerSettingsInsert: "managerSettingsInsert", handleRequestAction: "checkSameName" }, function(data) {
         if (data.exists) {
            messageFunction("{{file_with_same_name_exists}}");
         } else { 

            createAjaxRequest({ managerSettingsInsert: currentPath, handleRequestAction: "managerSettingsInsert" }, function(data) {
               if (data.message === 1) {
                  messageFunction("{{file_pasted_successfully}}");
               } else {
                  let errorMessage = data.error ? data.error : "{{file_paste_unknown_error}}";
                  messageFunction("{{error}}: " + errorMessage);
               }
               managerData(currentPath);
            });
            
         }
      });   
   };    
   
   let managerSettingsRenameId = document.getElementById('managerSettingsRename');
   managerSettingsRenameId.onclick = function() {
      if (managerTableDivFilePath) {
         let invalidCharacters = /[\/\\:*?"<>|]/;
         let managerTableDivFileName = managerTableDivFilePath.split(/[/\\]/).pop();
         let title = prompt("{{enter_new_name}}", managerTableDivFileName);
         let isFolder = !/\./.test(managerTableDivFileName);
    
   
         if (title !== null) {
            if (title && !invalidCharacters.test(title) && (!isFolder || !title.includes('.'))) {
               if (confirm("{{rename_confirm}} " + title + "?")) {

                  createAjaxRequest({ name: title, currentPath: currentPath, handleRequestAction: "checkSameName" }, function(data) {
                     if (data.exists) {
                        messageFunction("{{file_with_same_name_exists}}");
                     } else {
                        createAjaxRequest({ managerSettingsRename: managerTableDivFilePath, managerNamePath: title, handleRequestAction: "managerSettingsRename" }, function(data) {
                           if (data.message === 1) {
                              messageFunction("{{rename_success}}");
                           } else {
                              messageFunction("{{rename_error}}");
                           }
                           managerData(currentPath);
                        });
                     }
                  });    

               }
            } else {
               messageFunction("{{invalid_name_error}}");
            }
         }
      } else {
         messageFunction("{{right_click_to_select_file}}");
      }
   };
   
   let managerSettingsDeleteId = document.getElementById('managerSettingsDelete');
   managerSettingsDeleteId.onclick = function() {
      if (managerTableDivFilePath) {
         if (confirm("{{delete_confirm}}")) {
            createAjaxRequest({ managerSettingsDelete: managerTableDivFilePath, handleRequestAction: "managerSettingsDelete" }, function(data) {
               if (data.message === 1) {
                  messageFunction("{{delete_success}}");
               } else {
                  messageFunction("{{delete_error}}");
               }
               managerData(currentPath);
            });
         }
      } else {
         messageFunction("{{right_click_to_select_file}}");
      }
   };
   
   document.getElementById('managerSettingsButtonCreateFolder').onclick = function() {
      createManagerItem("{{folder}}", "{{enter_new_folder_name}}", "{{invalid_folder_name}}", "{{folder_created_successfully}}");
   };
   document.getElementById('managerSettingsButtonCreateFile').onclick = function() {
      createManagerItem("{{file}}", "{{enter_new_file_name}}", "{{invalid_file_name}}", "{{file_created_successfully}}");
   };

   function createManagerItem(type, promptMessage, errorMessage, successMessage, nameSuffix = '') {
      let title = prompt(promptMessage);
      if (title !== null) {
         let invalidCharacters = /[\/\\:*?"<>|]/;
         let isValidTitle = title && !invalidCharacters.test(title) && !title.startsWith('.') && !title.endsWith('.');
         if (type === "{{folder}}") {
            isValidTitle = isValidTitle && !title.includes('.');
         }
         if (isValidTitle) {
            title += nameSuffix;
            if (confirm("{{create}} " + type + " {{with_name}} " + title + "?")) {
               
               createAjaxRequest({ name: title, currentPath: currentPath, handleRequestAction: "checkSameName" }, function(data) {
                  if (data.exists) {
                     messageFunction("{{file_with_same_name_exists}}!");
                  } else {
                     createAjaxRequest({ managerSettingsCreate: title, managerType: type, managerNamePath: currentPath, handleRequestAction: "managerSettingsCreate", },
                        function (data) {
                           if (data.message === 0) {
                              messageFunction(successMessage);
                           } else if (data.message === "noSuccess") {
                              messageFunction("{{create_error}} " + type + "!");
                           } else if (data.message === "checkItemExists") {
                              messageFunction(type + " {{item_already_exists}}");
                           } else {
                              messageFunction("{{unknown_error}}");
                           }
                           managerData(currentPath);
                        }
                     );   
                  }
               });     
  
            }
         } else {
            messageFunction(errorMessage);
         }
      }
   }
   
   let managerSettingsPropertiesId = document.getElementById('managerSettingsProperties');;
   managerSettingsPropertiesId.onclick = function() {
      if (managerTableDivFilePath) {
         createAjaxRequest({ managerSettingsProperties: managerTableDivFilePath, handleRequestAction: "managerSettingsProperties" }, function(data) {
            let managerPropertiesId = document.getElementById('managerProperties');
            let managerPropertiesDivId = document.getElementById('managerPropertiesDiv');
            let managerPropertiesTopNameId = document.getElementById('managerPropertiesTopName');
            let managerPropertiesWindowPropertiesId = document.getElementById('managerPropertiesWindowProperties');
            let managerPropertiesWindowRightsId = document.getElementById('managerPropertiesWindowRights');

            let tableProperties = document.createElement('table');
            tableProperties.style.width = "100%";
            data.forEach(item => {
               let row = document.createElement('tr');
               let labelCell = document.createElement('td');
               let valueCell = document.createElement('td');
               labelCell.className = 'managerPropertiesDivDivs';
               valueCell.className = 'managerPropertiesDivDivs';
               labelCell.textContent = item.label;
               valueCell.textContent = item.value;
               row.appendChild(labelCell);
               row.appendChild(valueCell);
               tableProperties.appendChild(row);
            });
         
            let tableRights = document.createElement('div');
            tableRights.innerHTML= "{{no_rights_yet}}";
         
            managerPropertiesTopNameId.textContent = "{{properties}} " + data[0].value;
            managerPropertiesDivId.innerHTML = '';
         
            managerPropertiesWindowPropertiesId.onclick = function() {
               managerPropertiesDivId.innerHTML = '';
               managerPropertiesDivId.appendChild(tableProperties);
               managerPropertiesWindowPropertiesId.style.backgroundColor = "#f3f3f3";
               managerPropertiesWindowRightsId.style.backgroundColor = "";
            };
            managerPropertiesWindowRightsId.onclick = function() {
               managerPropertiesDivId.innerHTML = '';
               managerPropertiesDivId.appendChild(tableRights);
               managerPropertiesWindowPropertiesId.style.backgroundColor = "";
               managerPropertiesWindowRightsId.style.backgroundColor = "#f3f3f3";
            };         
            managerPropertiesWindowPropertiesId.click();

            if (managerPropertiesId.style.visibility == 'hidden') {
               managerPropertiesId.style.visibility = 'visible';
            }
         
            let managerPropertiesTopCloseId = document.getElementById('managerPropertiesTopClose');
            let managerPropertiesDivButtonOkId = document.getElementById('managerPropertiesDivButtonOk');
            let managerPropertiesDivButtonCancelId = document.getElementById('managerPropertiesDivButtonCancel');
            managerPropertiesTopCloseId.onclick = function() {
               managerPropertiesId.style.visibility = 'hidden';
            };
            managerPropertiesDivButtonOkId.onclick = function() {
               managerPropertiesId.style.visibility = 'hidden';
            };
            managerPropertiesDivButtonCancelId.onclick = function() {
               managerPropertiesId.style.visibility = 'hidden';
            };            

         });
      } else {
         messageFunction("{{right_click_to_select_file}}");
      }
   };
   
   //загрузка файла для менеджера
   let managerSettingsLoadId = document.getElementById('managerSettingsLoad');
   managerSettingsLoadId.onclick = function() {
      let fileInput = document.createElement('input');
      fileInput.type = 'file';
      fileInput.addEventListener('change', function() {

         createAjaxRequest({ name: fileInput.files[0].name, currentPath: currentPath, handleRequestAction: "checkSameName" }, function(data) {
            if (data.exists) {
               messageFunction("{{file_with_same_name_exists}}");
            } else {
               
                let formData = new FormData()
                formData.append('pathFile', fileInput.files[0])
                formData.append('pathLoad', currentPath)
                handleJsonRpcRequest('pathLoad', formData, 1)
                    .then(result => {
                        if (result.error) {
                            messageFunction("{{file_with_same_name_exists}}")
                        } else {
                            messageFunction("{{file_uploaded_successfully}}")
                            managerData(currentPath)
                        }
                    })
                    .catch(e => {
                        console.error('Upload error:', e)
                    })

            }
         });    
         

      });
      fileInput.click();
   };   
}

function removeLastSegment(str) { //удаление текста после последнего "/"
   const segments = str.split('/');
   if (segments.length > 1) {
      segments.pop();
   }
   return segments.join('/');
}

//окно менеджера настроек
function managerSettings() {
    let managerDiv = document.getElementById('managerDiv');
    let managerSettingsDiv = document.getElementById('managerSettings');

    managerDiv.addEventListener('contextmenu', managerSettingsClick);
    touchLong(managerDiv, managerSettingsClick);
}

if (isPhone) document.getElementById('managerDiv').style.paddingBottom = "150px"
if (!isPhone) {
    document.querySelectorAll('.managerSettingsButtons').forEach(btn=>{
        btn.style.backgroundColor='rgba(255, 255, 255, 1)'
        btn.style.borderRadius='5px'
        btn.style.padding='2px'
        btn.style.margin='3px'
        btn.style.cursor='pointer'
        btn.style.display='block'
        btn.addEventListener('mouseover',()=>btn.style.color='#787878')
        btn.addEventListener('mouseout',()=>btn.style.color='')
    })
} else {
    document.querySelectorAll('.managerSettingsButtons').forEach(btn=>{
        btn.style.backgroundImage='url(../../img/pict/b_iconslyb.svg)'
        btn.style.height='42px'
        btn.style.minWidth='42px'
        btn.style.setProperty('background-size','calc(1122px* 1.5)','important')
        btn.style.display='inline-block'
        btn.style.borderRadius='5px'
        btn.style.cursor='pointer'
        btn.style.display='flex'
        btn.style.flexDirection='column'
        btn.style.alignItems='center'
        btn.style.fontSize='10px'
        btn.style.lineHeight='1'
        btn.style.justifyContent = 'flex-end'
        btn.style.top='2px'
        btn.style.position = 'relative'
        btn.style.width = 'auto'
    })
    let wrap = document.getElementById('managerSettings')
    wrap.style.display = 'flex'
    wrap.style.maxWidth = '-webkit-fill-available'
    wrap.style.overflowX = 'auto'
    wrap.style.overflowY = 'hidden' 
    wrap.style.justifyContent = 'center' 
    let div = document.getElementById('managerSettingsDiv')
    div.style.height = '-webkit-fill-available'
    div.style.display = 'inline-flex'
    div.style.whiteSpace = 'nowrap'
    div.style.width = 'max-content'
    div.style.gap = '7px'
    div.style.alignItems = 'center'
}
function managerSettingsClick(event) {
    event.preventDefault();
    let managerSettingsDiv = document.getElementById('managerSettings');
    if (!isPhone) {
        managerSettingsDiv.style.left = `${touchX}px`;
        managerSettingsDiv.style.top = `${touchY}px`;
    } else {
        managerSettingsDiv.style.bottom = '15px';
        managerSettingsDiv.style.width = 'calc(100vw - 42px)';
        managerSettingsDiv.style.height = '42px';
        managerSettingsDiv.style.left = '15px';
        managerSettingsDiv.style.top = 'auto';
        managerSettingsDiv.style.boxShadow = 'none';
    }

    let ids = [
        'managerSettingsCopy',
        'managerSettingsCut',
        'managerSettingsRename',
        'managerSettingsDelete',
        'managerSettingsProperties',
        'managerSettingsLoad',
        'managerSettingsInsert',
        'managerSettingsButtonCreateFolder',
        'managerSettingsButtonCreateFile'
    ];
    ids.forEach(id => {
        let el = document.getElementById(id);
        if (el) {
            if (el.dataset.oldDisplay === undefined) {
                el.dataset.oldDisplay = getComputedStyle(el).display;
            }
            el.style.display = 'none';
        }
    });

    if (event.target.closest('.managerTableDivFile')) {
        ['managerSettingsCopy','managerSettingsCut','managerSettingsRename','managerSettingsDelete','managerSettingsProperties']
        .forEach(id => {
            let el = document.getElementById(id);
            el.style.display = el.dataset.oldDisplay;
        });
    } else {
        document.getElementById('managerSettingsLoad').style.display = document.getElementById('managerSettingsLoad').dataset.oldDisplay;
        createAjaxRequest(
            { checkFolderForFile: "$_SESSION['managerClipboardFile']['path']", handleRequestAction: "checkFolderForFile" },
            function(data) {
                if (data.message === 1) {
                    let el = document.getElementById('managerSettingsInsert');
                    el.style.display = el.dataset.oldDisplay;
                }
            }
        );
        ['managerSettingsButtonCreateFolder','managerSettingsButtonCreateFile']
        .forEach(id => {
            let el = document.getElementById(id);
            el.style.display = el.dataset.oldDisplay;
        });
    }

    managerSettingsDiv.style.visibility = 'visible';
    document.addEventListener('pointerdown', function hideMenu(e) {
        if (!managerSettingsDiv.contains(e.target)) {
            managerSettingsDiv.style.visibility = 'hidden';
            document.removeEventListener('pointerdown', hideMenu);
        }
    });
}

function managerPathContext(event){
    let targetElement=event.target.closest('[path]')
    if(targetElement){
        let pathValue=targetElement.getAttribute('path')
        managerTableDivFilePath=pathValue
    }
}
document.addEventListener('contextmenu',managerPathContext)
touchLong(document, managerPathContext)

/* функция сохранение файла через сохронить как */
function saveHow() {
   let currentPathHow = currentPath;
   if (currentPathHow.startsWith('/')) {
      currentPathHow = currentPathHow.slice(1);
   }
   if (!currentPathHow.endsWith('/')) {
      currentPathHow += '/';
   }
   window.saveContentIdHow(currentPathHow);
}

/* функции открытие страницы */
function openPageBut() {
   if (openPageButPath != "no/Select") {
      openPage(openPageButPath);   
   } else {
      messageFunction('{{select_file_ending_with_page_php}}');
   }
}

/* функции выбора страницы */
function propertiesUrlFun() {
   let saveHowNameValue = document.getElementById('saveHowName').value;
   if (!saveHowNameValue.includes('.page.php')) return;
   let newValue = saveHowNameValue.replace(/\.page\.php/, "");
   let cp = currentPath;
   if (cp.charAt(0) === "/") {
      cp = cp.substring(1);
   }
   document.getElementById('treePropertiesDivUrlValue').innerHTML = cp + "/" + newValue;
   window.managerDataAction = "";
   managerDiv.style.visibility = "hidden";
}

/* функции выбора страницы */
function selectImgFormButFun() {
    var rawPath = document.getElementById('managerPath').textContent;
    var cleanPath = rawPath.trim().replace(/\s*\/\s*/g, '/').replace(/\s+/g, '');
    var fileName = document.getElementById('saveHowName').value.trim();
    var fullPath = cleanPath + fileName;
    if (fullPath.startsWith('/')) {
        fullPath = fullPath.slice(1);
    }
    var img = document.createElement("img");
    img.src = fullPath;
    img.setAttribute("style", "float: left; margin: 10px; width: 250px; border: 0px solid rgb(0, 0, 0); overflow: hidden;");
    var sel = window.getSelection();
    if (sel.rangeCount) {
        var range = sel.getRangeAt(0);
        range.deleteContents();
        range.insertNode(img);
    }
    window.managerDataAction = "";
    managerDiv.style.visibility = "hidden";
}

window.managerSettings = managerSettings;
window.managerFun = managerFun;
window.saveHow = saveHow;
window.openPageBut = openPageBut;
window.propertiesUrlFun = propertiesUrlFun;
window.selectImgFormButFun = selectImgFormButFun;

});/* начало */