/** * Вкладка «Сравнение» на /reports. */ (function (global) { const COPY = () => global.WespAnalyticsCopy || {}; function escapeHtml(value) { return String(value ?? "") .replace(/&/g, "&") .replace(//g, ">"); } function getDateParams() { const from = document.getElementById("date-from")?.value || ""; const to = document.getElementById("date-to")?.value || ""; return { date_from: from, date_to: to }; } function buildUrl() { const params = global.WespReportsFilters?.buildAnalyticsParams?.() || new URLSearchParams(getDateParams()); return `/api/analytics/plan-fact?${params.toString()}`; } function barWidth(value, max) { if (!max || max <= 0) return 0; return Math.min(100, (Number(value) / max) * 100); } function faultBadge(fault, executionLabel) { const c = COPY().comparison || {}; if (fault === "execution") { return `${escapeHtml( executionLabel || c.noteExecution || "" )}`; } if (fault === "excluded" || fault === "zootech_ok") { return `${escapeHtml(c.noteExcluded || "")}`; } if (fault === "adjusted" || fault === "zootech") { return `${escapeHtml(c.noteAdjusted || "")}`; } return ""; } function legendHtml() { const c = COPY().comparison || {}; return ( `
` + `${escapeHtml(c.legendBase)}` + `${escapeHtml(c.legendPlan)}` + `${escapeHtml(c.legendActual)}` + `
` ); } function renderMetricRows(rows, maxKg, options) { const executionLabel = options?.executionLabel; return rows .map((c) => { const notes = (c.notes || []) .map((n) => { const note = typeof n === "string" ? { text: n, kind: "execution" } : n; const kind = note.kind === "zootech" ? "zootech" : "execution"; const cls = kind === "zootech" ? "analytics-pf-note analytics-pf-note--zootech" : "analytics-pf-note analytics-pf-note--execution"; return `
  • ${escapeHtml(note.text)}
  • `; }) .join(""); const faultBadgeHtml = faultBadge(c.fault, executionLabel); const compCls = c.skippedToday && (c.fault === "excluded" || c.fault === "zootech_ok") ? "analytics-pf-comp analytics-pf-comp--excluded" : options?.rowClass || "analytics-pf-comp"; return ( `
    ` + `
    ` + `
    ${escapeHtml(c.name)}
    ` + faultBadgeHtml + `
    ` + `
    ` + `
    ${Number(c.baseKg).toFixed(1)}
    ` + `
    ${Number(c.planTodayKg).toFixed(1)}
    ` + `
    ${Number(c.actualKg).toFixed(1)}
    ` + `
    ` + (notes ? `` : "") + `
    ` ); }) .join(""); } function sectionHtml(title, body) { if (!body) return ""; return ( `
    ` + `

    ${escapeHtml(title)}

    ` + body + `
    ` ); } function renderItem(item) { const comps = item.components || []; const groups = item.unloadingGroups || []; const mixer = item.mixerRemainder || null; const compMax = Math.max(...comps.flatMap((c) => [c.baseKg, c.planTodayKg, c.actualKg]), 1); const unloadMax = Math.max(...groups.flatMap((c) => [c.baseKg, c.planTodayKg, c.actualKg]), 1); const compHtml = renderMetricRows(comps, compMax, { executionLabel: COPY().comparison?.noteExecution, }); const unloadHtml = renderMetricRows(groups, unloadMax, { executionLabel: COPY().comparison?.noteUnloadingExecution, }); const mixerHtml = mixer ? (() => { const kg = Number(mixer.actualKg); const sign = kg > 0 ? "+" : ""; const cls = kg < 0 ? "analytics-pf-mixer-line analytics-pf-mixer-line--negative" : "analytics-pf-mixer-line"; return ( `

    ${escapeHtml( COPY().comparison?.mixerRemainderLabel || "Остаток в миксере" )}: ${sign}${kg.toFixed(1)} кг

    ` ); })() : ""; const when = item.startTime ? escapeHtml(item.startTime.slice(0, 16).replace("T", " ")) : ""; const unloadingSection = groups.length || mixer ? sectionHtml( COPY().comparison?.sectionUnloading || "Выгрузка", legendHtml() + unloadHtml + mixerHtml ) : ""; return ( `
    ` + `
    ` + `${escapeHtml(item.recipeName)}` + `${when}` + `
    ` + sectionHtml( COPY().comparison?.sectionLoading || "Загрузка", legendHtml() + compHtml ) + unloadingSection + `
    ` ); } function render(data) { const el = document.getElementById("analyticsPlanFactList"); if (!el) return; const items = data.items || []; if (!items.length) { el.innerHTML = `

    ${escapeHtml(COPY().comparison?.empty || "")}

    `; return; } el.innerHTML = `
    ${items.map(renderItem).join("")}
    `; el.querySelectorAll("[data-loading-report-id]").forEach((card) => { card.addEventListener("click", () => { const id = card.getAttribute("data-loading-report-id"); if (id) global.WespFeedAlerts?.openReportFromAlert?.(id); }); }); global.WespZootechModals?.revealContent(el.querySelector(".analytics-pf-list")); } async function load() { const el = document.getElementById("analyticsPlanFactList"); if (!el) return; el.innerHTML = '
    ' + '

    Загрузка сравнения…

    '; try { const resp = await fetch(buildUrl()); if (!resp.ok) throw new Error("Не удалось загрузить сравнение"); render(await resp.json()); } catch (err) { el.innerHTML = `

    ${escapeHtml(err.message)}

    `; } } function onFiltersApplied() { if (global.WespFeedAlerts?.getCurrentView?.() === "comparison") load(); } global.WespAnalyticsPlanFact = { load, onFiltersApplied }; })(typeof window !== "undefined" ? window : globalThis);