import{C as f}from"./auto-CFNrZGOd.js";import{g}from"./api-DZ3cXm1m.js";let d=null,i=null;document.addEventListener("DOMContentLoaded",function(){h()});function h(){const t=document.getElementById("reportFiltersForm");t&&t.addEventListener("submit",function(o){o.preventDefault(),w()});const e=document.getElementById("exportCsvBtn");e&&e.addEventListener("click",v);const n=document.getElementById("printReportBtn");n&&n.addEventListener("click",x)}async function w(){var t,e;try{I();const n=new FormData(document.getElementById("reportFiltersForm")),o=Object.fromEntries(n.entries()),r=new URLSearchParams(window.location.search).get("module");if(!r)throw new Error("Module parameter not found");const s=await fetch(`/admin/reports/api/${r}`,{method:"POST",headers:{"Content-Type":"application/json",Authorization:`Bearer ${g()}`},body:JSON.stringify(o)});if(!s.ok)throw new Error(`HTTP error! status: ${s.status}`);const c=await s.json();if(c.success)i=c,E(c);else throw new Error(c.message||"Report could not be generated")}catch(n){console.error("Report generation error:",n),k((((e=(t=window.translations)==null?void 0:t.reports)==null?void 0:e.generation_error)||"Error generating report: ")+n.message)}finally{j()}}function E(t){b(t.summary,t.meta),t.chart&&B(t.chart),y(t.data,t.columns),document.getElementById("results").style.display="block"}function b(t,e){document.getElementById("totalRecords").textContent=t.total_records,document.getElementById("generatedAt").textContent=new Date(t.generated_at).toLocaleString(document.documentElement.lang||"en"),document.getElementById("categoryInfo").textContent=e.category,document.getElementById("filtersInfo").textContent=_(t.filters_applied)}function B(t){var n,o;document.getElementById("chartSection").style.display="block";const e=document.getElementById("chartCanvas").getContext("2d");d&&d.destroy(),d=new f(e,{type:t.type,data:t.data,options:{responsive:!0,maintainAspectRatio:!1,plugins:{legend:{position:"top"},title:{display:!0,text:((o=(n=window.translations)==null?void 0:n.reports)==null?void 0:o.chart_title)||"Report Chart"}}}})}function y(t,e){const n=document.getElementById("tableHeader");n.innerHTML="",Object.values(e).forEach(a=>{const r=document.createElement("th");r.textContent=a.title,a.sortable&&(r.style.cursor="pointer",r.onclick=()=>C(a.title)),n.appendChild(r)});const o=document.getElementById("tableBody");o.innerHTML="",t.forEach(a=>{const r=document.createElement("tr");Object.keys(e).forEach(s=>{const c=document.createElement("td");c.textContent=a[s]||"-",r.appendChild(c)}),o.appendChild(r)})}function C(t){if(!i)return;const e=Object.keys(i.columns).find(o=>i.columns[o].title===t);if(!e)return;const n=[...i.data].sort((o,a)=>{const r=o[e],s=a[e];return i.columns[e].type==="number"?r-s:String(r).localeCompare(String(s))});y(n,i.columns)}function I(){document.getElementById("loading").style.display="block",document.getElementById("results").style.display="none"}function j(){document.getElementById("loading").style.display="none"}function k(t){window.showError(t)}function _(t){var n,o,a,r,s,c,l,m,p,u;if(!t||Object.keys(t).length===0)return((o=(n=window.translations)==null?void 0:n.reports)==null?void 0:o.no_filters)||"No filters";const e=[];return t.start_date&&e.push(`${((r=(a=window.translations)==null?void 0:a.reports)==null?void 0:r.start_date)||"Start"}: ${t.start_date}`),t.end_date&&e.push(`${((c=(s=window.translations)==null?void 0:s.reports)==null?void 0:c.end_date)||"End"}: ${t.end_date}`),t.period&&e.push(`${((m=(l=window.translations)==null?void 0:l.reports)==null?void 0:m.period)||"Period"}: ${t.period}`),t.cohort_size&&e.push(`${((u=(p=window.translations)==null?void 0:p.reports)==null?void 0:u.cohort)||"Cohort"}: ${t.cohort_size}`),e.join(", ")}function v(){if(!i)return;const e=[Object.values(i.columns).map(a=>a.title).join(","),...i.data.map(a=>Object.keys(i.columns).map(r=>a[r]||"").join(","))].join(`
`),n=new Blob([e],{type:"text/csv;charset=utf-8;"}),o=document.createElement("a");o.href=URL.createObjectURL(n),o.download=`report-${new Date().toISOString().split("T")[0]}.csv`,o.click()}function x(){window.print()}
