import{j as e}from"./ui-vendor-DUfjpZ4p.js";import{h as _e,r as p,L as ve}from"./react-vendor-DlVMRrtE.js";import{h as ne,u as je,am as J,B as x,b4 as be,e as v,H as X,ar as ee,E as A,C as k,k as C,m as S,d as P,at as m,aG as se,I as f,P as ye,aL as E,aM as L,aN as T,aO as U,aP as R,f as ae,F as ie,b5 as we,as as re,s as n,t as h,b6 as Ne,a9 as ke,aa as Ce,ab as Se,ac as Pe,D as Oe}from"./index-BKwmLAPt.js";import{I as qe}from"./ImporterReportPreviewDialog-loFKHEo6.js";import{E as Ie}from"./external-link-BLhC6a5v.js";import"./charts-vendor-wgDUSDZK.js";import"./supabase-vendor-Bd7R0Yn5.js";import"./ImporterReport-D9UG_J_9.js";import"./printer-CZ36GdKY.js";/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const De=ne("EyeOff",[["path",{d:"M10.733 5.076a10.744 10.744 0 0 1 11.205 6.575 1 1 0 0 1 0 .696 10.747 10.747 0 0 1-1.444 2.49",key:"ct8e1f"}],["path",{d:"M14.084 14.158a3 3 0 0 1-4.242-4.242",key:"151rxh"}],["path",{d:"M17.479 17.499a10.75 10.75 0 0 1-15.417-5.151 1 1 0 0 1 0-.696 10.75 10.75 0 0 1 4.446-5.143",key:"13bj9a"}],["path",{d:"m2 2 20 20",key:"1ooewy"}]]);/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Ae=ne("Image",[["rect",{width:"18",height:"18",x:"3",y:"3",rx:"2",ry:"2",key:"1m3agn"}],["circle",{cx:"9",cy:"9",r:"2",key:"af1f0g"}],["path",{d:"m21 15-3.086-3.086a2 2 0 0 0-2.828 0L6 21",key:"1xmnt7"}]]),Ee=["pending_review","documents_submitted","document_verification_in_progress","orc_verification_completed","directors_verified","shareholders_verified","beneficial_owners_verified","contact_verified","address_verified","onsite_verification_pending","onsite_verification_completed","approved","rejected","suspended"],te=["orc_verified","directors_verified","shareholders_verified","beneficial_owners_verified","trustees_verified","address_verified","contact_verified","onsite_verified","operationally_active","abtc_verified"],Le=["director","shareholder","beneficial_owner","trustee","contact"],Te=["Certificate of Incorporation","Business Registration","Tax Identification (TIN)","VAT Certificate","Directors ID","Beneficial Owner ID","Proof of Address","Utility Bill","Bank Statement","Import License","Site Photos","Other"],le=[{key:"company_name",label:"Company name"},{key:"registration_number",label:"Registration number"},{key:"tin",label:"TIN"},{key:"industry",label:"Industry / sector"},{key:"years_in_operation",label:"Years in operation",type:"number"},{key:"website",label:"Website"},{key:"email",label:"Email",type:"email"},{key:"phone",label:"Phone / WhatsApp"},{key:"country",label:"Country"},{key:"region",label:"Region"},{key:"city",label:"City"},{key:"ghana_post_gps",label:"Ghana Post GPS"},{key:"estimated_annual_import_volume",label:"Annual import volume"},{key:"warehouse_location",label:"Warehouse / office"},{key:"business_address",label:"Business address",full:!0,multiline:!0},{key:"products_imported",label:"Products imported",full:!0,multiline:!0},{key:"countries_imported_from",label:"Countries imported from",full:!0,multiline:!0},{key:"key_suppliers",label:"Key suppliers",full:!0,multiline:!0}],We=()=>{var Z;const{id:l}=_e(),{user:u}=je(),[oe,B]=p.useState(!0),[$,z]=p.useState(!1),[t,V]=p.useState(null),[b,y]=p.useState([]),[O,q]=p.useState([]),[_,Q]=p.useState({}),[I,F]=p.useState("pending_review"),[G,M]=p.useState(""),[d,W]=p.useState(null),[H,D]=p.useState(null),[Y,de]=p.useState("Other"),w=async()=>{if(!l)return;B(!0);const{data:s}=await n.from("importer_profiles").select("*").eq("id",l).maybeSingle(),{data:a}=await n.from("importer_officials").select("*").eq("importer_profile_id",l).order("created_at",{ascending:!0}),{data:i,error:o}=await n.from("importer_documents").select("*").eq("importer_profile_id",l).order("uploaded_at",{ascending:!0});o&&console.error("Load documents error:",o);const{data:r}=await n.from("importer_verifications").select("*").eq("importer_profile_id",l).maybeSingle();V(s),y(a||[]),q(i||[]),Q(r||{}),F((s==null?void 0:s.profile_status)||"pending_review"),M((r==null?void 0:r.notes)||""),B(!1)};p.useEffect(()=>{w()},[l]);const K=(s,a)=>V(i=>({...i,[s]:a})),g=(s,a)=>y(i=>i.map((o,r)=>r===s?{...o,...a,_dirty:!0}:o)),ce=()=>y(s=>[...s,{_new:!0,_dirty:!0,role_type:"director",full_name:"",importer_profile_id:l}]),me=async s=>{const a=b[s];a.id&&await n.from("importer_officials").delete().eq("id",a.id),y(i=>i.filter((o,r)=>r!==s)),h({title:"Official removed"})},pe=async s=>{var N;const{data:a,error:i}=await n.storage.from("importer-docs").createSignedUrl(s.file_path,600);if(i||!(a!=null&&a.signedUrl)){h({title:"Open failed",variant:"destructive"});return}const o=((N=(s.file_name||"").split(".").pop())==null?void 0:N.toLowerCase())||"",r=["png","jpg","jpeg","gif","webp","bmp"].includes(o),c=o==="pdf";let j=a.signedUrl;if(c)try{const ge=await(await fetch(a.signedUrl)).blob();j=URL.createObjectURL(new Blob([ge],{type:"application/pdf"}))}catch{}W({url:j,name:s.file_name,type:r?"image":c?"pdf":"other"})},ue=async s=>{const{data:a}=await n.storage.from("importer-docs").createSignedUrl(s.file_path,60);a!=null&&a.signedUrl&&window.open(a.signedUrl,"_blank")},he=async s=>{confirm(`Delete document "${s.file_name}"?`)&&(await n.storage.from("importer-docs").remove([s.file_path]),await n.from("importer_documents").delete().eq("id",s.id),q(a=>a.filter(i=>i.id!==s.id)),h({title:"Document deleted"}))},xe=async(s,a)=>{if(!l||!u)return;const i=s.name.replace(/[^a-zA-Z0-9._-]/g,"_"),o=`${t.created_by||u.id}/${l}/${Date.now()}_${i}`,{error:r}=await n.storage.from("importer-docs").upload(o,s,{upsert:!1,contentType:s.type||void 0});if(r){h({title:"Upload failed",description:r.message,variant:"destructive"});return}const{data:c,error:j}=await n.from("importer_documents").insert({importer_profile_id:l,document_type:a||"Other",file_path:o,file_name:s.name,uploaded_by:u.id}).select("*").single();if(j){h({title:"Save failed",description:j.message,variant:"destructive"});return}q(N=>[...N,c]),h({title:"Document uploaded"})},fe=async()=>{var s;if(!(!l||!u)){z(!0);try{const a={profile_status:I};le.forEach(r=>{let c=t[r.key];r.type==="number"&&(c=c===""||c==null?null:Number(c)),a[r.key]=c??null});const{error:i}=await n.from("importer_profiles").update(a).eq("id",l);if(i)throw i;for(const r of b.filter(c=>c._dirty)){const c={importer_profile_id:l,role_type:r.role_type,full_name:r.full_name,position:r.position||null,id_number:r.id_number||null,nationality:r.nationality||null,ownership_pct:r.ownership_pct===""||r.ownership_pct==null?null:Number(r.ownership_pct),email:r.email||null,phone:r.phone||null,is_beneficial_owner:r.role_type==="beneficial_owner"};r.id?await n.from("importer_officials").update(c).eq("id",r.id):(s=r.full_name)!=null&&s.trim()&&await n.from("importer_officials").insert(c)}const o={importer_profile_id:l,notes:G,verified_by:u.id,verified_at:new Date().toISOString()};for(const r of te)o[r]=!!_[r];_!=null&&_.id?await n.from("importer_verifications").update(o).eq("id",_.id):await n.from("importer_verifications").insert(o),await n.from("importer_audit_log").insert({importer_profile_id:l,actor_id:u.id,action:"admin_review_saved",after_data:{profile_status:I}}),h({title:"Saved",description:"Profile and verification updated."}),await w()}catch(a){h({title:"Save failed",description:a.message,variant:"destructive"})}finally{z(!1)}}};return oe?e.jsxs("div",{className:"flex items-center gap-2 p-6",children:[e.jsx(J,{className:"h-4 w-4 animate-spin"})," Loading…"]}):t?e.jsxs("div",{className:"space-y-6 max-w-6xl mx-auto",children:[e.jsx(x,{asChild:!0,variant:"ghost",size:"sm",children:e.jsxs(ve,{to:"/portal/importer-network/admin",children:[e.jsx(be,{className:"h-4 w-4 mr-1"})," Back to queue"]})}),e.jsxs("div",{className:"relative overflow-hidden rounded-2xl bg-navy-gradient text-white p-6 shadow-xl",children:[e.jsx("div",{className:"absolute inset-0 opacity-10 bg-[radial-gradient(circle_at_70%_30%,white,transparent_45%)]"}),e.jsxs("div",{className:"relative flex flex-wrap items-center justify-between gap-4",children:[e.jsxs("div",{children:[e.jsx(v,{className:"bg-white/15 text-white border-white/20 mb-2",children:"Editing submission"}),e.jsx("h1",{className:"font-heading text-2xl md:text-3xl font-bold",children:t.company_name}),e.jsx("p",{className:"text-white/70 text-sm mt-1",children:[t.city,t.country].filter(Boolean).join(", ")||"—"})]}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsx(v,{className:"bg-white/15 text-white border-white/20",children:(Z=t.profile_status)==null?void 0:Z.replace(/_/g," ")}),t.onboarding_payment_status==="paid"?e.jsx(v,{className:"bg-emerald-500/20 text-emerald-100 border-emerald-300/30",children:"💳 Onboarding Paid ($100)"}):e.jsx(v,{className:"bg-red-500/25 text-red-100 border-red-300/40",children:"⚠ Onboarding Unpaid"}),t.qa_approved&&e.jsxs(v,{className:"bg-amber-400/20 text-amber-100 border-amber-300/30",children:[e.jsx(X,{className:"h-3 w-3 mr-1"})," QA Approved"]}),t.published&&e.jsxs(v,{className:"bg-emerald-500/20 text-emerald-200 border-emerald-300/30",children:[e.jsx(ee,{className:"h-3 w-3 mr-1"})," Published"]}),e.jsxs(x,{size:"sm",variant:"outline",className:"border-white/30 text-white hover:bg-white/10 bg-transparent",onClick:()=>D("basic"),children:[e.jsx(A,{className:"h-4 w-4 mr-1"})," Preview Basic"]}),e.jsxs(x,{size:"sm",className:"bg-accent text-accent-foreground hover:bg-gold-light",onClick:()=>D("full"),children:[e.jsx(A,{className:"h-4 w-4 mr-1"})," Preview Full"]})]})]})]}),e.jsxs(k,{children:[e.jsx(C,{children:e.jsx(S,{children:"Company Information (editable)"})}),e.jsxs(P,{className:"grid grid-cols-1 md:grid-cols-2 gap-4",children:[le.map(s=>e.jsxs("div",{className:`space-y-1 ${s.full?"md:col-span-2":""}`,children:[e.jsx(m,{children:s.label}),s.multiline?e.jsx(se,{value:t[s.key]??"",onChange:a=>K(s.key,a.target.value),rows:2}):e.jsx(f,{type:s.type||"text",value:t[s.key]??"",onChange:a=>K(s.key,a.target.value)})]},s.key)),t.linked_company_id&&e.jsxs("div",{className:"md:col-span-2 text-xs text-accent",children:["Linked to existing company record (ID: ",t.linked_company_id,")"]})]})]}),e.jsxs(k,{children:[e.jsx(C,{children:e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx(S,{children:"Officials, Directors & Beneficial Owners"}),e.jsxs(x,{size:"sm",variant:"outline",onClick:ce,children:[e.jsx(ye,{className:"h-4 w-4 mr-1"})," Add"]})]})}),e.jsxs(P,{className:"space-y-3",children:[b.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"None listed."}),b.map((s,a)=>e.jsxs("div",{className:"grid grid-cols-1 md:grid-cols-12 gap-3 border border-border rounded-lg p-3",children:[e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(m,{children:"Role"}),e.jsxs(E,{value:s.role_type,onValueChange:i=>g(a,{role_type:i}),children:[e.jsx(L,{children:e.jsx(T,{})}),e.jsx(U,{children:Le.map(i=>e.jsx(R,{value:i,children:i.replace(/_/g," ")},i))})]})]}),e.jsxs("div",{className:"md:col-span-4",children:[e.jsx(m,{children:"Full name"}),e.jsx(f,{value:s.full_name||"",onChange:i=>g(a,{full_name:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(m,{children:"Position"}),e.jsx(f,{value:s.position||"",onChange:i=>g(a,{position:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-2",children:[e.jsx(m,{children:"Ownership %"}),e.jsx(f,{type:"number",value:s.ownership_pct??"",onChange:i=>g(a,{ownership_pct:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(m,{children:"ID number"}),e.jsx(f,{value:s.id_number||"",onChange:i=>g(a,{id_number:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(m,{children:"Nationality"}),e.jsx(f,{value:s.nationality||"",onChange:i=>g(a,{nationality:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-3",children:[e.jsx(m,{children:"Email"}),e.jsx(f,{value:s.email||"",onChange:i=>g(a,{email:i.target.value})})]}),e.jsxs("div",{className:"md:col-span-2",children:[e.jsx(m,{children:"Phone"}),e.jsx(f,{value:s.phone||"",onChange:i=>g(a,{phone:i.target.value})})]}),e.jsx("div",{className:"md:col-span-1 flex items-end justify-end",children:e.jsx(x,{size:"icon",variant:"ghost",onClick:()=>me(a),children:e.jsx(ae,{className:"h-4 w-4"})})})]},s.id||`new-${a}`))]})]}),e.jsxs(k,{children:[e.jsx(C,{children:e.jsxs("div",{className:"flex flex-col gap-2",children:[e.jsx("div",{className:"flex items-center justify-between gap-3 flex-wrap",children:e.jsxs(S,{children:["Documents (",O.length,")"]})}),e.jsxs("div",{className:"flex items-end gap-2 flex-wrap bg-muted/40 border border-border rounded-lg p-3",children:[e.jsxs("div",{className:"flex-1 min-w-[200px]",children:[e.jsx(m,{className:"text-xs",children:"Document type"}),e.jsxs(E,{value:Y,onValueChange:de,children:[e.jsx(L,{children:e.jsx(T,{})}),e.jsx(U,{children:Te.map(s=>e.jsx(R,{value:s,children:s},s))})]})]}),e.jsxs("div",{className:"flex-1 min-w-[240px]",children:[e.jsx(m,{className:"text-xs",children:"Upload on behalf of client"}),e.jsx(f,{type:"file",onChange:s=>{var i;const a=(i=s.target.files)==null?void 0:i[0];a&&xe(a,Y),s.currentTarget.value=""}})]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Use this to attach documents received via email or WhatsApp once you've verified them."})]})}),e.jsxs(P,{className:"space-y-2",children:[O.length===0&&e.jsx("p",{className:"text-sm text-muted-foreground",children:"No documents uploaded."}),O.map(s=>{var o;const a=((o=(s.file_name||"").split(".").pop())==null?void 0:o.toLowerCase())||"",i=["png","jpg","jpeg","gif","webp","bmp"].includes(a);return e.jsxs("div",{className:"flex flex-wrap items-center justify-between gap-3 border border-border rounded-lg p-3 text-sm hover:border-primary/40 transition-colors",children:[e.jsxs("div",{className:"flex items-center gap-3 min-w-0",children:[e.jsx("div",{className:"h-9 w-9 rounded-md bg-primary/10 border border-primary/20 flex items-center justify-center shrink-0",children:i?e.jsx(Ae,{className:"h-4 w-4 text-primary"}):e.jsx(ie,{className:"h-4 w-4 text-primary"})}),e.jsxs("div",{className:"min-w-0",children:[e.jsx("div",{className:"font-semibold truncate",children:s.document_type}),e.jsx("div",{className:"text-xs text-muted-foreground truncate",children:s.file_name})]})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs(x,{size:"sm",variant:"outline",onClick:()=>pe(s),children:[e.jsx(A,{className:"h-4 w-4 mr-1"})," View"]}),e.jsx(x,{size:"sm",variant:"ghost",onClick:()=>ue(s),title:"Open in new tab",children:e.jsx(Ie,{className:"h-4 w-4"})}),e.jsx(x,{size:"sm",variant:"ghost",className:"text-rose-600",onClick:()=>he(s),children:e.jsx(ae,{className:"h-4 w-4"})})]})]},s.id)})]})]}),e.jsxs(k,{children:[e.jsx(C,{children:e.jsx(S,{children:"Verification"})}),e.jsxs(P,{className:"space-y-4",children:[e.jsx("div",{className:"grid grid-cols-1 md:grid-cols-2 gap-3",children:te.map(s=>e.jsxs("label",{className:"flex items-center gap-2 text-sm cursor-pointer p-2 rounded-md hover:bg-muted/50",children:[e.jsx(we,{checked:!!_[s],onCheckedChange:a=>Q(i=>({...i,[s]:!!a}))}),s.replace(/_/g," ")]},s))}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(m,{children:"Reviewer notes"}),e.jsx(se,{value:G,onChange:s=>M(s.target.value),rows:3})]}),e.jsxs("div",{className:"flex flex-wrap items-center gap-3",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(m,{children:"Profile status"}),e.jsxs(E,{value:I,onValueChange:F,children:[e.jsx(L,{className:"w-72",children:e.jsx(T,{})}),e.jsx(U,{children:Ee.map(s=>e.jsx(R,{value:s,children:s.replace(/_/g," ")},s))})]})]}),e.jsxs("div",{className:"flex items-center gap-3 border border-border rounded-lg px-4 py-2 bg-amber-50/50",children:[e.jsx(X,{className:`h-4 w-4 ${t.qa_approved?"text-amber-600":"text-muted-foreground"}`}),e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-semibold",children:t.qa_approved?"QA Approved":"QA Pending"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:"Required before publishing"})]}),e.jsx(re,{checked:!!t.qa_approved,onCheckedChange:async s=>{!l||!u||(await n.from("importer_profiles").update({qa_approved:s,qa_approved_at:s?new Date().toISOString():null,qa_approved_by:s?u.id:null,...s?{}:{published:!1,published_at:null}}).eq("id",l),await n.from("importer_audit_log").insert({importer_profile_id:l,actor_id:u.id,action:s?"qa_approved":"qa_revoked"}),h({title:s?"QA approved":"QA approval revoked"}),await w())}})]}),e.jsxs("div",{className:`flex items-center gap-3 border rounded-lg px-4 py-2 ${t.qa_approved?"border-border":"border-dashed border-muted opacity-60"}`,children:[t.published?e.jsx(ee,{className:"h-4 w-4 text-emerald-600"}):e.jsx(De,{className:"h-4 w-4 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("div",{className:"text-sm font-semibold",children:t.published?"Published":"Not Published"}),e.jsx("div",{className:"text-xs text-muted-foreground",children:t.qa_approved?"Visible in public directory":"QA approval required"})]}),e.jsx(re,{disabled:!t.qa_approved,checked:!!t.published,onCheckedChange:async s=>{if(!(!l||!u)){if(s&&!t.qa_approved){h({title:"QA approval required",description:"Approve QA before publishing.",variant:"destructive"});return}await n.from("importer_profiles").update({published:s,published_at:s?new Date().toISOString():null}).eq("id",l),await n.from("importer_audit_log").insert({importer_profile_id:l,actor_id:u.id,action:s?"published":"unpublished"}),h({title:s?"Profile published":"Profile unpublished"}),await w()}}})]}),e.jsxs(x,{onClick:fe,disabled:$,className:"ml-auto",children:[$?e.jsx(J,{className:"h-4 w-4 mr-2 animate-spin"}):e.jsx(Ne,{className:"h-4 w-4 mr-2"}),"Save All Changes"]})]})]})]}),e.jsx(ke,{open:!!d,onOpenChange:s=>!s&&W(null),children:e.jsxs(Ce,{className:"max-w-5xl h-[85vh] flex flex-col",children:[e.jsx(Se,{children:e.jsxs(Pe,{className:"flex items-center gap-2",children:[e.jsx(ie,{className:"h-4 w-4"})," ",d==null?void 0:d.name]})}),e.jsx("div",{className:"flex-1 overflow-auto bg-muted/30 rounded-md",children:(d==null?void 0:d.type)==="image"?e.jsx("img",{src:d.url,alt:d.name,className:"max-w-full max-h-full mx-auto"}):(d==null?void 0:d.type)==="pdf"?e.jsx("iframe",{src:d.url,title:d.name,className:"w-full h-full"}):d?e.jsxs("div",{className:"p-6 text-center space-y-3",children:[e.jsx("p",{className:"text-sm text-muted-foreground",children:"Preview not supported for this file type."}),e.jsx(x,{asChild:!0,children:e.jsxs("a",{href:d.url,target:"_blank",rel:"noreferrer",children:[e.jsx(Oe,{className:"h-4 w-4 mr-1"})," Open file"]})})]}):null})]})}),e.jsx(qe,{open:!!H,onOpenChange:s=>!s&&D(null),profileId:l||null,reportType:H||"basic"})]}):e.jsx("div",{className:"p-6",children:"Not found."})};export{We as default};
