import{y as g,b4 as v,w as M,n as E,r as l,u as S,j as e,B as j,b6 as _,m as z,k as A,o as U,aS as R,t as B,bj as H,f as T}from"./index-7T7TTikv.js";import"./presignedAxios-BChDCNPv.js";import{C as V,d as K}from"./card-_ldW-koX.js";import{R as O}from"./refresh-cw-B7fipPS4.js";import{D as C}from"./download-Btm1n-GQ.js";/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Q=[["path",{d:"M15 5H9",key:"1tp3ed"}],["path",{d:"M15 9v3h4l-7 7-7-7h4V9z",key:"ncdc4b"}]],W=g("arrow-big-down-dash",Q);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const X=[["path",{d:"M15 3h6v6",key:"1q9fwt"}],["path",{d:"M10 14 21 3",key:"gplh6r"}],["path",{d:"M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6",key:"a6xqqp"}]],Y=g("external-link",X);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Z=[["path",{d:"M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z",key:"1rqfz7"}],["path",{d:"M14 2v4a2 2 0 0 0 2 2h4",key:"tnqrlb"}]],G=g("file",Z);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const J=[["path",{d:"M8 3H5a2 2 0 0 0-2 2v3",key:"1dcmit"}],["path",{d:"M21 8V5a2 2 0 0 0-2-2h-3",key:"1e4gt3"}],["path",{d:"M3 16v3a2 2 0 0 0 2 2h3",key:"wsl5sc"}],["path",{d:"M16 21h3a2 2 0 0 0 2-2v-3",key:"18trek"}]],ee=g("maximize",J);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const te=[["path",{d:"M8 3v3a2 2 0 0 1-2 2H3",key:"hohbtr"}],["path",{d:"M21 8h-3a2 2 0 0 1-2-2V3",key:"5jw1f3"}],["path",{d:"M3 16h3a2 2 0 0 1 2 2v3",key:"198tvr"}],["path",{d:"M16 21v-3a2 2 0 0 1 2-2h3",key:"ph8mxp"}]],se=g("minimize",te);/**
 * @license lucide-react v0.513.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const re=[["path",{d:"M6 8L2 12L6 16",key:"kyvwex"}],["path",{d:"M2 12H22",key:"1m8cig"}]],F=g("move-left",re),ae=async s=>(await v.get(`record-attachments/${s}`,{headers:M()})).data,ne=async s=>(await v.get(`record-attachments/sealed/${s}`,{headers:M()})).data,oe=async s=>{const{data:n}=await v.get(`records/${s}`,{headers:M()});return n},ce=async s=>(await v.get(`records/record-pdf/${s}`)).data,ie=async s=>(await v.get(`records/regenerate-pdf/${s}`,{headers:M()})).data,P=s=>E({queryKey:["record-pdf",s],queryFn:()=>ce(s),enabled:!!s}),de=s=>s<1024?`${s} B`:s<1024*1024?`${(s/1024).toFixed(2)} KB`:`${(s/(1024*1024)).toFixed(2)} MB`,le=({attachments:s,onDelete:n,showDelete:y})=>{const[r,m]=l.useState(null),[h,b]=l.useState(null),{t:u}=S(),{data:i,isSuccess:o}=E({queryKey:["record-attachment",r],queryFn:async()=>{if(!r)return null;try{return await ae(r)}catch{return await ne(r)}},enabled:!!r,staleTime:1e3*60*5});l.useEffect(()=>{if(i!=null&&i.presigned&&o){const t=i.presigned;N(t,i),m(null)}else h&&(N(h,i),b(null))},[i,o,h]);const N=(t,d)=>{var p;const a=(p=d==null?void 0:d.fileInfo)==null?void 0:p.contentType;a==="application/vnd.ms-excel"||a==="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"?window.location.href=`ms-excel:ofe|u|${t}`:a==="application/vnd.ms-powerpoint"||a==="application/vnd.openxmlformats-officedocument.presentationml.presentation"?window.location.href=`ms-powerpoint:ofe|u|${t}`:a==="application/msword"||a==="application/vnd.openxmlformats-officedocument.wordprocessingml.document"?window.location.href=`ms-word:ofe|u|${t}`:window.open(t,"_blank","noopener,noreferrer")},k=async(t,d)=>{const p=await(await fetch(t,{})).blob(),w=URL.createObjectURL(p),x=document.createElement("a");x.href=w,x.download=d,document.body.appendChild(x),x.click(),document.body.removeChild(x),URL.revokeObjectURL(w)},f=t=>{t&&m(t)};return e.jsx(V,{className:"w-full p-4",children:e.jsx(K,{children:s.length>0?e.jsx("ul",{className:"space-y-2",children:s.map(t=>e.jsxs("li",{className:"flex flex-col sm:flex-row sm:items-center justify-between border p-3 rounded-lg bg-white dark:bg-gray-800 dark:border-gray-700 w-full gap-3",children:[e.jsxs("div",{className:"flex items-start sm:items-center space-x-3 min-w-0 w-full sm:w-auto",children:[e.jsx(G,{className:"w-8 h-8 text-primary-600 dark:text-primary-300 flex-shrink-0 mt-0.5 sm:mt-0"}),e.jsxs("div",{className:"flex flex-col min-w-0 flex-1 sm:flex-initial",title:t==null?void 0:t.fileInfo.originalname,children:[e.jsx("span",{className:"truncate font-medium text-gray-700 dark:text-gray-100 text-sm sm:text-base",title:t==null?void 0:t.fileInfo.originalname,children:t==null?void 0:t.fileInfo.originalname}),e.jsx("span",{className:"text-xs sm:text-sm text-gray-500 dark:text-gray-300",children:de((t==null?void 0:t.fileInfo.size)||0)})]})]}),e.jsxs("div",{className:"flex flex-col sm:flex-row items-stretch sm:items-center gap-2 w-full sm:w-auto",children:[e.jsxs(j,{size:"sm",variant:"outline",className:"w-full sm:w-auto justify-center flex items-center space-x-1 bg-primary-50 hover:bg-primary-100 text-primary-700 dark:bg-primary-900/50 dark:hover:bg-primary-800/70 dark:text-white dark:border-primary-700",onClick:()=>f((t==null?void 0:t.id)??b(t.presigned||"")??""),children:[e.jsx(Y,{className:"w-4 h-4"}),e.jsx("span",{children:u("viewDetail.open")})]}),e.jsxs(j,{size:"sm",variant:"outline",className:"w-full sm:w-auto justify-center flex items-center space-x-1 bg-primary-50 hover:bg-primary-100 text-primary-700 dark:bg-primary-900/50 dark:hover:bg-primary-800/70 dark:text-white dark:border-primary-700",onClick:()=>{var d;return k(t.presigned||"",((d=t==null?void 0:t.fileInfo)==null?void 0:d.originalname)||"")},children:[e.jsx(W,{className:"w-4 h-4"}),e.jsx("span",{children:u("userRecord.Download")})]}),y&&n&&e.jsxs(j,{size:"sm",variant:"ghost",className:"w-full sm:w-auto justify-center flex items-center space-x-1 text-red-600 hover:bg-red-50 dark:text-red-400 dark:hover:bg-red-900/50",onClick:()=>n((t==null?void 0:t.id)||t.itemId||""),children:[e.jsx(_,{className:"w-4 h-4"}),e.jsx("span",{className:"sm:hidden",children:u("common.delete","Delete")})]})]})]},(t==null?void 0:t.id)||(t==null?void 0:t.itemId)||(t==null?void 0:t.fileInfo.fileName)||Math.random().toString()))}):e.jsx("p",{className:"text-gray-500 dark:text-gray-400",children:u("viewDetail.noAttachmentsAvailable")})})})},me=({title:s,children:n})=>e.jsxs("div",{className:"bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-xl p-4 hover:border-primary-200 dark:hover:border-primary-600 transition-colors",children:[e.jsx("label",{className:"block text-base font-semibold text-gray-500 dark:text-gray-400 uppercase tracking-wide mb-3",children:s}),n]}),xe=({itemId:s,onBack:n})=>{var D;const y=z(),{t:r}=S(),[m,h]=l.useState(!1),[b,u]=l.useState(!0),{handleError:i}=A(r),{data:o,isLoading:N,error:k}=P(s),{data:f,error:t}=E({queryKey:["record",s],queryFn:()=>oe(s),enabled:!!s}),d=l.useRef(null);l.useEffect(()=>{t&&t!==d.current&&(d.current=t,i(t))},[t,i]);const a=l.useMemo(()=>{if(!o)return null;if(o instanceof ArrayBuffer){const c=new Blob([o],{type:"application/pdf"});return URL.createObjectURL(c)}return typeof o=="object"&&"presigned"in o?o.presigned:typeof o=="string"&&o.startsWith("http")?o:null},[o]);l.useEffect(()=>()=>{a&&a.startsWith("blob:")&&URL.revokeObjectURL(a)},[a]);const{mutate:p,isPending:w}=U({mutationFn:async()=>{if(!s)throw new Error("Record ID is missing");await ie(s)},onSuccess:()=>{B.success(r("msg.pdfRegenerated")),y.invalidateQueries({queryKey:["attachmentResponse",s]})},onError:c=>{i(c)}}),x=()=>{if(!a)return;const c=document.createElement("a");c.href=a,c.download=`document-${s}.pdf`,document.body.appendChild(c),c.click(),document.body.removeChild(c)},$=()=>{h(!0)},L=()=>{h(!1)},q=()=>{u(!1)};return l.useEffect(()=>{const c=I=>{I.key==="Escape"&&m&&L()};return document.addEventListener("keydown",c),()=>document.removeEventListener("keydown",c)},[m]),N?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-gray-50",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-12 w-12 border-b-2 border-blue-600 mx-auto mb-4"}),e.jsxs("p",{className:"text-gray-600",children:[r("loading"),"..."]})]})}):k?e.jsx("div",{className:"min-h-screen flex items-center justify-center bg-gray-50",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"bg-red-100 rounded-full p-3 w-12 h-12 flex items-center justify-center mx-auto mb-4",children:e.jsx(R,{className:"h-6 w-6 text-red-600"})}),e.jsx("h3",{className:"text-lg font-medium text-red-800 mb-2",children:r("error.failedToLoadPdf")}),e.jsx("p",{className:"text-red-600 mb-4",children:k.message}),e.jsx("div",{className:"flex",children:e.jsxs(j,{onClick:n,size:"sm",className:"bg-gray-600 text-sm px-1 py-1 w-[100px] hover:bg-gray-700",children:[e.jsx(F,{className:"w-4 h-4"}),r("common.back")]})})]})}):e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"min-h-screen bg-gray-50 py-8",children:e.jsxs("div",{className:"max-w-7xl mx-auto px-4 sm:px-6 lg:px-8",children:[e.jsxs("div",{className:"flex justify-between items-center mb-6",children:[e.jsx("div",{className:"flex",children:e.jsxs(j,{onClick:n,size:"sm",className:"bg-gray-600 text-sm px-1 py-1 w-[100px] hover:bg-gray-700",children:[e.jsx(F,{className:"w-4 h-4"}),r("common.back")]})}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("button",{onClick:()=>p(),disabled:w||!a,className:"flex items-center px-4 py-2 bg-white border border-gray-300 rounded-lg shadow-sm text-gray-700 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-200",children:[e.jsx(O,{className:`h-4 w-4 mr-2 ${w?"animate-spin":""}`}),r("viewDetail.regeneratePdf")]}),e.jsxs("button",{onClick:x,disabled:!a,className:"flex items-center px-4 py-2 bg-white border border-gray-300 rounded-lg shadow-sm text-gray-700 hover:bg-gray-50 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-200",children:[e.jsx(C,{className:"h-4 w-4 mr-2"}),r("userRecord.Download")]}),e.jsxs("button",{onClick:$,disabled:!a,className:"flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg shadow-sm hover:bg-blue-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors duration-200",children:[e.jsx(ee,{className:"h-4 w-4 mr-2"}),r("viewDetail.fullScreen")]})]})]}),e.jsx("div",{className:"bg-white rounded-xl shadow-lg border border-gray-200 overflow-hidden",children:a?e.jsxs("div",{className:"relative",children:[b&&e.jsx("div",{className:"absolute inset-0 flex items-center justify-center bg-gray-50",children:e.jsxs("div",{className:"text-center",children:[e.jsx("div",{className:"animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600 mx-auto mb-2"}),e.jsxs("p",{className:"text-gray-600 dark:text-gray-400 flex items-center gap-2",children:[e.jsx("span",{className:"animate-spin rounded-full h-4 w-4 border-t-2 border-primary-600 dark:border-primary-400"}),r("viewDetail.loading")]})]})}),e.jsx("iframe",{src:a,title:"PDF Preview",width:"100%",height:"800px",style:{border:"none"},onLoad:q,className:b?"opacity-0":"opacity-100 transition-opacity duration-300"})]}):e.jsxs("div",{className:"flex flex-col items-center justify-center h-96 bg-gray-50",children:[e.jsx("div",{className:"bg-gray-100 rounded-full p-4 w-16 h-16 flex items-center justify-center mb-4",children:e.jsx(R,{className:"h-8 w-8 text-gray-400"})}),e.jsx("h3",{className:"text-lg font-medium text-gray-900 mb-2",children:r("noPdfAvailable")}),e.jsx("p",{className:"text-gray-500 text-center max-w-md",children:r("noPdfAvailableDescription")||"The PDF document is not available. You can try regenerating it using the button above."})]})})]})}),m&&a&&e.jsxs("div",{className:"fixed inset-0 bg-black z-50 flex flex-col",children:[e.jsxs("div",{className:"bg-gray-900 text-white px-6 py-4 flex justify-between items-center",children:[e.jsx("div",{className:"flex items-center",children:e.jsx("span",{className:"font-medium",children:"Document Preview"})}),e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx("button",{onClick:x,className:"flex items-center px-3 py-2 bg-gray-700 rounded-lg hover:bg-gray-600 transition-colors duration-200",title:"Download",children:e.jsx(C,{className:"h-4 w-4"})}),e.jsx("button",{onClick:L,className:"flex items-center px-3 py-2 bg-gray-700 rounded-lg hover:bg-gray-600 transition-colors duration-200",title:"Exit Fullscreen",children:e.jsx(se,{className:"h-4 w-4"})})]})]}),e.jsx("div",{className:"flex-1 bg-gray-800",children:e.jsx("iframe",{src:a,title:"PDF Preview - Full Screen",width:"100%",height:"100%",style:{border:"none"}})}),e.jsx("div",{className:"bg-gray-900 text-white px-6 py-3 text-sm text-center",children:e.jsx("p",{children:"Press ESC to exit fullscreen mode"})})]}),e.jsx("div",{className:"mt-3",children:e.jsx(me,{title:r("viewDetail.attachments"),children:e.jsx(le,{attachments:((D=f==null?void 0:f.content[0])==null?void 0:D.recordAttachments)??[],showDelete:!1})})})]})},be=()=>{const{id:s}=H(),n=T();if(!s)return e.jsx("div",{className:"p-4 text-red-500",children:"Error: Invalid record ID"});const{data:y,isLoading:r,error:m}=P(s);return l.useEffect(()=>{},[y,r,m]),r?e.jsx("div",{className:"p-4 text-center",children:"Loading record..."}):m?e.jsxs("div",{className:"p-4 text-center text-red-500",children:["Error loading record: ",m.message]}):e.jsx(xe,{itemId:s,onBack:()=>n("/user-management/migrated-records-management")})};export{be as default};
