import{d as v}from"./datatables-I6jthWPb.js";import{r as m,g as f}from"./api-DZ3cXm1m.js";import{i as E}from"./tooltip-manager-D9RaGGY4.js";import"./bootstrap.esm-Bb7dPm-H.js";import"./datatables-Bo82YvhT.js";/* empty css                                  */document.addEventListener("DOMContentLoaded",function(){var b,y,h,_;const g=document.getElementById("resellersTable");if(!g)return;const w=g.getAttribute("data-records-url"),o=window.resellersConfig,j={pending:"bg-warning",needs_info:"bg-info",rejected:"bg-danger",active:"bg-success",suspended:"bg-danger",terminated:"bg-secondary"},l=$("#resellersTable").DataTable({...v.getDefaultConfig(),serverSide:!0,searching:!1,dom:'<"d-flex justify-content-between align-items-center mb-3"<"d-flex align-items-center"l><"d-flex align-items-center"f>>t<"d-flex justify-content-between align-items-center mt-3"<"d-flex align-items-center"i><"d-flex align-items-center"p>>',ajax:{url:w,type:"GET",data:function(t){var n;t.status=((n=document.getElementById("filter-status"))==null?void 0:n.value)||""},beforeSend:function(t){t.setRequestHeader("Authorization",`Bearer ${f()}`)},error:async function(t){t.status===401&&await m()&&l.ajax.reload()}},columns:[{data:"id",name:"id"},{data:"client",name:"client"},{data:"brand_name",name:"brand_name"},{data:"status",name:"status",render:function(t){const n=j[t]||"bg-secondary",e=o.translations["status_"+t]||t;return`<span class="badge ${n}">${e}</span>`}},{data:"clients_count",name:"clients_count"},{data:"commission",name:"commission"},{data:"created_at",name:"created_at"},{data:"id",name:"actions",orderable:!1,searchable:!1,render:function(t,n,e){var s;const i=((s=window.pageConfig)==null?void 0:s.permissions)||{},a=[];if(i.update&&e.is_application){const r=A=>String(A??"").replace(/&/g,"&amp;").replace(/"/g,"&quot;").replace(/</g,"&lt;"),u=`data-id="${t}" data-brand="${r(e.brand_name)}" data-client="${r(e.client)}" data-message="${r(e.application_message)}" data-notes="${r(e.notes)}"`;(e.status==="pending"||e.status==="needs_info")&&a.push(`
                                <button type="button" class="btn btn-sm btn-outline-success d-inline-flex align-items-center justify-content-center application-approve"
                                        style="width: 32px; height: 32px;" ${u}
                                        data-bs-toggle="tooltip" title="${o.translations.approve_tooltip}">
                                    <i class="fas fa-check"></i>
                                </button>`),e.status==="pending"&&a.push(`
                                <button type="button" class="btn btn-sm btn-outline-info d-inline-flex align-items-center justify-content-center ms-1 application-request-info"
                                        style="width: 32px; height: 32px;" ${u}
                                        data-bs-toggle="tooltip" title="${o.translations.request_info_tooltip}">
                                    <i class="fas fa-question"></i>
                                </button>`),e.status!=="rejected"&&a.push(`
                                <button type="button" class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 application-reject"
                                        style="width: 32px; height: 32px;" ${u}
                                        data-bs-toggle="tooltip" title="${o.translations.reject_tooltip}">
                                    <i class="fas fa-times"></i>
                                </button>`)}if(i.update){const r=o.routes.edit.replace(":id",t);a.push(`
                            <a href="${r}"
                               class="btn btn-sm btn-outline-primary d-inline-flex align-items-center justify-content-center ms-1"
                               style="width: 32px; height: 32px;"
                               data-bs-toggle="tooltip" title="Edit">
                                <i class="fas fa-edit"></i>
                            </a>`)}if(i.delete){const r=o.routes.destroy.replace(":id",t);a.push(`
                            <button type="button"
                                    class="btn btn-sm btn-outline-danger d-inline-flex align-items-center justify-content-center ms-1 delete-reseller"
                                    style="width: 32px; height: 32px;"
                                    data-id="${t}" data-url="${r}"
                                    data-bs-toggle="tooltip" title="Delete">
                                <i class="fas fa-trash"></i>
                            </button>`)}return a.length?`<div class="d-inline-flex">${a.join("")}</div>`:'<span class="text-muted">-</span>'}}],drawCallback:function(){E()}});(b=document.getElementById("filter-status"))==null||b.addEventListener("change",()=>l.ajax.reload()),document.body.addEventListener("click",async function(t){const n=t.target.closest(".delete-reseller");if(!n||!confirm(o.translations.confirm_delete))return;const e=n.getAttribute("data-url");try{const i=await fetch(e,{method:"DELETE",headers:{"Content-Type":"application/json",Authorization:`Bearer ${f()}`}});if(i.status===401){await m()&&n.click();return}i.ok&&l.ajax.reload(null,!1)}catch(i){console.error("Delete error:",i)}});let c=null;function x(t){const n=t.getAttribute("data-brand")||"-",e=t.getAttribute("data-client")||"-",i=t.getAttribute("data-message")||"",a=t.getAttribute("data-notes")||"";let s=`<div class="border rounded p-2 small">
            <div><strong>${n}</strong> — ${e}</div>`;return a&&(s+=`<div class="text-muted mt-1">${a}</div>`),i&&(s+=`<div class="mt-1">${o.translations.application_message}: ${i}</div>`),s+="</div>",s}function d(t,n,e){c=t.getAttribute("data-id"),document.getElementById(e).innerHTML=x(t),bootstrap.Modal.getOrCreateInstance(document.getElementById(n)).show()}document.body.addEventListener("click",function(t){const n=t.target.closest(".application-approve");if(n)return d(n,"approveApplicationModal","approve-application-summary");const e=t.target.closest(".application-reject");if(e)return d(e,"rejectApplicationModal","reject-application-summary");const i=t.target.closest(".application-request-info");if(i)return d(i,"requestInfoModal","request-info-application-summary")});async function p(t,n,e,i){if(c){i.disabled=!0;try{let a=await fetch(t.replace(":id",c),{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${f()}`},body:JSON.stringify(n)});if(a.status===401){const r=await m();r&&(a=await fetch(t.replace(":id",c),{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${r}`},body:JSON.stringify(n)}))}const s=await a.json();window.showToast&&window.showToast(s.success?"success":"error",s.message||""),s.success&&(bootstrap.Modal.getOrCreateInstance(document.getElementById(e)).hide(),l.ajax.reload(null,!1))}catch(a){console.error("Application action error:",a),window.showToast&&window.showToast("error",a.message)}finally{i.disabled=!1}}}(y=document.getElementById("approveApplicationSubmit"))==null||y.addEventListener("click",function(){const t=document.getElementById("approveApplicationForm");if(!t.reportValidity())return;const n=new FormData(t),e={group_id:n.get("group_id"),commission_type:n.get("commission_type"),commission_rate:n.get("commission_rate"),max_clients:n.get("max_clients")||0,allowed_product_groups:n.getAll("allowed_product_groups[]"),allowed_tlds:n.getAll("allowed_tlds[]")};e.allowed_product_groups.length===0&&(e.allowed_product_groups=null),e.allowed_tlds.length===0&&(e.allowed_tlds=null),p(o.routes.approve,e,"approveApplicationModal",this)}),(h=document.getElementById("rejectApplicationSubmit"))==null||h.addEventListener("click",function(){const t=document.getElementById("rejectApplicationForm");t.reportValidity()&&p(o.routes.reject,{admin_note:new FormData(t).get("admin_note")},"rejectApplicationModal",this)}),(_=document.getElementById("requestInfoSubmit"))==null||_.addEventListener("click",function(){const t=document.getElementById("requestInfoForm");t.reportValidity()&&p(o.routes.requestInfo,{admin_note:new FormData(t).get("admin_note")},"requestInfoModal",this)})});
