with open('app/modules/sag/templates/detail.html', 'r', encoding='utf-8') as f: text = f.read() # Replace HTML for timeForm html_start = text.find('
') html_end = text.find('
', html_start) + 7 new_html = """
Min.
""" if html_start != -1 and html_end != -1: text = text[:html_start] + new_html + text[html_end:] print("Replaced timeForm HTML.") # Modify reset logic in showAddTimeModal reset_start = text.find('if(document.getElementById(\'time_hours_input\')) {') reset_end = text.find('}', reset_start) + 1 if reset_start != -1: new_reset = """if(document.getElementById('time_total_minutes')) { document.getElementById('time_total_minutes').value = ''; document.getElementById('time_start_input').value = ''; document.getElementById('time_end_input').value = ''; }""" text = text[:reset_start] + new_reset + text[reset_end:] print("Replaced modal form reset.") # Delete old updateTimeTotal function, add bindTimeModalCalculations updateTotalStart = text.find('function updateTimeTotal() {') updateTotalEnd = text.find('}', updateTotalStart) + 1 if updateTotalStart != -1: new_updateTotal = """function bindTimeModalCalculations() { const startIn = document.getElementById('time_start_input'); const endIn = document.getElementById('time_end_input'); const minIn = document.getElementById('time_total_minutes'); if (!startIn || !endIn || !minIn) return; const parseTime = (val) => { if (!val) return null; const [h,m] = val.split(':').map(Number); return (h * 60) + m; }; const toTimeStr = (totalMins) => { const h = Math.floor(totalMins / 60) % 24; const m = totalMins % 60; return `${h.toString().padStart(2,'0')}:${m.toString().padStart(2,'0')}`; }; const recalculate = (trigger) => { const s = parseTime(startIn.value); const e = parseTime(endIn.value); const dur = parseInt(minIn.value); if (trigger === 'start' || trigger === 'end') { if (s !== null && e !== null) { let diff = e - s; if (diff < 0) diff += 24*60; minIn.value = diff; } else if (s !== null && !isNaN(dur) && dur > 0 && !endIn.value) { endIn.value = toTimeStr(s + dur); } else if (e !== null && !isNaN(dur) && dur > 0 && !startIn.value) { let base = e - dur; while (base < 0) base += 24*60; startIn.value = toTimeStr(base); } } else if (trigger === 'min') { if (s !== null && !isNaN(dur) && dur > 0) { endIn.value = toTimeStr(s + dur); } else if (e !== null && !isNaN(dur) && dur > 0 && !startIn.value) { let base = e - dur; while(base < 0) base+=24*60; startIn.value = toTimeStr(base); } } }; startIn.addEventListener('change', () => recalculate('start')); endIn.addEventListener('change', () => recalculate('end')); minIn.addEventListener('input', () => recalculate('min')); }""" text = text[:updateTotalStart] + new_updateTotal + text[updateTotalEnd:] print("Replaced updateTimeTotal with bindTimeModalCalculations") # Fix listeners initialization dom_start = text.find('const hInput = document.getElementById(\'time_hours_input\');') dom_end = text.find('if(mInput) mInput.addEventListener(\'input\', updateTimeTotal);', dom_start) + 63 if dom_start != -1: text = text[:dom_start] + "bindTimeModalCalculations();" + text[dom_end:] print("Fixed DOM listeners") # Replace saveTime body part logic: calculate minutes explicitly from `time_total_minutes` save_start = text.find('async function saveTime() {') save_end = text.find('const isInternal = document.getElementById(\'time_internal\')?.checked || false;', save_start) if save_start != -1: new_save = """async function saveTime() { const mInput = document.getElementById('time_total_minutes'); const minVal = parseInt(mInput ? mInput.value : 0); if (!minVal || minVal <= 0) { alert('Indtast en gyldig varighed (minutter).'); return; } const totalHours = minVal / 60; """ text = text[:save_start] + new_save + text[save_end:] print("Updated saveTime first half.") # Note: saveTime uses `POST /api/v1/cases/${sagId}/time` or similar, wait let me check the actual fetch path. # Let's check `saveTime` first before committing blindly. I will just do the above first, then verify `saveTime`. with open('app/modules/sag/templates/detail.html', 'w', encoding='utf-8') as f: f.write(text)