document.addEventListener("DOMContentLoaded",function(){const t=document.getElementById("process-bills-form");t&&t.addEventListener("submit",async function(s){s.preventDefault();const o=t.querySelector('button[type="submit"]'),i=o.innerHTML;o.disabled=!0,o.innerHTML='<i class="fas fa-spinner fa-spin"></i> Processing...';try{const r=new FormData(t),n=await fetch(t.action,{method:"POST",headers:{"X-CSRF-TOKEN":document.querySelector('meta[name="csrf-token"]').getAttribute("content"),Accept:"application/json","X-Requested-With":"XMLHttpRequest"},body:r}),e=await n.json();if(n.ok)Swal.fire({title:e.alert.title,html:`
                            <div class="text-left">
                                <p class="mb-2">${e.alert.message}</p>
                                <div class="mt-4 p-3 bg-gray-50 rounded-md">
                                    <p class="text-sm text-gray-600">
                                        <span class="font-medium">Processed:</span> ${e.alert.details.total_processed} bills<br>
                                        <span class="font-medium">Failed:</span> ${e.alert.details.total_failed} bills<br>
                                        <span class="font-medium">Billing Period:</span> ${e.alert.details.billing_period}
                                    </p>
                                </div>
                            </div>
                        `,icon:e.alert.type,confirmButtonText:"OK",confirmButtonColor:"#2563eb",customClass:{confirmButton:"px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"}}).then(a=>{a.isConfirmed&&window.location.reload()});else throw new Error(e.message||"An error occurred while processing the bills")}catch(r){console.error("Error:",r),Swal.fire({title:"Error!",text:r.message||"An error occurred while processing your request",icon:"error",confirmButtonText:"OK",confirmButtonColor:"#dc2626"})}finally{o.disabled=!1,o.innerHTML=i}})});
