document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('wfmForm'); const resultsSection = document.getElementById('results'); const historyList = document.getElementById('historyList'); form.addEventListener('submit', async function(e) { e.preventDefault(); const formData = new FormData(form); const params = {}; formData.forEach((value, key) => { if (value !== '') { params[key] = parseFloat(value); } }); try { const response = await fetch('/api/calculate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); const results = await response.json(); displayResults(results); loadHistory(); resultsSection.style.display = 'block'; resultsSection.scrollIntoView({ behavior: 'smooth', block: 'start' }); } catch (error) { alert('Ошибка расчёта: ' + error.message); } }); function displayResults(results) { if (results.incoming) { document.getElementById('resIncoming').textContent = results.incoming.fte; document.getElementById('resIncomingSL').textContent = 'SL: ' + results.incoming.sl + '%'; } else { document.getElementById('resIncoming').textContent = '-'; document.getElementById('resIncomingSL').textContent = ''; } if (results.outgoing) { document.getElementById('resOutgoing').textContent = results.outgoing.fte; } else { document.getElementById('resOutgoing').textContent = '-'; } if (results.tickets) { document.getElementById('resTickets').textContent = results.tickets.fte; } else { document.getElementById('resTickets').textContent = '-'; } if (results.chats) { document.getElementById('resChats').textContent = results.chats.fte; } else { document.getElementById('resChats').textContent = '-'; } document.getElementById('resTotalSchedule').textContent = results.total.fteInSchedule.toFixed(1); document.getElementById('resTotalWithAbsence').textContent = results.total.fteWithAbsence.toFixed(1); } async function loadHistory() { try { const response = await fetch('/api/history'); const history = await response.json(); if (history.length === 0) { historyList.innerHTML = '
История пуста
'; return; } historyList.innerHTML = history.map(calc => { const date = new Date(calc.date).toLocaleString('ru-RU'); const total = calc.results.total.fteWithAbsence.toFixed(1); return `
${total} FTE ${date}
Вх: ${calc.results.incoming ? calc.results.incoming.fte : '-'} | Исх: ${calc.results.outgoing ? calc.results.outgoing.fte : '-'} | Чаты: ${calc.results.chats ? calc.results.chats.fte : '-'}
`; }).join(''); } catch (error) { console.error('Ошибка загрузки истории:', error); } } loadHistory(); });