mne-nuzhno-sdelat-sayt-ili-p/script.js

102 lines
3.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = '<div class="kt-ai-empty">История пуста</div>';
return;
}
historyList.innerHTML = history.map(calc => {
const date = new Date(calc.date).toLocaleString('ru-RU');
const total = calc.results.total.fteWithAbsence.toFixed(1);
return `
<div class="kt-ai-card" style="padding:var(--kt-ai-space-4);margin-bottom:var(--kt-ai-space-3)">
<div style="display:flex;justify-content:space-between;align-items:center">
<div>
<strong>${total} FTE</strong>
<span style="color:var(--kt-ai-fg-muted);margin-left:var(--kt-ai-space-2);font-size:var(--kt-ai-text-sm)">${date}</span>
</div>
<div style="font-size:var(--kt-ai-text-sm);color:var(--kt-ai-fg-muted)">
Вх: ${calc.results.incoming ? calc.results.incoming.fte : '-'} |
Исх: ${calc.results.outgoing ? calc.results.outgoing.fte : '-'} |
Чаты: ${calc.results.chats ? calc.results.chats.fte : '-'}
</div>
</div>
</div>
`;
}).join('');
} catch (error) {
console.error('Ошибка загрузки истории:', error);
}
}
loadHistory();
});