import{aq as T,r as i,j as n,b as o,F as k,ai as B,b2 as E,R as x,ac as $,b3 as S,D as A,f as I,g as j,q as D,v as N,x as g,y as l,w as O,z as U,A as z,i as F,B as L,as as H}from"./index-DlNZgIG9.js";import{d as M}from"./CloudDownload-BpPA4B_Z.js";const W=`${H}/erp-crm/v1`,V=({modulePath:w,fileBaseName:y,criteria:C,onImported:q,canExport:v=!0,canImport:_=!0})=>{const m=T(r=>{var a,s;return(s=(a=r==null?void 0:r.user)==null?void 0:a.data)==null?void 0:s.accessToken});i.useRef(null);const[p,d]=i.useState(null),[c,h]=i.useState(null),[e,f]=i.useState(null),R=()=>m?{Authorization:`Bearer ${m}`}:{},u=async r=>{d(null);try{const a=await fetch(`${W}/${w}/export?format=${r}`,{method:"POST",headers:{"Content-Type":"application/json",...R()},body:JSON.stringify(C||{})});if(!a.ok)throw new Error(`Export failed (${a.status})`);const s=await a.blob(),b=window.URL.createObjectURL(s),t=document.createElement("a");t.href=b,t.download=`${y}.${r}`,document.body.appendChild(t),t.click(),t.remove(),window.URL.revokeObjectURL(b)}catch(a){h({severity:"error",message:(a==null?void 0:a.message)||"Export failed"})}};return n(L,{sx:{display:"flex",gap:1,flexWrap:"wrap"},children:[v&&n(k,{children:[o(B,{startIcon:o(M,{}),sx:{bgcolor:"#1F4E78",px:2,color:"#ffffff","&:hover":{bgcolor:"#4d6274",color:"#fff"}},onClick:r=>d(r.currentTarget),children:"Export"}),n(E,{anchorEl:p,open:!!p,onClose:()=>d(null),children:[o(x,{onClick:()=>u("xlsx"),children:"Excel (.xlsx)"}),o(x,{onClick:()=>u("csv"),children:"CSV (.csv)"})]})]}),o(S,{open:!!c,autoHideDuration:5e3,onClose:()=>h(null),anchorOrigin:{vertical:"bottom",horizontal:"center"},children:c?o($,{severity:c.severity,onClose:()=>h(null),sx:{width:"100%"},children:c.message}):void 0}),n(A,{open:!!(e&&(e.failed??0)>0),onClose:()=>f(null),maxWidth:"sm",fullWidth:!0,children:[o(I,{children:"Import result"}),n(j,{dividers:!0,children:[n(D,{variant:"body2",gutterBottom:!0,children:[e!=null&&e.fileName?`${e.fileName}: `:"",(e==null?void 0:e.imported)??0," imported, ",(e==null?void 0:e.failed)??0," failed",typeof(e==null?void 0:e.totalRows)=="number"?` of ${e.totalRows} row(s)`:"","."]}),(e==null?void 0:e.errors)&&e.errors.length>0&&n(N,{size:"small",children:[o(O,{children:n(g,{children:[o(l,{children:"Row"}),o(l,{children:"Name"}),o(l,{children:"Reason"})]})}),o(U,{children:e.errors.map((r,a)=>n(g,{children:[o(l,{children:r.row}),o(l,{children:r.label||r.customerName||"-"}),o(l,{children:r.message})]},`${r.row}-${a}`))})]})]}),o(F,{children:o(z,{btnType:"close",onClick:()=>f(null),children:"Close"})})]})]})};export{V as I};
