document.addEventListener('DOMContentLoaded', async function () { const {email, password, name} = await window.electronAPI.readUserConfig('user_info'); const emailInput = document.querySelector('input[name="email"]'); const usernameInput = document.querySelector('input[name="username"]'); const passwordInput = document.querySelector('input[name="password"]'); // Pre-fill form fields with existing data emailInput.value = email; usernameInput.value = name; passwordInput.value = password; const backButton = document.getElementById('back'); const submitButton = document.getElementById('submit'); // Get operation codes from the backend const operationCodes = await window.electronAPI.getOperationsCodes(); if (!operationCodes) { await window.electronAPI.showAlert('Internal error of the application.'); return; } const codeModifyUser = operationCodes.MODIFY_USER; const codeOk = operationCodes.OK; // Handle the back button click backButton.addEventListener('click', async function () { console.log('Back button clicked!'); fadeOut('main_menu'); }); // Handle the submit button click submitButton.addEventListener('click', async function (e) { e.preventDefault(); console.log('Submit button clicked!'); // Fetch form values const email = emailInput.value; const name = usernameInput.value; const password = passwordInput.value; const { id, departmentId } = await window.electronAPI.readUserConfig('user_info'); // Fetch login data const app_type = await window.electronAPI.readUserConfig('app_type'); // Fetch app type // Prepare the data to be sent via the UC socket const messageData = { id: id, name: name, email: email, password: password, departmentId: departmentId, app_type: app_type // Include app_type in the payload }; // Open UC socket if (!await window.electronAPI.openUcSocket()) { await window.electronAPI.showAlert('Failed to open socket. Internal error of the application.'); return; } // Send the message to the server using the UC socket if (!await window.electronAPI.sendUcMessage(codeModifyUser, messageData)) { await window.electronAPI.showAlert('Failed to send message.'); return; } // Wait for the response const response = await waitForResponse(); if (response && response.operationCode === codeOk) { console.log('User update successful.'); // Save updated user info to the userConfig await window.electronAPI.writeUserConfig('user_credentials', { email: email, password: password }); await window.electronAPI.writeUserConfig('user_info', {id: id, departmentId: departmentId, name: name}); // Navigate back to the main menu fadeOut('main_menu'); } else { console.log('Error updating user:', response?.metaInfo?.message || 'Unknown error'); await window.electronAPI.showAlert(response?.metaInfo?.message || 'Unknown error occurred'); } }); });