(function(){var v=window.checkoutInstallmentConfig||{},h=v.routes||{},u=v.translations||{},d=document.getElementById("installment-selector-card"),c=document.getElementById("installment-selector-container"),s=document.getElementById("installment_count"),m=document.getElementById("installment_payment_id");if(!d||!c||!s)return;function g(t){m&&(m.value=t&&t.payment_id!=null?t.payment_id:"")}var l=document.getElementById("summary-grand-total"),i=document.getElementById("summary-installment-surcharge-row"),p=document.getElementById("summary-installment-surcharge-amount"),w=l?l.textContent:"",f=[];function C(){var t=document.querySelector('input[name="payment_gateway_id"]:checked');return t?t.value:null}function o(t){var e=parseFloat(t)||0,n=e.toFixed(2),a=window.currency||{};return a.prefix?a.prefix+n:a.suffix?n+" "+a.suffix:a.code?a.code+" "+n:n}function T(t){var e=parseInt(t.count,10);if(e===1)return(u.single_payment||"Single Payment")+" — "+o(t.total);var n=e+" "+(u.installments||"Installments")+" — "+(u.monthly||"Monthly")+" "+o(t.monthly)+" ("+(u.total||"Total")+" "+o(t.total)+")";return parseFloat(t.surcharge)>0&&(n+=" (+"+o(t.surcharge)+")"),n}function I(t){if(t){var e=parseFloat(t.surcharge)||0,n=parseFloat(t.total)||0;e>0?(i&&(i.style.display=""),p&&(p.textContent=o(e))):i&&(i.style.display="none"),l&&(l.textContent=o(n))}}function S(){i&&(i.style.display="none"),l&&(l.textContent=w)}function b(t){if(!t||t.length===0){y();return}f=t;var e="";t.forEach(function(n,a){var r="installment_plan_"+a,k=a===0?" checked":"",x=T(n);e+='<div class="form-check mb-2"><input class="form-check-input" type="radio" name="installment_plan" id="'+r+'" value="'+a+'"'+k+'><label class="form-check-label" for="'+r+'">'+x+"</label></div>"}),c.innerHTML=e,s.value=t[0].count,g(t[0]),I(t[0]),c.querySelectorAll('input[name="installment_plan"]').forEach(function(n){n.addEventListener("change",function(){var a=parseInt(this.value,10),r=f[a]||null;r&&(s.value=r.count,g(r),I(r))})}),d.style.display=""}function y(){d.style.display="none",c.innerHTML="",s.value=1,m&&(m.value=""),f=[],S()}function E(t){if(!t||!h.plans){y();return}fetch(h.plans,{method:"POST",headers:{"Content-Type":"application/json",Accept:"application/json","X-CSRF-TOKEN":window.csrfToken||""},body:JSON.stringify({gateway_id:t})}).then(function(e){if(!e.ok)throw new Error("HTTP "+e.status);return e.json()}).then(function(e){b(e.plans||[])}).catch(function(){y()})}document.querySelectorAll('input[name="payment_gateway_id"]').forEach(function(t){t.addEventListener("change",function(){E(this.value)})});function _(){var t=C();t&&E(t)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",_):_()})();
