1import{u as oe,P as ie,e as ce,r as u,b as de,j as e,B as f,o as W,C as x,K as p,N as j,O as he,c as y,S as K,I as h,L as O,X as Z,J as v,s as F,i as me}from"./index-PtGACUVf.js";import{u as ge}from"./useMutation-B6B2ZZTv.js";import{u as ue,F as xe,a as l,b as o,c as i,d as c,e as d,t as pe}from"./form-6nOHp15Z.js";import{I as P}from"./ImageUpload-BRjL-9Qb.js";import{g as je}from"./slug-utils-CW2Vmkgw.js";import{T as ye}from"./textarea-B113Ozlu.js";import{C as ve}from"./checkbox-Be-GC6mS.js";import{S as T,a as G,b as $,c as B,d as S}from"./select-BwSQ4ldD.js";import{c as fe,m as we}from"./categoryMappings-Bsfax6Lj.js";import{A as be}from"./arrow-left-QiRMPYSA.js";import{I as _e}from"./image-D7k63tZZ.js";import{o as Ne,s as m,l as J,b as Ce,n as A}from"./types-iW-1e-aB.js";import"./label-BGYTmHnu.js";import"./tabs-DRyusGut.js";import"./progress-OepA4vif.js";import"./upload-CQnoTNQE.js";import"./link-BKMUFJ4O.js";import"./index-BdQq_4o_.js";import"./chevron-up-BZ1Up2zp.js";const Se=["Restaurants","Lodging","Activities","Attractions","Shopping","Services","Real Estate","Technology","Nightlife","Events"];function ke(k){if(!k)return"";for(const[_]of Object.entries(fe))if(we(k,_))return _;return""}const Le=Ne({name:m().min(1,"Business name is required"),description:m().optional(),address:m().optional(),address2:m().optional(),city:m().optional(),state:m().optional(),zip_code:m().optional(),phone:m().optional(),email:m().email("Invalid email").optional().or(J("")),website:m().url("Invalid URL").optional().or(J("")),google_category:m().optional(),primary_category:m().optional(),city_id:m().min(1,"Site/Location is required"),approved_status:m().optional(),is_featured:Ce().optional(),google_place_id:m().optional(),rating:A().optional().nullable(),review_count:A().optional().nullable(),latitude:A().optional().nullable(),longitude:A().optional().nullable()});function Ze(){const k=oe(),_=ie(),[X]=ce(),Y=X.get("city_id")||"",[U,D]=u.useState(""),[q,Q]=u.useState(!1),[H,L]=u.useState([]),[ee,Ve]=u.useState([]),[se,te]=u.useState(""),[V,I]=u.useState(""),[N,R]=u.useState(""),[ae,Fe]=u.useState("physical"),{data:w}=de({queryKey:["cities-for-new-listing"],queryFn:async()=>{const{data:s,error:a}=await F.from("cities").select("id, name, state, domain").eq("is_active",!0).order("domain");if(a)throw a;return s}}),t=ue({resolver:pe(Le),defaultValues:{name:"",description:"",address:"",address2:"",city:"",state:"",zip_code:"",phone:"",email:"",website:"",google_category:"",primary_category:"",city_id:Y,approved_status:"approved",is_featured:!1,google_place_id:"",rating:null,review_count:null,latitude:null,longitude:null}}),z=t.watch("city_id");u.useEffect(()=>{if(z&&w){const s=w.find(a=>a.id===z);s&&(t.setValue("city",s.name),t.setValue("state",s.state||""))}},[z,w,t]);const M=async()=>{var a;const s=U.trim()||t.getValues("name");if(!s){v.error("Enter a business name to search");return}Q(!0);try{const n=t.getValues("city"),g=t.getValues("state");console.log(`[AdminListingNew] Searching for: "${s}" in ${n||"any"}, ${g||"any"}`);const{data:r,error:b}=await F.functions.invoke("search-business",{body:{businessName:s,city:n,state:g}});if(b)throw b;if(console.log("[AdminListingNew] Search response:",{searchQuery:r.searchQuery,knowledge_graph:r.knowledge_graph,resultsCount:((a=r.results)==null?void 0:a.length)||0,totalFound:r.totalFound}),r.knowledge_graph&&r.knowledge_graph.title){const C={place_id:r.knowledge_graph.place_id||"",title:r.knowledge_graph.title,type:r.knowledge_graph.type||"",types:[],address:r.knowledge_graph.address||"",phone:r.knowledge_graph.phone||"",website:r.knowledge_graph.website||"",rating:r.knowledge_graph.rating||0,reviews:r.knowledge_graph.review_count||0,latitude:r.knowledge_graph.latitude||0,longitude:r.knowledge_graph.longitude||0,thumbnail:r.knowledge_graph.image||"",description:r.knowledge_graph.description||"",hours:null,open_state:"",service_options:null};L([C]),v.success("Found Google Business Profile!")}else if(r.results&&r.results.length>0)L(r.results),v.success(`Found ${r.results.length} result(s)`);else{const C=r.searchQuery||`${s} ${n||""} ${g||""}`.trim();v.info(`No Google Business Profile found for: "${C}". Try the exact business name.`),L([])}}catch(n){console.error("[AdminListingNew] Search error:",n),v.error("Search failed: "+(n.message||"Unknown error"))}finally{Q(!1)}},re=s=>{if(!s)return{street:"",city:"",state:"",zip:""};const a=s.split(",").map(n=>n.trim());if(a.length>=3){const n=a[0],g=a[1],r=a[2].split(" "),b=r[0]||"",C=r[1]||"";return{street:n,city:g,state:b,zip:C}}else if(a.length===2){const n=a[0],g=a[1].split(" ");return{street:n,city:"",state:g[0]||"",zip:g[1]||""}}return{street:s,city:"",state:"",zip:""}},ne=s=>{const a=re(s.address);t.setValue("name",s.title),t.setValue("google_category",s.type),t.setValue("google_place_id",s.place_id),t.setValue("phone",s.phone||""),t.setValue("website",s.website||""),t.setValue("description",s.description||""),t.setValue("rating",s.rating||null),t.setValue("review_count",s.reviews||null),t.setValue("latitude",s.latitude||null),t.setValue("longitude",s.longitude||null),t.setValue("address",a.street),t.getValues("city")||t.setValue("city",a.city),t.getValues("state")||t.setValue("state",a.state),t.setValue("zip_code",a.zip||t.getValues("zip_code"));const n=ke(s.type);n&&(t.setValue("primary_category",n),te(me(n)||"")),L([]),D(""),v.success("Form populated with Google data")},E=ge({mutationFn:async s=>{let a=je(s.name);const{data:n}=await F.from("businesses").select("slug").ilike("slug",`${a}%`);n&&n.length>0&&(a=`${a}-${n.length+1}`);const g={name:s.name,slug:a,description:s.description||null,address:s.address||null,address2:s.address2||null,city:s.city||null,state:s.state||null,zip_code:s.zip_code||null,phone:s.phone||null,email:s.email||null,website:s.website||null,google_category:s.google_category||null,primary_category:s.primary_category||null,category:se||s.google_category||null,categories:ee,city_id:s.city_id,approved_status:s.approved_status||"approved",is_featured:s.is_featured||!1,google_place_id:s.google_place_id||null,rating:s.rating||null,review_count:s.review_count||null,latitude:s.latitude||null,longitude:s.longitude||null,logo_url:V||null,photos:N?[{url:N,caption:""}]:[],location_type:ae},{error:r,data:b}=await F.from("businesses").insert(g).select().single();if(r)throw r;return b},onSuccess:s=>
1{v.success("Listing created successfully"),_.invalidateQueries({queryKey:["admin-listings"]}),_.invalidateQueries({queryKey:["admin-pending-count"]}),k("/admin/listings")},onError:s=>{v.error("Failed to create listing: "+s.message)}}),le=s=>{E.mutate(s)};return e.jsxs("div",{className:"space-y-6",children:[e.jsxs("div",{className:"flex items-center gap-4",children:[e.jsx(f,{variant:"ghost",size:"icon",asChild:!0,children:e.jsx(W,{to:"/admin/listings",children:e.jsx(be,{className:"h-4 w-4"})})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Add New Listing"}),e.jsx("p",{className:"text-muted-foreground",children:"Create a new business listing"})]})]}),e.jsx(xe,{...t,children:e.jsxs("form",{onSubmit:t.handleSubmit(le),className:"space-y-6",children:[e.jsxs(x,{className:"border-2 border-primary/30",children:[e.jsx(p,{children:e.jsxs(j,{className:"flex items-center gap-2",children:[e.jsx(he,{className:"h-5 w-5"}),"Site / Location"]})}),e.jsx(y,{children:e.jsx(l,{control:t.control,name:"city_id",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Select Site *"}),e.jsxs(T,{onValueChange:s.onChange,value:s.value,children:[e.jsx(c,{children:e.jsx(G,{children:e.jsx($,{placeholder:"Choose a site for this listing..."})})}),e.jsx(B,{children:w==null?void 0:w.map(a=>e.jsxs(S,{value:a.id,children:[a.name,", ",a.state," (",a.domain,")"]},a.id))})]}),e.jsx(d,{}),e.jsx("p",{className:"text-sm text-muted-foreground mt-1",children:"This determines which site the listing will appear on and auto-fills city/state."})]})})})]}),e.jsxs(x,{className:"border-primary/20 bg-primary/5",children:[e.jsx(p,{children:e.jsxs(j,{className:"flex items-center gap-2",children:[e.jsx(K,{className:"h-5 w-5"}),"Lookup Business from Google"]})}),e.jsxs(y,{className:"space-y-4",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx(h,{placeholder:"Enter business name to search...",value:U,onChange:s=>D(s.target.value),onKeyDown:s=>s.key==="Enter"&&(s.preventDefault(),M()),className:"flex-1"}),e.jsx(f,{type:"button",onClick:M,disabled:q,children:q?e.jsx(O,{className:"h-4 w-4 animate-spin"}):e.jsxs(e.Fragment,{children:[e.jsx(K,{className:"h-4 w-4 mr-2"}),"Search"]})})]}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Select a site first, then search to use city/state for better results."}),H.length>0&&e.jsx("div",{className:"space-y-2 max-h-60 overflow-y-auto border rounded-lg",children:H.map(s=>e.jsxs("div",{className:"p-3 border-b last:border-b-0 cursor-pointer hover:bg-background transition-colors",onClick:()=>ne(s),children:[e.jsx("p",{className:"font-medium",children:s.title}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[s.type," · ",s.address]}),s.rating>0&&e.jsxs("p",{className:"text-sm",children:["â ",s.rating," (",s.reviews," reviews)"]}),e.jsx(f,{type:"button",size:"sm",className:"mt-2",children:"Import this data"})]},s.place_id||s.title))})]})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsxs(j,{className:"flex items-center gap-2",children:[e.jsx(_e,{className:"h-5 w-5"}),"Logo & Cover Photo"]})}),e.jsx(y,{children:e.jsxs("div",{className:"grid gap-6 sm:grid-cols-2",children:[e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium",children:"Cover Photo"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Wide banner image for listing header (recommended 1920Ã600)"})]}),N?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"relative rounded-lg overflow-hidden border bg-muted",children:e.jsx("img",{src:N,alt:"Cover photo",className:"w-full h-[150px] object-cover",onError:s=>{s.target.src="/placeholder.svg"}})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{value:"",onChange:R,bucket:"business_assets",folder:"covers",placeholder:"Replace cover photo...",maxWidth:1920,maxHeight:800,showPreview:!1,className:"flex-1"}),e.jsx(f,{type:"button",variant:"outline",size:"icon",onClick:()=>R(""),title:"Remove cover photo",className:"text-destructive hover:text-destructive",children:e.jsx(Z,{className:"h-4 w-4"})})]})]}):e.jsx(P,{value:N,onChange:R,bucket:"business_assets",folder:"covers",placeholder:"Cover photo URL...",maxWidth:1920,maxHeight:800,showPreview:!1})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{children:[e.jsx("label",{className:"text-sm font-medium",children:"Logo"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Business logo shown on listing cards (recommended 400Ã400)"})]}),V?e.jsxs("div",{className:"space-y-3",children:[e.jsx("div",{className:"relative w-24 h-24 rounded-lg overflow-hidden border bg-muted",children:e.jsx("img",{src:V,alt:"Logo",className:"w-full h-full object-contain",onError:s=>{s.target.src="/placeholder.svg"}})}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(P,{value:"",onChange:I,bucket:"business_assets",folder:"logos",placeholder:"Replace logo...",maxWidth:800,maxHeight:800,showPreview:!1,className:"flex-1"}),e.jsx(f,{type:"button",variant:"outline",size:"icon",onClick:()=>I(""),title:"Remove logo",className:"text-destructive hover:text-destructive",children:e.jsx(Z,{className:"h-4 w-4"})})]})]}):e.jsx(P,{value:V,onChange:I,bucket:"business_assets",folder:"logos",placeholder:"Logo URL...",maxWidth:800,maxHeight:800,showPreview:!1})]})]})})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsx(j,{children:"Basic Information"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsx(l,{control:t.control,name:"name",render:({field:s})=>
1e.jsxs(o,{children:[e.jsx(i,{children:"Business Name *"}),e.jsx(c,{children:e.jsx(h,{placeholder:"Enter business name",...s})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"description",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Description"}),e.jsx(c,{children:e.jsx(ye,{placeholder:"Enter business description",rows:4,...s})}),e.jsx(d,{})]})}),e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2",children:[e.jsx(l,{control:t.control,name:"google_category",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Google Category"}),e.jsx(c,{children:e.jsx(h,{placeholder:"Auto-filled from Google lookup",...s,className:"bg-muted",readOnly:!0})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"primary_category",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Display Category"}),e.jsxs(T,{onValueChange:s.onChange,value:s.value,children:[e.jsx(c,{children:e.jsx(G,{children:e.jsx($,{placeholder:"Select category"})})}),e.jsx(B,{children:Se.map(a=>e.jsx(S,{value:a,children:a},a))})]}),e.jsx(d,{})]})})]})]})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsx(j,{children:"Address"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsx(l,{control:t.control,name:"address",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Street Address"}),e.jsx(c,{children:e.jsx(h,{placeholder:"123 Main Street",...s})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"address2",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Address Line 2"}),e.jsx(c,{children:e.jsx(h,{placeholder:"Suite, Unit, Building (optional)",...s})}),e.jsx(d,{})]})}),e.jsxs("div",{className:"grid gap-4 sm:grid-cols-3",children:[e.jsx(l,{control:t.control,name:"city",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"City"}),e.jsx(c,{children:e.jsx(h,{placeholder:"City",...s})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"state",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"State"}),e.jsx(c,{children:e.jsx(h,{placeholder:"State",...s})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"zip_code",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"ZIP Code"}),e.jsx(c,{children:e.jsx(h,{placeholder:"12345",...s})}),e.jsx(d,{})]})})]}),e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2",children:[e.jsx(l,{control:t.control,name:"latitude",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Latitude"}),e.jsx(c,{children:e.jsx(h,{type:"number",step:"any",placeholder:"e.g. 42.5847",...s,value:s.value??"",onChange:a=>s.onChange(a.target.value?parseFloat(a.target.value):null)})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"longitude",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Longitude"}),e.jsx(c,{children:e.jsx(h,{type:"number",step:"any",placeholder:"e.g. -88.4326",...s,value:s.value??"",onChange:a=>s.onChange(a.target.value?parseFloat(a.target.value):null)})}),e.jsx(d,{})]})})]})]})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsx(j,{children:"Contact Information"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2",children:[e.jsx(l,{control:t.control,name:"phone",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Phone"}),e.jsx(c,{children:e.jsx(h,{placeholder:"(555) 123-4567",...s})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"email",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Email"}),e.jsx(c,{children:e.jsx(h,{type:"email",placeholder:"[email protected]",...s})}),e.jsx(d,{})]})})]}),e.jsx(l,{control:t.control,name:"website",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Website"}),e.jsx(c,{children:e.jsx(h,{type:"url",placeholder:"https://www.example.com",...s})}),e.jsx(d,{})]})})]})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsx(j,{children:"Google Data"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsx(l,{control:t.control,name:"google_place_id",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Google Place ID"}),e.jsx(c,{children:e.jsx(h,{placeholder:"Auto-filled from Google lookup",...s,className:"bg-muted",readOnly:!0})}),e.jsx(d,{})]})}),e.jsxs("div",{className:"grid gap-4 sm:grid-cols-2",children:[e.jsx(l,{control:t.control,name:"rating",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Rating"}),e.jsx(c,{children:e.jsx(h,{type:"number",step:"0.1",min:"0",max:"5",placeholder:"e.g. 4.5",...s,value:s.value??"",onChange:a=>s.onChange(a.target.value?parseFloat(a.target.value):null)})}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"review_count",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Review Count"}),e.jsx(c,{children:e.jsx(h,{type:"number",min:"0",placeholder:"e.g. 150",...s,value:s.value??"",onChange:a=>s.onChange(a.target.value?parseInt(a.target.value):null)})}),e.jsx(d,{})]})})]})]})]}),e.jsxs(x,{children:[e.jsx(p,{children:e.jsx(j,{children:"Admin Settings"})}),e.jsxs(y,{className:"space-y-4",children:[e.jsx(l,{control:t.control,name:"approved_status",render:({field:s})=>e.jsxs(o,{children:[e.jsx(i,{children:"Approval Status"}),e.jsxs(T,{onValueChange:s.onChange,value:s.value,children:[e.jsx(c,{children:e.jsx(G,{children:e.jsx($,{placeholder:"Select status"})})}),e.jsxs(B,{children:[e.jsx(S,{value:"approved",children:"Approved"}),e.jsx(S,{value:"pending",children:"Pending"}),e.jsx(S,{value:"rejected",children:"Rejected"})]})]}),e.jsx(d,{})]})}),e.jsx(l,{control:t.control,name:"is_featured",render:({field:s})=>e.jsxs(o,{className:"flex flex-row items-start space-x-3 space-y-0",children:[e.jsx(c,{children:e.jsx(ve,{checked:s.value,onCheckedChange:s.onChange})}),e.jsxs("div",{className:"space-y-1 leading-none",children:[e.jsx(i,{children:"Featured on Homepage"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Show this listing in the homepage carousel"})]})]})})]})]}),e.jsxs("div",{className:"flex justify-end gap-4",children:[e.jsx(f,{type:"button",variant:"outline",asChild:!0,children:e.jsx(W,{to:"/admin/listings",children:"Cancel"})}),e.jsx(f,{type:"submit",disabled:E.isPending,children:E.isPending?e.jsxs(e.Fragment,{children:[e.jsx(O,{className:"mr-2 h-4 w-4 animate-spin"}),"Creating..."]}):"Create Listing"})]})]})})]})}export{Ze 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.