import{c as T,m4 as k,r as h,m5 as C,j as f,m6 as w,bE as l}from"./index-5768b108.js";import{L as y}from"./LoadingButton-b684247e.js";const L=({instance_id:u,app_label:o,model_name:r,downloadFileName:P,hidePrintButton:D=!1})=>{const p=T(),{printTemplate:{TemplateList:g},system:{userAccessList:a}}=k(),[E,c]=h.useState(!1),[F,d]=h.useState(!1);h.useEffect(()=>{o&&r&&p(C({app_label:o,model_name:r,pageParams:{no_of_pages:1,page_size:10,page:1}}))},[o,r]);const x=async(e,n)=>{try{const i=e==null?void 0:e.file_url;if(!i){l.fire({icon:"error",title:"Error",text:"PDF file not available"});return}if(n===1){c(!1);const t=window.open(i,"_blank");t?t.onload=()=>{setTimeout(()=>{t.print()},1e3)}:l.fire({icon:"error",title:"Error",text:"Unable to open print window. Please check popup blocker."})}else d(!1),fetch(i).then(t=>t.blob()).then(t=>{const m=window.URL.createObjectURL(t),s=document.createElement("a");s.href=m,s.download=P,document.body.appendChild(s),s.click(),document.body.removeChild(s),window.URL.revokeObjectURL(m)}).catch(t=>{console.error("Download failed:",t),l.fire({icon:"error",title:"Error",text:"Failed to download PDF"})})}catch{c(!1),d(!1),l.fire({icon:"error",title:"Error",text:"Failed to handle PDF"})}};return f.jsxs(f.Fragment,{children:[!D&&(a==null?void 0:a.indexOf(`${o}.print_${r}`))!==-1&&f.jsxs(y,{loading:E,variant:"contained",onClick:()=>{g.length>0?(c(!0),p(w({instance_id:u,model_path:`${o}.${r}`})).then(async e=>{var n,i;console.log((n=e.payload)==null?void 0:n.response),x((i=e.payload)==null?void 0:i.response,1)}).catch(e=>{console.log("err",e),c(!1)})):l.fire({title:'<p style="font-size:20px">Error</p>',text:"No Template Found",icon:"error",confirmButtonText:"Close",confirmButtonColor:"#3085d6"})},children:[" ","Print"]}),(a==null?void 0:a.indexOf(`${o}.download_${r}`))!==-1&&f.jsx(y,{loading:F,variant:"contained",size:"large",onClick:()=>{g.length>0?(d(!0),p(w({instance_id:u,model_path:`${o}.${r}`})).then(async e=>{var n;x((n=e.payload)==null?void 0:n.response,2)}).catch(e=>{d(!1)})):l.fire({title:'<p style="font-size:20px">Error</p>',text:"No Template Found",icon:"error",confirmButtonText:"Close",confirmButtonColor:"#3085d6"})},children:"Download PDF"})]})};export{L as P};
