document.addEventListener("DOMContentLoaded",(function(){$("#client_payment_type").select2({placeholder:"Select Payment Type"}),$("#client_payment_mode").select2({placeholder:"Select Payment Method"}),$(".amount").hide();var e=window.location.toString();if(e.indexOf("?")>0){var t=e.substring(0,e.indexOf("?"));window.history.replaceState({},document.title,t)}$.ajaxSetup({headers:{"X-CSRF-TOKEN":$('meta[name="csrf-token"]').attr("content")}})})),listenChange("#client_payment_mode",(function(){1==$(this).val()?$(".payment-attachment").removeClass("d-none"):$(".payment-attachment").addClass("d-none")})),listenChange("#client_payment_mode",(function(){1==$(this).val()?$("#transaction").show():$("#transaction").hide()})),listenChange("#client_payment_type",(function(){var e=$(this).val(),t=$("#payable_amount").val();"2"==e?($(".amount").hide(),$("#amount").val(t),$("#amount").prop("readonly",!0)):"3"==e?($(".amount").show(),$("#amount").val(""),$("#amount").prop("readonly",!1)):($(".amount").hide(),$("#amount").prop("readonly",!1))})),listenKeyup("#amount",(function(){var e=parseFloat($("#payable_amount").val()),t=parseFloat($("#amount").val()),a=parseInt($("#client_payment_type").val());3===a&&e<t||2===a&&e<t?($("#error-msg").text(Lang.get("js.amount_should_be_less_than_payable_amount")),$("#btnPay").addClass("disabled")):($("#error-msg").text(""),$("#btnPay").removeClass("disabled"))})),listenChange("#client_payment_mode",(function(){paymentMode=$(this).val(),parseInt(paymentMode)})),listenSubmit("#clientPaymentForm",(function(e){var t=this;if(e.preventDefault(),0==$("#amount").val())return displayErrorMessage("Amount should not be equal to zero"),!1;if(0==$("#payment_note").val().trim().length)return displayErrorMessage("Note field is Required"),!1;var a=$(this).find("#btnPay");setAdminBtnLoader(a);var n={amount:parseFloat($("#amount").val()),invoiceId:parseInt($("#client_invoice_id").val()),transactionNotes:$("#payment_note").val()};1==paymentMode?$.ajax({url:route("clients.payments.store"),type:"POST",data:new FormData(this),processData:!1,contentType:!1,success:function(e){e.success&&(Livewire.dispatch("refreshDatatable"),Livewire.dispatch("resetPageTable"),window.location.href=e.data.redirectUrl)},error:function(e){displayErrorMessage(e.responseJSON.message)},complete:function(){setAdminBtnLoader(a)}}):2==paymentMode?$.post(invoiceStripePaymentUrl,n).done((function(e){var t=e.data.sessionId;stripe.redirectToCheckout({sessionId:t}).then((function(e){$(this).html("Make Payment").removeClass("disabled"),manageAjaxErrors(e)}))})).catch((function(e){$(t).html("Make Payment").removeClass("disabled"),manageAjaxErrors(e)})):3==paymentMode?$.ajax({type:"GET",url:route("paypal.init"),data:{amount:n.amount,invoiceId:n.invoiceId,transactionNotes:n.transactionNotes},success:function(e){if("CREATED"==e.status){var t="";$.each(e.links,(function(e,a){"approve"==a.rel&&(t=a.href)})),location.href=t}else location.href=e.url},error:function(e){displayErrorMessage(e.responseJSON.message)},complete:function(){setAdminBtnLoader(a)}}):5==paymentMode?$.ajax({type:"GET",url:route("razorpay.init"),data:$(this).serialize(),success:function(e){if(e.success){$("#clientPaymentModal").modal("hide");var t=e.data,a=t.id,n=t.amount,o=t.name,i=t.email,s=t.invoiceId,r=(t.invoice_id,t.description);options.description=r,options.order_id=a,options.amount=n,options.prefill.name=o,options.prefill.email=i,options.prefill.invoiceId=s;var l=new Razorpay(options);l.open(),l.on("payment.failed")}},error:function(e){displayErrorMessage(e.responseJSON.message)},complete:function(){setAdminBtnLoader(a)}}):6==paymentMode&&window.location.replace(route("client.paystack.init",{invoiceId:n.invoiceId,amount:n.amount,note:n.transactionNotes}))}));