1import{u as k,r as u,b as E,j as e,C as P,K as A,N as B,bJ as D,c as L,B as f,a2 as U,J as p,s as b}from"./index-PtGACUVf.js";import{u as H}from"./useMutation-B6B2ZZTv.js";import{S as $,a as q,b as J,c as K,d as M}from"./select-BwSQ4ldD.js";import{T as Q,a as W,b as I,c as o,d as G,e as m}from"./table-BzCEyvEt.js";import{U as O}from"./upload-CQnoTNQE.js";import{F as X}from"./file-spreadsheet-BSwbbLS_.js";import"./index-BdQq_4o_.js";import"./chevron-up-BZ1Up2zp.js";function le(){const T=k(),y=u.useRef(null),[h,_]=u.useState(""),[r,C]=u.useState([]),[N,v]=u.useState(""),[S,w]=u.useState(null),{data:x}=E({queryKey:["admin-cities-import"],queryFn:async()=>{const{data:s,error:t}=await b.from("cities").select("id, name, domain").order("domain");if(t)throw t;return s}}),g=H({mutationFn:async s=>{const{data:t}=await b.auth.getUser();if(!t.user)throw new Error("Not authenticated");const c=s.map(i=>({name:i.business_name,description:i.description||null,address:i.address||null,city:i.city||null,state:i.state||null,zip_code:i.zip||null,phone:i.phone||null,email:i.email||null,website:i.website||null,category:i.category||null,city_id:h,user_id:t.user.id,is_claimed:!1,approved_status:"approved"})),{data:l,error:n}=await b.from("businesses").insert(c).select();if(n)throw n;return l},onSuccess:s=>{w(s.length),C([]),v(""),p.success(`Successfully imported ${s.length} listings`)},onError:s=>{p.error("Import failed: "+s.message)}}),F=s=>{const t=s.split(` 2`).filter(n=>n.trim());if(t.length<2)return[];const c=t[0].split(",").map(n=>n.trim().toLowerCase().replace(/['"]/g,"")),l=[];for(let n=1;n<t.length;n++){const i=t[n].split(",").map(d=>d.trim().replace(/^["']|["']$/g,"")),a={};c.forEach((d,R)=>{a[d]=i[R]||""}),l.push({business_name:a.business_name||a.name||"",description:a.description||"",address:a.address||"",city:a.city||"",state:a.state||"",zip:a.zip||a.zip_code||"",phone:a.phone||"",email:a.email||"",website:a.website||"",category:a.category||""})}return l},V=s=>{var l;const t=(l=s.target.files)==null?void 0:l[0];if(!t)return;v(t.name),w(null);const c=new FileReader;c.onload=n=>{var d;const i=(d=n.target)==null?void 0:d.result,a=F(i);C(a)},c.readAsText(t)},z=()=>{if(!h){p.error("Please select a website to assign listings to");return}if(r.length===0){p.error("No data to import");return}g.mutate(r)},j=r.slice(0,5);return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Import Listings"}),e.jsx("p",{className:"text-muted-foreground",children:"Import business listings from a CSV file"})]}),e.jsxs(P,{children:[e.jsxs(A,{children:[e.jsx(B,{children:"CSV Import"}),e.jsx(D,{children:"Upload a CSV file with columns: business_name, description, address, city, state, zip, phone, email, website, category"})]}),e.jsxs(L,{className:"space-y-6",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"Assign to Website"}),e.jsxs($,{value:h,onValueChange:_,children:[e.jsx(q,{className:"w-full sm:w-[300px]",children:e.jsx(J,{placeholder:"Select a website..."})}),e.jsx(K,{children:x==null?void 0:x.map(s=>e.jsx(M,{value:s.id,children:s.domain},s.id))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx("label",{className:"text-sm font-medium",children:"CSV File"}),e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx("input",{ref:y,type:"file",accept:".csv",onChange:V,className:"hidden"}),e.jsxs(f,{variant:"outline",onClick:()=>{var s;return(s=y.current)==null?void 0:s.click()},className:"gap-2",children:[e.jsx(O,{className:"h-4 w-4"}),"Choose File"]}),N&&e.jsxs("div",{className:"flex items-center gap-2 text-sm text-muted-foreground",children:[e.jsx(X,{className:"h-4 w-4"}),N]})]})]}),S!==null&&e.jsxs("div",{className:"flex items-center gap-2 p-4 bg-green-500/10 text-green-700 rounded-lg",children:[e.jsx(U,{className:"h-5 w-5"}),"Successfully imported ",S," listings!"]}),j.length>0&&e.jsxs("div",{className:"space-y-2",children:[e.jsxs("label",{className:"text-sm font-medium",children:["Preview (showing first ",j.length," of ",r.length," rows)"]}),e.jsx("div",{className:"border rounded-lg overflow-x-auto",children:e.jsxs(Q,{children:[e.jsx(W,{children:e.jsxs(I,{children:[e.jsx(o,{children:"Business N
2ame"}),e.jsx(o,{children:"Address"}),e.jsx(o,{children:"City"}),e.jsx(o,{children:"State"}),e.jsx(o,{children:"Phone"}),e.jsx(o,{children:"Category"})]})}),e.jsx(G,{children:j.map((s,t)=>e.jsxs(I,{children:[e.jsx(m,{className:"font-medium",children:s.business_name}),e.jsx(m,{children:s.address||"-"}),e.jsx(m,{children:s.city||"-"}),e.jsx(m,{children:s.state||"-"}),e.jsx(m,{children:s.phone||"-"}),e.jsx(m,{children:s.category||"-"})]},t))})]})})]}),e.jsxs("div",{className:"flex gap-4",children:[e.jsx(f,{onClick:z,disabled:r.length===0||!h||g.isPending,children:g.isPending?"Importing...":`Import ${r.length} Listings`}),e.jsx(f,{variant:"outline",onClick:()=>T("/admin/listings"),children:"Cancel"})]})]})]})]})}export{le 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.