jQuery(document).ready((function(t){"use strict";window.jsPDF=window.jspdf.jsPDF;let e=t("#wpbody");e&&e.append('<div class="mf-pdf-wrapper"><div class="mfContentToMakePdf"></div></div>');let a={export_btn:t(".metform-pdf-export-btn")};t.extend(a,{init:function(){this.getEntryData()},getEntryData:function(){let e=this;this.export_btn.on("click",(a=>{a.preventDefault(),a.stopPropagation(),a.target.dataset.nonce?(a.target.classList.add("loading"),jQuery.ajax({url:ajaxurl,type:"POST",data:{action:"metform_pdf_get_entry",entry_id:a.target.dataset.id,_wpnonce:a.target.dataset.nonce},success:function(o){let n=t(".mfContentToMakePdf");n&&n.html(o),e.makePdf(a,n[0])},error:function(t,e,o){console.error("Error: "+o),a.target.classList.remove("loading")}})):alert("Please update Metform free plugin too!")}))},makePdf:function(e,a){a.style.width="1000px",a.style.padding="30px",a.style.backgroundColor="#fff",a.style.boxSizing="border-box";const o=a.querySelector("table");o&&(o.style.width="100%",o.style.borderCollapse="collapse");a.querySelectorAll("img").forEach((function(t){const e=t.getAttribute("src");e&&e.startsWith("image/")&&t.setAttribute("src","data:"+e)})),html2canvas(a,{scale:2,useCORS:!0,logging:!1,width:1e3,windowWidth:1e3,backgroundColor:"#ffffff"}).then((function(t){const a=t.toDataURL("image/png",1),o=190*t.height/t.width,n=new jsPDF({orientation:"portrait",unit:"mm",format:"a4",compress:!0}),r=Math.ceil(o/277);for(let t=0;t<r;t++){t>0&&n.addPage();const e=-277*t;n.addImage(a,"PNG",10,10+e,190,o,undefined,"FAST")}n.save(`Entry#${e.target.dataset.id}.pdf`)}))["catch"]((function(t){console.error("Error generating PDF:",t),alert("Failed to generate PDF. Please try again.")}))["finally"]((function(){e.target.classList.remove("loading"),a.style.width="",a.style.padding="",a.style.backgroundColor="",a.style.boxSizing="";const o=a.querySelector("table");o&&(o.style.width="",o.style.borderCollapse="");let n=t(".mfContentToMakePdf");n&&n.html("")}))}}).init()}));