import{o as e}from"./rolldown-runtime-GyhWBpbT.js";import{F as t,I as n,L as r,O as i,P as a,s as o}from"./react-vendor-BD_NkAgt.js";import{Bn as s,Fr as c,Gr as l,Jr as u,Lr as d,Pn as f,Rn as p,Vn as m,Vr as h,Wr as g,Z as _,Zt as v,et as y,h as b,ii as x,oi as S,qr as ee,xr as C,zn as w}from"./mui-vendor-DsoH2_ot.js";import"./state-vendor-Cw2RdylD.js";import{ft as te,k as T,r as E,w as D}from"./index-Bo9BrBsx.js";import{t as O}from"./html2canvas-DZsOdvuV.js";import{t as k}from"./pod.api-DMh4RIbX.js";import{t as A}from"./jspdf.es.min-BJ2LNVDK.js";var j=e(S(),1),M=e(O(),1),N=e(b(),1),P=x(),F=()=>{let{id:e}=n(),b=t(),x=a(),[S]=r(),O=i(e=>e.auth.user);D(`Print POD`);let{success:F,error:I}=te(),L=S.get(`download`)===`pdf`,R=S.get(`print`)===`1`,z=T(O,`download_proofofdelivery`),B=T(O,`print_proofofdelivery`);(0,j.useEffect)(()=>{!z&&!B&&b(`/unauthorized`,{replace:!0})},[z,B,b]);let V=(0,j.useMemo)(()=>x.state?.from||`/sales/pod`,[x.state]),H=(0,j.useRef)(null),[U,W]=(0,j.useState)(!1),{data:G,isLoading:K,isError:q}=o({queryKey:[`pod`,e,`print`],queryFn:()=>k.getById(e),enabled:!!e});(0,j.useEffect)(()=>{q&&I(`Failed to load Proof of Delivery`)},[q,I]);let J=(0,j.useCallback)(()=>{b(V)},[V,b]),ne=(0,j.useCallback)(()=>{window.print(),J()},[J]),Y=e=>e?(0,N.default)(e).format(`DD-MM-YYYY`):`-`,X=e=>{if(!e)return``;let t=e.split(`,`).map(e=>e.trim()).filter(Boolean),n=[],r=``;return t.forEach(e=>{let t=r?`${r}, ${e}`:e;t.length>50&&r?(n.push(r),r=e):r=t}),r&&n.push(r),n.slice(0,2).join(`
`)},Z=(0,j.useMemo)(()=>{let e={SUCCESS:{label:`Delivered`,bg:`#d1e7dd`,color:`#0f5132`},PARTIAL:{label:`Partial`,bg:`#d1ecf1`,color:`#0c5460`},FAILED:{label:`Failed`,bg:`#f8d7da`,color:`#842029`},PENDING:{label:`Pending`,bg:`#fff3cd`,color:`#664d03`}};return G?.status&&e[G.status]||e.PENDING},[G?.status]),Q=G?.company_logo;(0,j.useEffect)(()=>{let e=document.createElement(`style`);return e.setAttribute(`data-print-style`,`pod-print`),e.innerHTML=`
      .print-container {
        max-width: 210mm;
        width: 100%;
        margin: 0 auto;
      }
      .print-container .MuiTableCell-root {
        padding: 6px 8px;
        font-size: 10px;
        line-height: 1.25;
        white-space: normal;
        word-break: break-word;
        vertical-align: top;
      }
      @media print {
        html, body {
          background: white;
          -webkit-print-color-adjust: exact;
          print-color-adjust: exact;
          margin: 0 !important;
          padding: 0 !important;
          width: 100% !important;
          overflow: visible !important;
        }
        body * { visibility: hidden !important; }
        .print-container, .print-container * { visibility: visible !important; }
        .print-container {
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
          margin: 0 !important;
          max-width: none !important;
          width: 100% !important;
          box-sizing: border-box !important;
          box-shadow: none !important;
          border: 0 !important;
          padding-top: 6mm !important;
          padding-bottom: 8mm !important;
          padding-left: 6mm !important;
          padding-right: 6mm !important;
        }
        .no-print { display: none !important; }
        @page { size: A4 portrait; margin: 6mm; }
        .print-container table { border-collapse: collapse !important; }
        .print-container .MuiTableCell-root {
          padding: 4px 6px !important;
          font-size: 9px !important;
          line-height: 1.2 !important;
          white-space: normal !important;
          word-break: break-word !important;
          vertical-align: top !important;
        }
      }
    `,document.head.appendChild(e),()=>{document.head.querySelector(`[data-print-style="pod-print"]`)?.remove()}},[]);let $=(0,j.useCallback)(async(e=!1)=>{if(!(!H.current||!G))try{W(!0);let t=await(0,M.default)(H.current,{scale:2,useCORS:!0,backgroundColor:`#ffffff`}),n=t.toDataURL(`image/png`),r=new A(`p`,`mm`,`a4`),i=r.internal.pageSize.getWidth(),a=r.internal.pageSize.getHeight(),o=t.height*i/t.width,s=a-6-8,c=o,l=6;for(r.addImage(n,`PNG`,0,l,i,o),c-=s;c>0;)l=c-o+6,r.addPage(),r.addImage(n,`PNG`,0,l,i,o),c-=s;r.save(`pod-${G.pod_number||G.invoice_number||G.id||`print`}.pdf`),e||F(`PDF downloaded`)}catch{e||I(`Failed to download PDF`)}finally{W(!1),e&&J()}},[G,J,I,F]);return(0,j.useEffect)(()=>{if(!(!G||K||U)){if(L&&!z){I(`You do not have permission to download POD PDF`),J();return}if(R&&!B){I(`You do not have permission to print POD`),J();return}if(L){let e=setTimeout(()=>void $(!0),400);return()=>clearTimeout(e)}if(R){if(!B)return;let e=setTimeout(()=>{window.print(),J()},500);return()=>clearTimeout(e)}}},[L,R,z,B,G,J,$,U,K,I]),K?(0,P.jsx)(d,{sx:{display:`flex`,justifyContent:`center`,alignItems:`center`,height:`100vh`},children:(0,P.jsx)(ee,{})}):q||!G?(0,P.jsxs)(d,{sx:{p:4},children:[(0,P.jsx)(l,{severity:`error`,sx:{mb:2},children:`Failed to load Proof of Delivery`}),(0,P.jsx)(c,{variant:`contained`,startIcon:(0,P.jsx)(v,{}),onClick:J,children:`Back`})]}):(0,P.jsxs)(d,{sx:{minHeight:`100vh`,bgcolor:`#f5f6f8`,py:3,px:{xs:2,md:4}},children:[(0,P.jsxs)(d,{className:`no-print`,sx:{display:L||R?`none`:`flex`,justifyContent:`flex-end`,gap:1.5,mb:2},children:[(0,P.jsx)(c,{variant:`text`,color:`inherit`,startIcon:(0,P.jsx)(v,{}),onClick:J,disabled:U,children:`Cancel`}),z&&(0,P.jsx)(c,{variant:`outlined`,startIcon:(0,P.jsx)(y,{}),onClick:()=>void $(!1),disabled:U,children:U?`Preparing PDF...`:`Download PDF`}),B&&(0,P.jsx)(c,{variant:`contained`,startIcon:(0,P.jsx)(_,{}),onClick:ne,disabled:U,children:`Print`})]}),(0,P.jsxs)(u,{ref:H,className:`print-container`,sx:{maxWidth:`210mm`,width:`100%`,mx:`auto`,p:{xs:2.5,md:3},pt:{xs:3.5,md:4},pb:{xs:4.5,md:5.5},backgroundColor:`white`,boxShadow:`0 12px 30px rgba(0,0,0,0.12)`,borderRadius:2,"@media print":{boxShadow:`none`,borderRadius:0,maxWidth:`100%`,width:`100%`,p:0,m:0}},children:[(0,P.jsxs)(d,{sx:{display:`flex`,alignItems:`flex-start`,gap:2,mb:2},children:[(0,P.jsx)(d,{sx:{height:72,width:72,borderRadius:`50%`,border:`1px solid #d6d8df`,display:`flex`,alignItems:`center`,justifyContent:`center`,overflow:`hidden`,bgcolor:`#f9fafb`,flexShrink:0},children:Q?(0,P.jsx)(E,{src:Q,alt:G.company_name||`Company`,sx:{height:`100%`,width:`100%`,objectFit:`contain`}}):(0,P.jsx)(g,{variant:`subtitle2`,color:`text.secondary`,children:`Logo`})}),(0,P.jsxs)(d,{sx:{flex:1,textAlign:`center`},children:[(0,P.jsx)(g,{variant:`h6`,sx:{fontWeight:800,letterSpacing:.4,color:`#1f2d3d`},children:G.company_name||`Company`}),(0,P.jsx)(g,{variant:`body2`,color:`text.secondary`,sx:{whiteSpace:`pre-line`,mt:.5},children:X(G.company_address)}),(0,P.jsxs)(g,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[G.company_phone,` `,G.company_phone&&G.company_email?` | `:``,` `,G.company_email]}),G.company_gst&&(0,P.jsxs)(g,{variant:`body2`,color:`text.secondary`,sx:{mt:.5},children:[`GST: `,G.company_gst]})]}),(0,P.jsx)(d,{sx:{minWidth:130,display:`flex`,justifyContent:`flex-end`},children:(0,P.jsx)(h,{label:Z.label,sx:{backgroundColor:Z.bg,color:Z.color,fontWeight:700,fontSize:`0.95rem`,height:36,px:1.5,textTransform:`uppercase`,letterSpacing:.4}})})]}),(0,P.jsx)(C,{sx:{my:2}}),(0,P.jsx)(g,{variant:`subtitle1`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:2,textTransform:`uppercase`},children:`Proof of Delivery Summary`}),(0,P.jsx)(p,{component:u,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,P.jsx)(m,{size:`small`,children:(0,P.jsxs)(s,{children:[(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`POD Number`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:G.pod_number||`-`}),(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`POD Date`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`,fontWeight:600},children:Y(G.pod_date)})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Delivered Date`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:Y(G.delivered_date)}),(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Number`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.invoice_number||`-`})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Invoice Date`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:Y(G.invoice_date)}),(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Sales Order`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.order_number||`-`})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Order Date`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:Y(G.order_date)}),(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Customer`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.customer_name||`-`})]})]})})}),(0,P.jsx)(g,{variant:`subtitle2`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:1,textTransform:`uppercase`},children:`Address Details`}),(0,P.jsx)(p,{component:u,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,P.jsx)(m,{size:`small`,children:(0,P.jsxs)(s,{children:[(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,width:`20%`,border:`1px solid #e6e8f0`},children:`Billing Address`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`,whiteSpace:`pre-line`},children:X(G.billing_address)||`-`})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Shipping Address`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`,whiteSpace:`pre-line`},children:X(G.shipping_address)||`-`})]})]})})}),(0,P.jsx)(g,{variant:`subtitle2`,sx:{textAlign:`center`,fontWeight:800,letterSpacing:1,color:`#263343`,mb:1,textTransform:`uppercase`},children:`Receiver Details`}),(0,P.jsx)(p,{component:u,elevation:0,sx:{border:`1px solid #e6e8f0`,mb:3},children:(0,P.jsx)(m,{size:`small`,children:(0,P.jsxs)(s,{children:[(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`,width:`20%`},children:`Received By`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.receiver_name||`-`})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Receiver Contact`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.receiver_phone||`-`})]}),(0,P.jsxs)(f,{children:[(0,P.jsx)(w,{sx:{fontWeight:700,bgcolor:`#f6f7fb`,border:`1px solid #e6e8f0`},children:`Delivered By`}),(0,P.jsx)(w,{sx:{border:`1px solid #e6e8f0`},children:G.delivered_by||`-`})]})]})})}),G.remarks&&(0,P.jsxs)(d,{sx:{mb:3},children:[(0,P.jsx)(g,{variant:`subtitle2`,sx:{fontWeight:700,color:`#263343`,mb:1},children:`Remarks`}),(0,P.jsx)(u,{elevation:0,sx:{p:2,border:`1px solid #e6e8f0`,bgcolor:`#f9fbff`},children:(0,P.jsx)(g,{variant:`body2`,sx:{whiteSpace:`pre-line`},children:G.remarks})})]}),(0,P.jsx)(C,{sx:{mt:2,mb:1}}),(0,P.jsxs)(d,{sx:{display:`flex`,justifyContent:`space-between`,alignItems:`center`},children:[(0,P.jsxs)(g,{variant:`body2`,sx:{fontWeight:600,color:`#1f2d3d`},children:[`POD Number: `,G.pod_number||`-`]}),(0,P.jsxs)(g,{variant:`body2`,color:`text.secondary`,children:[`Generated on: `,(0,N.default)().format(`DD/MM/YYYY HH:mm`)]})]})]})]})};export{F as default};