import{j as e}from"./ui-vendor-DUfjpZ4p.js";import{r as a}from"./react-vendor-DlVMRrtE.js";import{s as i,a9 as P,aa as C,ab as k,ac as B,e as R,H as q,B as p,am as u,D as z,b7 as F,t as A}from"./index-BKwmLAPt.js";import{I as E}from"./ImporterReport-D9UG_J_9.js";import{P as L}from"./printer-CZ36GdKY.js";const Z=({open:o,onOpenChange:x,profileId:t,reportType:n})=>{const[l,m]=a.useState(!1),[r,g]=a.useState(null),[h,w]=a.useState({}),[j,b]=a.useState([]),[D,_]=a.useState([]),[d,f]=a.useState(!1),c=a.useRef(null),v=async()=>{if(!(!c.current||!r)){f(!0);try{const s=String(r.company_name||"importer").replace(/[^A-Za-z0-9]+/g,"_");await F(c.current,`ABTC_Importer_${s}_${n}.pdf`)}catch(s){console.error(s),A({title:"Download failed",description:(s==null?void 0:s.message)||"Could not generate PDF.",variant:"destructive"})}f(!1)}};return a.useEffect(()=>{!o||!t||(async()=>{m(!0);const[{data:s},{data:N},{data:y},{data:S}]=await Promise.all([i.from("importer_profiles").select("*").eq("id",t).maybeSingle(),i.from("importer_verifications").select("*").eq("importer_profile_id",t).maybeSingle(),i.from("importer_officials").select("*").eq("importer_profile_id",t),i.from("importer_documents").select("id,document_type,file_name").eq("importer_profile_id",t)]);g(s),w(N||{}),b(y||[]),_(S||[]),m(!1)})()},[o,t]),e.jsx(P,{open:o,onOpenChange:x,children:e.jsxs(C,{className:"max-w-5xl w-[95vw] max-h-[92vh] overflow-y-auto p-0",children:[e.jsxs(k,{className:"sticky top-0 z-10 bg-background/95 backdrop-blur border-b border-border px-6 py-4 flex flex-row items-center justify-between gap-3 space-y-0",children:[e.jsxs("div",{className:"flex items-center gap-3",children:[e.jsx(B,{className:"font-heading",children:"QA Report Preview"}),e.jsxs(R,{className:"bg-accent/20 text-accent border-accent/30 gap-1",children:[e.jsx(q,{className:"h-3 w-3"})," ",n==="full"?"Full Due Diligence":"Basic"]})]}),e.jsxs("div",{className:"flex items-center gap-2 mr-8",children:[e.jsxs(p,{size:"sm",variant:"outline",onClick:()=>window.print(),children:[e.jsx(L,{className:"h-4 w-4 mr-1"})," Print"]}),e.jsxs(p,{size:"sm",onClick:v,disabled:d||l||!r,className:"gap-1",children:[d?e.jsx(u,{className:"h-4 w-4 animate-spin"}):e.jsx(z,{className:"h-4 w-4"}),"Download PDF"]})]})]}),e.jsx("div",{className:"p-6",children:l||!r?e.jsxs("div",{className:"flex items-center justify-center gap-2 p-12 text-muted-foreground",children:[e.jsx(u,{className:"h-4 w-4 animate-spin"})," Loading report…"]}):e.jsx("div",{ref:c,children:e.jsx(E,{profile:r,verif:h,officials:j,documents:D,reportType:n})})})]})})};export{Z as I};
