import{M as j}from"./bootstrap.esm-Bb7dPm-H.js";import{d as x}from"./datatables-I6jthWPb.js";import{s as c}from"./toastr-helper-Wdj0mZjX.js";import{i as w}from"./tooltip-manager-D9RaGGY4.js";import{r as A,g}from"./api-DZ3cXm1m.js";import{c as E,g as B}from"./datatable-filters-CecjJm_7.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */import"./vendor-zLyX4B24.js";import"./tr-CHCA6xOE.js";function b(m){if(!m)return"";const d={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#039;"};return String(m).replace(/[&<>"']/g,l=>d[l])}document.addEventListener("DOMContentLoaded",function(){var h;if(!document.getElementById("affiliates-table"))return;const d=window.affiliateConfig||{},l=d.routes||{},t=d.translations||{},u=window.permissions||{},p=E(null,{filters:[{type:"select",id:"statusFilter",width:"150px",options:[{value:"",label:t.filter_all||"All Statuses"},{value:"pending",label:t.status_pending||"Pending"},{value:"active",label:t.status_active||"Active"},{value:"rejected",label:t.status_rejected||"Rejected"},{value:"suspended",label:t.status_suspended||"Suspended"}]}],search:!0,searchPlaceholder:t.search_placeholder||"Search..."}),f=$("#affiliates-table").DataTable({...x.getDefaultConfig(),serverSide:!0,searching:!1,dom:B(),ajax:{url:l.records,type:"GET",headers:{Authorization:"Bearer "+g()},data:function(e){const a=p.getValues();e.search=a.search||"",e.status=a.statusFilter||""},error:function(e){e.status===401&&A().then(a=>{a&&f.ajax.reload()})}},columns:[{data:"id",name:"id"},{data:"client_name",name:"client_name",orderable:!1,render:function(e,a,n){return`<div>
                        <span class="fw-semibold">${b(e)}</span>
                        <br><small class="text-muted">${b(n.client_email||"")}</small>
                    </div>`}},{data:"referral_code",name:"referral_code",render:function(e){return`<code>${b(e)}</code>`}},{data:"status",name:"status",className:"text-center",render:function(e){const n={pending:{class:"bg-warning",text:t.status_pending||"Pending"},active:{class:"bg-success",text:t.status_active||"Active"},rejected:{class:"bg-danger",text:t.status_rejected||"Rejected"},suspended:{class:"bg-secondary",text:t.status_suspended||"Suspended"}}[e]||{class:"bg-secondary",text:e};return`<span class="badge ${n.class}">${n.text}</span>`}},{data:"total_earned",name:"total_earned",className:"text-end"},{data:"pending_balance",name:"pending_balance",className:"text-end"},{data:"total_conversions",name:"total_conversions",className:"text-center"},{data:"created_at",name:"created_at",render:function(e){return e?new Date(e).toLocaleDateString(document.documentElement.lang||"tr"):"-"}},{data:"id",name:"actions",orderable:!1,searchable:!1,className:"text-end pe-4",render:function(e,a,n){var s,r,o,y,v;const i=[];if((s=u.affiliates)!=null&&s.view){const _=(r=l.show)==null?void 0:r.replace(":id",e);i.push(`
                            <a href="${_}"
                               class="btn btn-sm btn-outline-primary d-inline-flex align-items-center justify-content-center"
                               style="width: 32px; height: 32px;"
                               data-bs-toggle="tooltip"
                               title="${t.button_view||"View"}">
                                <i class="fas fa-eye"></i>
                            </a>`)}return(o=u.affiliates)!=null&&o.manage&&n.status==="pending"&&(i.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-success d-inline-flex align-items-center justify-content-center ms-1 action-btn"
                                    style="width: 32px; height: 32px;"
                                    data-action="approve"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${t.button_approve||"Approve"}">
                                <i class="fas fa-check"></i>
                            </button>`),i.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 reject-btn"
                                    style="width: 32px; height: 32px;"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${t.button_reject||"Reject"}">
                                <i class="fas fa-times"></i>
                            </button>`)),(y=u.affiliates)!=null&&y.manage&&n.status==="active"&&i.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-warning d-inline-flex align-items-center justify-content-center ms-1 action-btn"
                                    style="width: 32px; height: 32px;"
                                    data-action="suspend"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${t.button_suspend||"Suspend"}">
                                <i class="fas fa-ban"></i>
                            </button>`),(v=u.affiliates)!=null&&v.manage&&(n.status==="suspended"||n.status==="rejected")&&i.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-success d-inline-flex align-items-center justify-content-center ms-1 action-btn"
                                    style="width: 32px; height: 32px;"
                                    data-action="activate"
                                    data-id="${e}"
                                    data-bs-toggle="tooltip"
                                    title="${t.button_activate||"Activate"}">
                                <i class="fas fa-play"></i>
                            </button>`),`<div class="d-inline-flex">${i.join("")}</div>`}}],drawCallback:function(){w()},initComplete:function(){p.init()}});p.setDataTable(f),$(document).on("click",".action-btn",async function(){var r;const e=$(this).data("action"),a=$(this).data("id"),n={approve:t.messages_confirm_approve||"Are you sure you want to approve this affiliate?",suspend:t.messages_confirm_suspend||"Are you sure you want to suspend this affiliate?",activate:t.messages_confirm_activate||"Are you sure you want to reactivate this affiliate?"};if(!await window.showConfirm(n[e],{confirmClass:e==="suspend"?"btn-warning":"btn-success",icon:e==="suspend"?"warning":"question"}))return;const s=(r=l[e])==null?void 0:r.replace(":id",a);s&&fetch(s,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer "+g()}}).then(o=>o.json()).then(o=>{o.success?(c("success",o.message),f.ajax.reload()):c("error",o.message||"Operation failed.")}).catch(o=>{console.error("Error:",o),c("error","An error occurred.")})}),$(document).on("click",".reject-btn",function(){const e=$(this).data("id");document.getElementById("rejectAffiliateId").value=e,document.getElementById("rejectReason").value="",new j(document.getElementById("rejectModal")).show()}),(h=document.getElementById("confirmReject"))==null||h.addEventListener("click",function(){var i;const e=document.getElementById("rejectAffiliateId").value,a=document.getElementById("rejectReason").value;if(!a.trim()){c("error","Please provide a rejection reason.");return}const n=(i=l.reject)==null?void 0:i.replace(":id",e);n&&fetch(n,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"Bearer "+g()},body:JSON.stringify({reason:a})}).then(s=>s.json()).then(s=>{var r;s.success?(c("success",s.message),f.ajax.reload(),(r=j.getInstance(document.getElementById("rejectModal")))==null||r.hide()):c("error",s.message||"Operation failed.")}).catch(s=>{console.error("Error:",s),c("error","An error occurred.")})})});
