// renderer.js document.addEventListener('DOMContentLoaded', () => { console.log('DOM fully loaded. renderer.js is now running.'); // --- 0. INITIALIZE FORM DEFAULTS --- const publicationDateInput = document.getElementById('publicationDate'); if (publicationDateInput) { publicationDateInput.value = new Date().toISOString().split('T')[0]; } // --- 1. TAB SWITCHING LOGIC --- const tabButtons = document.querySelectorAll('.tab-btn'); const tabContents = document.querySelectorAll('.tab-content'); tabButtons.forEach(button => { button.addEventListener('click', () => { tabButtons.forEach(btn => btn.classList.remove('active')); tabContents.forEach(content => content.classList.remove('active')); button.classList.add('active'); document.getElementById(button.dataset.tab + '-tab').classList.add('active'); }); }); // Set the initial active tab content document.getElementById('machine-tab').classList.add('active'); // --- 2. DYNAMIC ALARM ROW LOGIC --- const addAlarmBtn = document.getElementById('add-alarm-btn'); const alarmsTableBody = document.getElementById('alarms-table-body'); const alarmTemplate = document.getElementById('alarm-row-template'); addAlarmBtn.addEventListener('click', () => { const currentIdInputs = alarmsTableBody.querySelectorAll('[name="alarm_id"]'); let maxId = 0; currentIdInputs.forEach(input => { const currentId = parseInt(input.value, 10); if (!isNaN(currentId) && currentId > maxId) { maxId = currentId; } }); const nextId = maxId + 1; const paddedId = nextId.toString().padStart(3, '0'); const clone = alarmTemplate.content.cloneNode(true); clone.querySelector('[name="alarm_id"]').value = nextId; clone.querySelector('[name="alarm_name"]').value = `ALARM_${paddedId}`; const removeBtn = clone.querySelector('.remove-row-btn'); removeBtn.addEventListener('click', (e) => { e.target.closest('tr').remove(); }); alarmsTableBody.appendChild(clone); }); // --- 3. FORM SUBMISSION LOGIC --- const form = document.getElementById('machine-form'); if (form) { form.addEventListener('submit', async (event) => { event.preventDefault(); // A. Collect data from the main "Machine" tab using FormData const formData = new FormData(form); // Convert FormData to a plain object const mainData = {}; for (const [key, value] of formData.entries()) { // This check prevents multi-value fields from overwriting themselves if (!mainData[key]) { mainData[key] = value; } } mainData.addins = formData.getAll('addins'); // B. Collect all alarm rows from the table const alarmRows = alarmsTableBody.querySelectorAll('tr'); const alarmsData = []; alarmRows.forEach(row => { const alarm = { id: row.querySelector('[name="alarm_id"]').value, name: row.querySelector('[name="alarm_name"]').value, message: row.querySelector('[name="alarm_message"]').value, }; if (alarm.message) { alarmsData.push(alarm); } }); // C. Combine all data into a single object const finalData = { ...mainData, alarms: alarmsData }; console.log('Sending all collected data to main process:', finalData); try { const result = await window.electronAPI.submitForm(finalData); if (result.success) { // // Reset the entire form to its initial state // form.reset(); // // Re-apply the defaults that are not part of the standard reset // document.getElementById('publicationDate').value = new Date().toISOString().split('T')[0]; // document.querySelector('#stopReasonYes').checked = true; // Set StopReason back to Yes // updateFilename(); // Clear the filename preview } } catch (error) { console.error('Error during form submission process:', error); } }); } });