'use strict';
/* admin → sublog (submission log) section */
(function () {
'use strict';
let inited = false;
const SL_STATUSES = { new:'На проверке', reviewed:'Проверено', accepted:'Принято', revision:'На доработке', resubmitted:'Повторно' };
async function load() {
const el = document.getElementById('sublog-list');
const countEl = document.getElementById('sublog-count');
const classId = document.getElementById('sublog-class-filter').value;
el.innerHTML = '
';
countEl.textContent = '';
try {
const url = classId ? `/api/submissions/log?class_id=${classId}` : '/api/submissions/log';
const rows = await LS.api(url);
// Populate class filter on first load
const sel = document.getElementById('sublog-class-filter');
if (sel.options.length <= 1 && rows.length) {
const classMap = new Map();
rows.forEach(r => { if (r.class_id && r.class_name) classMap.set(r.class_id, r.class_name); });
classMap.forEach((name, id) => {
const opt = document.createElement('option');
opt.value = id; opt.textContent = name;
sel.appendChild(opt);
});
}
countEl.textContent = rows.length ? `${rows.length} записей` : '';
if (!rows.length) {
el.innerHTML = ``;
if (window.lucide) lucide.createIcons({ nodes: [el] });
return;
}
const ROLE_LABELS = { admin: 'Админ', teacher: 'Учитель', student: 'Ученик' };
el.innerHTML = `
| Дата |
Ученик |
Файл |
Задание |
Класс |
Статус |
Оценка |
Удалил |
${rows.map(r => {
const dt = r.deleted_at ? new Date(r.deleted_at.includes('T') ? r.deleted_at : r.deleted_at.replace(' ','T')+'Z') : null;
const dateStr = dt ? dt.toLocaleDateString('ru',{day:'numeric',month:'short'}) + ' ' + dt.toLocaleTimeString('ru',{hour:'2-digit',minute:'2-digit'}) : '—';
const initials = (r.student_name || '?').split(' ').slice(0,2).map(w => w[0]?.toUpperCase() || '').join('');
const st = r.status || 'new';
const gradeVal = r.grade != null ? r.grade : null;
const gradeCls = gradeVal != null ? (gradeVal >= 80 ? 'sl-grade-hi' : gradeVal >= 50 ? 'sl-grade-mid' : 'sl-grade-lo') : 'sl-grade-none';
const roleCls = 'sl-role-' + (r.deleted_by_role || 'student');
return `
| ${dateStr} |
${initials}${esc(r.student_name || '—')} |
${esc(r.original_name || '—')} |
${esc(r.assignment_title || '—')} |
${esc(r.class_name || '—')} |
${SL_STATUSES[st] || st} |
${gradeVal != null ? gradeVal : '—'} |
${esc(r.deleted_by_name || '—')} ${ROLE_LABELS[r.deleted_by_role] || r.deleted_by_role || '?'} |
`;
}).join('')}
`;
document.getElementById('btn-clear-sublog').style.display = '';
} catch (e) {
el.innerHTML = `Ошибка: ${esc(e.message)}
`;
}
}
async function clearSubmissionLog() {
if (!await LS.confirm('Очистить весь журнал удалённых работ? Это действие необратимо.', { title: 'Очистка журнала', confirmText: 'Очистить', danger: true })) return;
try {
await LS.api('/api/submissions/log', { method: 'DELETE' });
document.getElementById('btn-clear-sublog').style.display = 'none';
document.getElementById('sublog-count').textContent = '';
document.getElementById('sublog-list').innerHTML = ``;
if (window.lucide) lucide.createIcons({ nodes: [document.getElementById('sublog-list')] });
LS.toast('Журнал очищен', 'success');
} catch (e) { LS.toast('Ошибка: ' + e.message, 'error'); }
}
// Expose handlers used by HTML onclicks
window.loadSubmissionLog = load;
window.clearSubmissionLog = clearSubmissionLog;
window.AdminSections = window.AdminSections || {};
window.AdminSections.sublog = {
init: async () => { if (inited) return; inited = true; await load(); },
reload: load,
};
})();