import{g as _,r as y}from"./api-DZ3cXm1m.js";import{s as m}from"./toastr-helper-Wdj0mZjX.js";document.addEventListener("DOMContentLoaded",function(){var u;const i=window.reportsConfig||{},c=i.routes||{},l=i.translations||{},r=document.getElementById("startDate"),a=document.getElementById("endDate");if(r&&a){const t=new Date,e=new Date(t);e.setDate(e.getDate()-30),r.value=e.toISOString().split("T")[0],a.value=t.toISOString().split("T")[0]}(u=document.getElementById("loadReport"))==null||u.addEventListener("click",function(){s()}),s();function s(){const t=(r==null?void 0:r.value)||"",e=(a==null?void 0:a.value)||"";if(!t||!e){m("error","Please select a date range.");return}const n=`${c.reportData}?start_date=${t}&end_date=${e}`;fetch(n,{method:"GET",headers:{"Content-Type":"application/json",Authorization:"Bearer "+_(),Accept:"application/json"}}).then(o=>o.status===401?y().then(d=>{d&&s()}):o.json()).then(o=>{o&&(p(o.summary),f(o.top_affiliates),g(o.product_distribution))}).catch(o=>{console.error("Error loading report:",o),m("error","Failed to load report data.")})}function p(t){t&&(document.getElementById("statTotalAffiliates").textContent=t.total_affiliates??"-",document.getElementById("statActiveAffiliates").textContent=t.active_affiliates??"-",document.getElementById("statTotalCommissions").textContent=t.total_commissions??"-",document.getElementById("statTotalPayouts").textContent=t.total_payouts??"-",document.getElementById("statPendingPayouts").textContent=t.pending_payouts??"-",document.getElementById("statTotalConversions").textContent=t.total_conversions??"-")}function f(t){const e=document.getElementById("topAffiliatesBody");if(e){if(!t||t.length===0){e.innerHTML=`<tr><td colspan="5" class="text-center text-muted py-4">${l.report_no_data||"No data available."}</td></tr>`;return}e.innerHTML=t.map(n=>{var d;return`<tr>
                <td><a href="${(d=c.showAffiliate)==null?void 0:d.replace(":id",n.id)}" class="text-decoration-none">${n.client_name}</a></td>
                <td><code>${n.referral_code}</code></td>
                <td class="text-end fw-semibold">${n.period_earned}</td>
                <td class="text-center">${n.period_conversions}</td>
                <td class="text-end">${n.total_earned}</td>
            </tr>`}).join("")}}function g(t){const e=document.getElementById("productDistributionBody");if(e){if(!t||t.length===0){e.innerHTML=`<tr><td colspan="3" class="text-center text-muted py-4">${l.report_no_data||"No data available."}</td></tr>`;return}e.innerHTML=t.map(n=>`<tr>
                <td>${n.product_name}</td>
                <td class="text-center">${n.commission_count}</td>
                <td class="text-end fw-semibold">${n.total_amount}</td>
            </tr>`).join("")}}});
