'use strict';
/* admin → tpl (templates: courses + lessons) section */
(function () {
'use strict';
let inited = false;
async function load() {
try {
const [courses, lessons] = await Promise.all([
LS.getCourseTemplates().catch(() => []),
LS.getLessonTemplates().catch(() => [])
]);
renderTplTable('tpl-course-body', courses, 'courses');
renderTplTable('tpl-lesson-body', lessons, 'lessons');
if (window.lucide) lucide.createIcons();
} catch(e) {
document.getElementById('tpl-course-body').innerHTML = `
| Ошибка: ${esc(e.message)} |
`;
}
}
function renderTplTable(bodyId, items, type) {
const body = document.getElementById(bodyId);
if (!items || !items.length) {
body.innerHTML = '| Нет шаблонов |
';
return;
}
body.innerHTML = items.map(t => `
| ${t.id} |
${esc(t.name || t.title || '—')} |
${esc(t.subject || '—')} |
${esc(t.category || '—')} |
${esc(t.author_name || t.author || '—')} |
|
|
`).join('');
}
async function tplTogglePublic(type, id, isPublic) {
try {
const endpoint = type === 'courses' ? '/api/templates/courses/' : '/api/templates/lessons/';
await LS.api(endpoint + id, { method: 'PUT', body: JSON.stringify({ is_public: isPublic ? 1 : 0 }) });
LS.toast(isPublic ? 'Шаблон опубликован' : 'Шаблон скрыт', 'success');
} catch(e) { LS.toast('Ошибка: ' + e.message, 'error'); }
}
async function tplDelete(type, id) {
if (!confirm('Удалить шаблон #' + id + '?')) return;
try {
if (type === 'courses') await LS.deleteCourseTemplate(id);
else await LS.deleteLessonTemplate(id);
LS.toast('Шаблон удалён', 'success');
inited = false;
await load();
inited = true;
} catch(e) { LS.toast('Ошибка: ' + e.message, 'error'); }
}
window.tplTogglePublic = tplTogglePublic;
window.tplDelete = tplDelete;
window.AdminSections = window.AdminSections || {};
window.AdminSections.tpl = {
init: async () => { if (inited) return; inited = true; await load(); },
reload: load,
};
})();