(function () { const chartStore = new Map(); function qs(selector, root) { return (root || document).querySelector(selector); } function qsa(selector, root) { return Array.from((root || document).querySelectorAll(selector)); } function buildChartForClass(classId, report) { if (!classId || chartStore.has(classId) || typeof Chart === "undefined") { return; } const sections = Array.isArray(report.sections) ? report.sections : []; const entry = sections .flatMap((section) => section.entries || []) .find((item) => item.id === classId); if (!entry) { return; } const canvas = qs(`.week-chart[data-chart-for="${classId}"]`); if (!canvas) { return; } const chartData = entry.chart_data || {}; const labels = Array.isArray(chartData.labels) ? chartData.labels : []; const missed = Array.isArray(chartData.missed) ? chartData.missed : []; const other = Array.isArray(chartData.other) ? chartData.other : []; const conducted = Array.isArray(chartData.conducted) ? chartData.conducted : []; if (!labels.length) { const container = canvas.parentElement; if (container) { container.innerHTML = "
Нет данных.
"; } return; } const chart = new Chart(canvas, { type: "bar", data: { labels, datasets: [ { label: "Пропущено", data: missed, backgroundColor: "#ef4444", stack: "pairs", borderRadius: 4, }, { label: "Не по расписанию", data: other, backgroundColor: "#f59e0b", stack: "pairs", borderRadius: 4, }, { label: "Проведено", data: conducted, backgroundColor: "#22c55e", stack: "pairs", borderRadius: 4, }, ], }, options: { responsive: true, maintainAspectRatio: false, animation: false, interaction: { mode: "index", intersect: false, }, scales: { x: { stacked: true, grid: { display: false, }, }, y: { stacked: true, beginAtZero: true, ticks: { precision: 0, }, }, }, }, }); chartStore.set(classId, chart); } function setActiveSection(sectionId, report) { qsa(".section-nav .nav-btn[data-section]").forEach((btn) => { btn.classList.toggle("active", btn.dataset.section === sectionId); }); qsa(".class-nav-group").forEach((group) => { group.classList.toggle("active", group.dataset.section === sectionId); }); const firstButton = qs(`.class-nav-group.active .class-btn[data-class-id]`); if (firstButton) { setActiveClass(firstButton.dataset.classId, false, report); } } function setActiveClass(classId, scrollIntoView, report) { qsa(".class-btn[data-class-id]").forEach((btn) => { btn.classList.toggle("active", btn.dataset.classId === classId); }); qsa(".class-block[data-class-id]").forEach((block) => { const active = block.dataset.classId === classId; block.classList.toggle("active", active); if (active && scrollIntoView) { block.scrollIntoView({ behavior: "smooth", block: "start" }); } }); buildChartForClass(classId, report); } function init() { const report = window.__CLASS_REPORT__ || {}; const activeSection = report.active_section || ""; const activeClass = report.active_class || ""; qsa(".section-nav .nav-btn[data-section]").forEach((btn) => { btn.addEventListener("click", () => setActiveSection(btn.dataset.section, report), ); }); qsa(".class-btn[data-class-id]").forEach((btn) => { btn.addEventListener("click", () => setActiveClass(btn.dataset.classId, true, report), ); }); if (activeSection) { setActiveSection(activeSection, report); } if (activeClass) { setActiveClass(activeClass, false, report); } } // ---- Print support: convert canvases to static images ---- function prepareForPrint() { var report = window.__CLASS_REPORT__ || {}; var sections = Array.isArray(report.sections) ? report.sections : []; var allEntries = sections.flatMap(function (s) { return s.entries || []; }); // Activate each class one-by-one, build its chart, capture the image, // then move on. This ensures Chart.js renders each canvas properly. var idx = 0; var storedImages = {}; function processNext() { if (idx >= allEntries.length) { // All done — replace canvases with stored images Object.keys(storedImages).forEach(function (classId) { var canvas = qs('.week-chart[data-chart-for="' + classId + '"]'); if (!canvas) return; var img = document.createElement("img"); img.src = storedImages[classId]; img.alt = "Chart"; img.style.width = "100%"; img.style.height = "320px"; img.style.objectFit = "contain"; canvas.parentNode.replaceChild(img, canvas); }); // Restore original class if (originalClassId) { setActiveClass(originalClassId, false, report); } // Short delay then open print dialog setTimeout(function () { window.print(); }, 50); return; } var entry = allEntries[idx]; var cid = entry.id; if (!cid) { idx++; processNext(); return; } // Switch to this class to make its canvas visible setActiveClass(cid, false, report); // Wait for layout + Chart.js to render setTimeout(function () { // Build chart if not already built buildChartForClass(cid, report); // Wait again for chart animation to finish setTimeout(function () { var chart = chartStore.get(cid); if (chart) { storedImages[cid] = chart.toBase64Image(); } idx++; processNext(); }, 100); }, 50); } // Remember which class was active var originalClassId = null; var activeBlock = qs(".class-block.active"); if (activeBlock) originalClassId = activeBlock.dataset.classId; // Start sequential processing from first entry idx = 0; processNext(); } window.printReport = function printReport() { prepareForPrint(); // print dialog opens inside prepareForPrint after all charts are processed }; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", init); } else { init(); } })();