import"./bootstrap.esm-Bb7dPm-H.js";import{b as f,c as u}from"./toastr-helper-Wdj0mZjX.js";import{g as y,r as h}from"./api-DZ3cXm1m.js";document.addEventListener("DOMContentLoaded",function(){g(),v(),E()});function g(){w()}function w(){var i,n,r,s,l,d;$("#client_id").select2({placeholder:((n=(i=window.translations)==null?void 0:i.clientBalances)==null?void 0:n.placeholders_select_client)||"Select Client",allowClear:!0,ajax:{url:(s=(r=window.routes)==null?void 0:r.clientBalances)==null?void 0:s.clientSearch,dataType:"json",delay:250,headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")},data:function(c){return{q:c.term}},processResults:function(c){return{results:c.results.map(function(t){var o;return{id:t.id,text:t.text,name:t.name||t.text.split(" (")[0],email:t.email||((o=t.text.match(/\((.*?)\)/))==null?void 0:o[1]),currency_id:t.currency_id}})}}},templateResult:_,templateSelection:B,minimumInputLength:2});const a=new URLSearchParams(window.location.search).get("client_id");if(a){const c=(d=(l=window.routes)==null?void 0:l.clientBalances)==null?void 0:d.clientSearch;c&&$.ajax({url:c,data:{q:a,id:a},headers:{Authorization:"Bearer "+document.querySelector('meta[name="api-token"]').getAttribute("content")},dataType:"json",success:function(t){if(t.results&&t.results.length>0){const o=t.results.find(p=>p.id==a)||t.results[0],m=new Option(o.text,o.id,!0,!0);$("#client_id").append(m).trigger("change"),o.currency_id&&(document.getElementById("currency_id").value=o.currency_id)}}})}$("#client_id").on("select2:select",function(c){const t=c.params.data;t.currency_id&&(document.getElementById("currency_id").value=t.currency_id)}).on("select2:clear",function(){document.getElementById("currency_id").value=""})}function _(e){return e.loading?e.text:$('<div class="select2-client-result"><div class="select2-client-name">'+(e.name||e.text)+'</div><div class="select2-client-email text-muted small">'+(e.email||"")+"</div></div>")}function B(e){return e.text||e.name}function v(){const e=document.getElementById("addBalanceForm");e&&e.addEventListener("submit",function(a){a.preventDefault(),I()})}function E(){const e=document.getElementById("balance_type_id"),a=document.getElementById("amount");e&&a&&e.addEventListener("change",function(){var r,s,l,d,c,t;this.options[this.selectedIndex].getAttribute("data-positive")==="1"?a.placeholder=((l=(s=(r=window.translations)==null?void 0:r.clientBalances)==null?void 0:s.placeholders)==null?void 0:l.positive_amount)||"Enter positive amount (e.g. 100.00)":a.placeholder=((t=(c=(d=window.translations)==null?void 0:d.clientBalances)==null?void 0:c.placeholders)==null?void 0:t.negative_amount)||"Enter negative amount (e.g. 50.00)"})}async function I(){try{const e=new FormData;e.append("client_id",document.getElementById("client_id").value),e.append("currency_id",document.getElementById("currency_id").value),e.append("balance_type_id",document.getElementById("balance_type_id").value),e.append("amount",document.getElementById("amount").value),e.append("description",document.getElementById("description").value),e.append("reference_type",document.getElementById("reference_type").value),e.append("reference_id",document.getElementById("reference_id").value);const a=n=>({method:"POST",headers:{Accept:"application/json","X-Requested-With":"XMLHttpRequest",Authorization:`Bearer ${n}`},body:e});let i=await fetch(window.routes.clientBalances.addBalance,a(y()));if(i.status===401){const n=await h();n&&(i=await fetch(window.routes.clientBalances.addBalance,a(n)))}if(i.ok){const n=await i.json();f(n.message||"Balance added successfully!"),setTimeout(()=>{window.location.href=window.routes.clientBalances.index},1500)}else{const n=await i.json();if(n.errors){const r=Object.values(n.errors).flat().join(`
`);u(r)}else u(n.message||"Error adding balance.")}}catch(e){console.error("Error:",e),u("Beklenmeyen bir hata oluştu. Lütfen tekrar deneyin.")}}
