1import{r as i,k as l,j as e}from"./index-D0CLt0PK.js";import{F as f}from"./file-text-BrDHY82M.js";import{U as b}from"./upload-DfGXKiKx.js";import{D as N}from"./download-DknK7O33.js";function u(d,n){const o=window.URL.createObjectURL(d),a=document.createElement("a");a.href=o,a.download=n,document.body.appendChild(a),a.click(),document.body.removeChild(a),window.URL.revokeObjectURL(o)}function U(){const[d,n]=i.useState([]),[o,a]=i.useState(!1),[m,c]=i.useState("");i.useEffect(()=>{x()},[]);const x=async()=>{try{const s=await l.getHistory();n(Array.isArray(s.data)?s.data:[])}catch(s){console.error("Failed to load import history:",s)}},p=async(s,t)=>{if(t){a(!0),c("");try{const r=s==="crops"?await l.uploadCrops(t):await l.uploadCustomers(t),g=r.data?.details?.success??0,y=r.data?.details?.failed??0;c(`Import finished: ${g} succeeded, ${y} failed.`),await x()}catch(r){console.error("Upload failed:",r),c(`Import failed: ${r.response?.data?.error||r.message}`)}finally{a(!1)}}},h=async s=>{try{const t=await l.downloadTemplate(s);u(t.data,`${s}_template.csv`)}catch(t){console.error("Template download failed:",t),c("Failed to download template.")}},j=async s=>{try{const t=s==="crops"?await l.exportCrops():await l.exportCustomers();u(t.data,`${s}_export.csv`)}catch(t){console.error("Export failed:",t),c("Failed to export CSV.")}};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold text-gray-800",children:"Data Import / Export"}),e.jsx("p",{className:"text-gray-600 mt-1",children:"Upload CSVs to save time and export your records anytime."})]}),m&&e.jsx("div",{className:"card bg-blue-50 border border-blue-200 text-blue-800",children:m}),e.jsxs("div",{className:"grid grid-cols-1 lg:grid-cols-2 gap-6",children:[e.jsxs("div",{className:"card",children:[e.jsx("h2",{className:"text-xl font-semibold mb-3",children:"Crops CSV"}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs("button",{className:"btn-secondary flex items-center gap-2",onClick:()=>h("crops"),children:[e.jsx(f,{className:"w-4 h-4"}),"Download Template"]}),e.jsxs("label",{className:"btn-primary flex items-center gap-2 cursor-pointer",children:[e.jsx(b,{className:"w-4 h-4"}),"Upload CSV",e.jsx("input",{type:"file",accept:".csv,text/csv",className:"hidden",disabled:o,onChange:s=>p("crops",s.target.files?.[0])})]}),e.jsxs("button",{className:"btn-secondary flex items-center gap-2",onClick:()=>j("crops"),children:[e.jsx(N,{className:"w-4 h-4"}),"Export CSV"]})]})]}),e.jsxs("div",{className:"card",children:[e.jsx("h2",{className:"text-xl font-semibold mb-3",children:"Customers CSV"}),e.jsxs("div",{className:"flex flex-wrap gap-2",children:[e.jsxs("button",{className:"btn-secondary flex items-center gap-2",onClick:()=>h("customers"),children:[e.jsx(f,{className:"w-4 h-4"}),"Download Template"]}),e.jsxs("label",{className:"btn-primary flex items-center gap-2 cursor-pointer",children:[e.jsx(b,{className:"w-4 h-4"}),"Upload CSV",e.jsx("input",{type:"file",accept:".csv,text/csv",className:"hidden",disabled:o,onChange:s=>p("customers",s.target.files?.[0])})]}),e.jsxs("button",{className:"btn-secondary flex items-center gap-2",onClick:()=>j("customers"),children:[e.jsx(N,{className:"w-4 h-4"}),"Export CSV"]})]})]})]}),e.jsxs("div",{className:"card",children:[e.jsx("h2",{className:"text-xl font-semibold mb-3",children:"Recent Imports"}),e.jsx("div",{className:"overflow-x-auto",children:e.jsxs("table",{className:"w-full text-sm",children:[e.jsx("thead",{children:e.jsxs("tr",{className:"border-b border-gray-200",children:[e.jsx("th",{className:"text-left p-3 font-semibold",children:"Type"}),e.jsx("th",{className:"text-left p-3 font-semibold",children:"File"}),e.jsx("th",{className:"text-right p-3 font-semibold",children:"Total"}),e.jsx("th",{className:"text-right p-3 font-semibold",children:"Success"}),e.jsx("th",{className:"text-right p-3 font-semibold",children:"Failed"}),e.jsx("th",{className:"text-left p-3 font-semibold",children:"When"})]})}),e.jsx("tbody",{children:d.length===0?e.jsx("tr",{children:e.jsx("td",{colSpan:"6",className:"p-8 text-center text-gray-500",children:"No import history yet."})}):d.map(s=>e.jsxs("tr",{className:"border-b border-gray-100",children:[e.jsx("td",{className:"p-3",children:s.type}),e.jsx("td",{className:"p-3",children:s.filename}),e.jsx("td",{className:"p-3 text-right",children:s.recordsTotal}),e.jsx("td",{className:"p-3 text-right text-green-700",children:s.recordsSuccess}),e.jsx("td",{className:"p-3 text-right text-red-700",children:s.recordsFailed}),e.jsx("td",{className:"p-3",children:new Date(s.createdAt).toLocaleStr
1ing()})]},s.id))})]})})]})]})}export{U as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.