import{g as c,r as l}from"./api-DZ3cXm1m.js";const o=window.pageConfig||{};$(document).ready(function(){d(),p()});async function u(t,r="GET",n=null){const e={method:r,headers:{"Content-Type":"application/json",Accept:"application/json",Authorization:`Bearer ${c()}`}};n&&(e.body=JSON.stringify(n));let a=await fetch(t,e);if(a.status===401){const i=await l();i&&(e.headers.Authorization=`Bearer ${i}`,a=await fetch(t,e))}return a}function d(){f()}function f(){var t,r;$("#client_id").select2({placeholder:((t=o.translations)==null?void 0:t.selectClient)||"Search and select a client...",allowClear:!0,ajax:{url:(r=o.routes)==null?void 0:r.clientSearch,dataType:"json",delay:250,headers:{Authorization:"Bearer "+c()},data:function(n){return{q:n.term}},processResults:function(n){return{results:n.results.map(function(e){return{id:e.id,text:e.text,currency_id:e.currency_id}})}}},minimumInputLength:2}),$("#client_id").on("select2:select",function(n){const e=n.params.data;e.currency_id&&$("#currency_id").val(e.currency_id)})}function p(){$("#createTransactionForm").on("submit",async function(t){t.preventDefault();const r={client_id:$("#client_id").val(),invoice_id:$("#invoice_id").val()||null,type:$("#type").val(),amount:$("#amount").val(),currency_id:$("#currency_id").val(),payment_method:$("#payment_method").val()||null,payment_gateway:$("#payment_gateway").val()||null,payment_transaction_id:$("#payment_transaction_id").val()||null,bank_fee:$("#bank_fee").val()||null,description:$("#description").val()||null,notes:$("#notes").val()||null},n=$(this).find('button[type="submit"]');n.prop("disabled",!0);try{const e=await u(o.routes.store,"POST",r),a=await e.json();if(a.success)s("success",a.message||o.translations.created),setTimeout(()=>{window.location.href=a.redirect},1500);else if(e.status===422&&a.errors){const i=Object.values(a.errors)[0];s("error",Array.isArray(i)?i[0]:i)}else s("error",a.message||o.translations.createFailed)}catch{s("error",o.translations.createFailed)}finally{n.prop("disabled",!1)}})}function s(t,r){t==="success"&&window.showSuccess?window.showSuccess(r):t==="error"&&window.showError?window.showError(r):window.showToast&&window.showToast(t,r)}
