1import{j as e}from"./vendor-tanstack-CeuMW_UU.js";import{r,R as ye,i as be,d as Ce}from"./vendor-react-DEnzFQ2s.js";import{u as z,t as L,v as E,w as P,x as M,L as R,a4 as U,B as c,s as l,a as w,E as y,F as b,b as C,X as Se,H as _e,J as ke,K as W,l as De,A as Le,$ as Z,a0 as ee,a1 as se,a2 as o,D as te,N as ae,ah as ne,I as ie}from"./index-f3p8tUtI.js";import{V as re}from"./VehiclePhotoGallery-BEO7_bnX.js";import{V as Ee,E as Pe}from"./EditVehicleDialog-T3lflray.js";import{M as Me,r as Ue,g as Ve}from"./MentionTextarea-CrGtOq9l.js";import{S as Ae}from"./SearchableSelect-CTk-Gqyd.js";import{S as $}from"./sticky-note-CAcp91s4.js";import{d as Ie}from"./vendor-date-D27i00CA.js";import{U as Fe}from"./user-check-BQ16hOun.js";import{E as le}from"./ellipsis-vertical-fs2KrMwK.js";import{P as oe}from"./pencil-22bIV6MM.js";import{C as ce}from"./camera-CDpfKkN1.js";import{U as de}from"./user-plus-Blr2Nw5M.js";import{C as me}from"./calendar-days-YXUWxhhD.js";import{C as xe}from"./circle-check-big-DruVdTar.js";import{F as Te}from"./file-text-Bd15-eWh.js";import{M as qe}from"./index-DD3Yz5-G.js";import"./vendor-supabase-gM5zGnqC.js";import"./image-off-DYNuLhN1.js";import"./chevron-left-B3fQy1XN.js";import"./useDropZone-DnVumFpJ.js";import"./upload-BmLxZMhD.js";import"./textarea-BTiMUeQ3.js";import"./command-DcGFIEtt.js";import"./search-wPBvxK1v.js";function Be({open:t,onOpenChange:a,vehicleId:i,onSaved:s}){const[h,S]=r.useState(""),[p,j]=r.useState(!1),{toast:d}=z(),_=async()=>{if(!h.trim())return;j(!0);const{data:{session:m}}=await l.auth.getSession();if(!m){d({title:"Error",description:"You must be logged in",variant:"destructive"}),j(!1);return}const{error:v}=await l.from("vehicle_notes").insert({vehicle_id:i,user_id:m.user.id,content:h.trim()});if(v)d({title:"Error",description:v.message,variant:"destructive"});else{if(h.includes("@[")){const f={content:h.trim(),resource_type:"vehicle",resource_id:i};l.functions.invoke("create-mention-notifications",{body:f}).then(({error:k})=>{k&&(console.error("Mention notification failed, retrying:",k),l.functions.invoke("create-mention-notifications",{body:f}).then(({error:x})=>{x&&console.error("Mention notification retry failed:",x)}))})}d({title:"Note added"}),S(""),s(),a(!1)}j(!1)};return e.jsx(L,{open:t,onOpenChange:a,children:e.jsxs(E,{className:"sm:max-w-[425px] shadow-lg",children:[e.jsx(P,{children:e.jsx(M,{children:"Add Vehicle Note"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(R,{htmlFor:"vehicleNote",children:"Note"}),e.jsx(Me,{value:h,onChange:S,placeholder:"Enter your note... Use @ to mention team members"})]}),e.jsxs(U,{children:[e.jsx(c,{variant:"outline",onClick:()=>a(!1),children:"Cancel"}),e.jsx(c,{onClick:_,disabled:p||!h.trim(),children:p?"Saving...":"Save Note"})]})]})})}function Re({open:t,onOpenChange:a,vehicleId:i,onSaved:s}){const[h,S]=r.useState([]),[p,j]=r.useState(""),[d,_]=r.useState(!1),{toast:m}=z();r.useEffect(()=>{if(!t)return;(async()=>{const{data:k}=await l.from("subscription_leads").select("id, first_name, last_name, email").order("first_name",{ascending:!0}).limit(500);S((k||[]).map(x=>({value:x.id,label:`${x.first_name||""} ${x.last_name||""}`.trim()||x.email||"Unknown"})))})()},[t]);const v=async()=>{if(!p)return;_(!0);const{error:f}=await l.from("inventory").update({assigned_lead_id:p}).eq("id",i);f?m({title:"Error",description:f.message,variant:"destructive"}):(m({title:"Lead assigned"}),j(""),s(),a(!1)),_(!1)};return e.jsx(L,{open:t,onOpenChange:a,children:e.jsxs(E,{className:"sm:max-w-[425px] shadow-lg",children:[e.jsx(P,{children:e.jsx(M,{children:"Assign to Lead"})}),e.jsx(Ae,{options:h,value:p,onValueChange:j,placeholder:"Select a lead...",searchPlaceholder:"Search leads...",emptyMessage:"No leads found."}),e.jsxs(U,{children:[e.jsx(c,{variant:"outline",onClick:()=>a(!1),children:"Cancel"}),e.jsx(c,{onClick:v,disabled:d||!p,children:d?"Assigning...":"Assign"})]})]})})}const $e=t=>t?t.replace(/_/g," ").replace(/\b\w/g,a=>a.toUpperCase()):null,he=ye.memo(({note:t})=>{const a=t.user_profiles,i=(a==null?void 0:a.full_name)||[a==null?void 0:a.first_name,a==null?void 0:a.last_name].filter(
1Boolean).join(" ")||(a==null?void 0:a.email)||"Unknown",s=$e(a==null?void 0:a.role);return e.jsxs("div",{className:"border-b border-border pb-3 last:border-0 last:pb-0",children:[e.jsx("div",{className:"text-sm text-foreground whitespace-pre-wrap",children:Ue(t.content)}),e.jsxs("div",{className:"flex items-center gap-2 mt-1",children:[e.jsx("span",{className:"text-xs text-muted-foreground font-medium",children:i}),s&&e.jsxs(e.Fragment,{children:[e.jsx("span",{className:"text-xs text-muted-foreground",children:"·"}),e.jsx("span",{className:`${Ve((a==null?void 0:a.role)||"")} inline-flex items-center px-1.5 py-0.5 rounded text-[10px] font-medium border`,children:s})]}),e.jsx("span",{className:"text-xs text-muted-foreground",children:"·"}),e.jsx("span",{className:"text-xs text-muted-foreground",children:Ie(new Date(t.created_at),"MMM d, yyyy 'at' h:mm a")})]})]})});he.displayName="NoteItem";function ze({notes:t}){return t.length?e.jsxs(w,{className:"mt-6",children:[e.jsx(y,{children:e.jsxs(b,{className:"text-lg flex items-center gap-2",children:[e.jsx($,{className:"w-5 h-5"})," Notes"]})}),e.jsx(C,{className:"space-y-4",children:t.map(a=>e.jsx(he,{note:a},a.id))})]}):null}function He({lead:t,onUnassign:a}){const i=[t.first_name,t.last_name].filter(Boolean).join(" ")||"Unknown";return e.jsxs(w,{children:[e.jsxs(y,{className:"flex flex-row items-center justify-between pb-2",children:[e.jsxs(b,{className:"text-lg flex items-center gap-2",children:[e.jsx(Fe,{className:"w-5 h-5"})," Assigned Lead"]}),e.jsx(c,{variant:"ghost",size:"icon",onClick:a,className:"h-7 w-7",children:e.jsx(Se,{className:"w-4 h-4"})})]}),e.jsxs(C,{className:"space-y-1",children:[e.jsx("p",{className:"text-sm font-medium",children:i}),t.phone&&e.jsx("p",{className:"text-xs text-muted-foreground",children:t.phone}),t.email&&e.jsx("p",{className:"text-xs text-muted-foreground",children:t.email})]})]})}function fs(){var X,Q;const{id:t}=be(),a=Ce(),{toast:i}=z(),[s,h]=r.useState(null),[S,p]=r.useState(!0),[j,d]=r.useState(!1),[_,m]=r.useState(!1),[v,f]=r.useState(""),[k,x]=r.useState(!1),[ue,V]=r.useState(!1),[pe,D]=r.useState(!1),[je,A]=r.useState(!1),[H,O]=r.useState(""),[fe,ge]=r.useState([]),[K,I]=r.useState(null),{hasRole:F}=_e(),T=ke(),Y=F("admin")||F("general_manager")||F("sales_manager"),u=async()=>{var N;if(!t)return;const{data:n,error:g}=await l.from("inventory").select("*").eq("id",t).single();if(g)i({title:"Error",description:"Vehicle not found",variant:"destructive"}),a("/inventory");else if(h(n),f(((N=n.cost)==null?void 0:N.toString())||""),O(n.date_in_stock||new Date(n.created_at).toISOString().split("T")[0]),n.assigned_lead_id){const{data:q}=await l.from("subscription_leads").select("id, first_name, last_name, phone, email").eq("id",n.assigned_lead_id).single();I(q)}else I(null);p(!1)},G=async()=>{if(!t)return;const{data:n}=await l.from("vehicle_notes").select("*, user_profiles(first_name, last_name, full_name, email, role)").eq("vehicle_id",t).order("created_at",{ascending:!1});ge(n||[])};r.useEffect(()=>{u(),G()},[t]);const Ne=async()=>{if(!t)return;const{error:n}=await l.from("inventory").update({cost:parseFloat(v)}).eq("id",t);n?i({title:"Error",description:n.message,variant:"destructive"}):(i({title:"Success",description:"Cost updated"}),u(),m(!1))},J=async()=>{if(!t)return;const{error:n}=await l.from("inventory").update({status:"sold"}).eq("id",t);n?i({title:"Error",description:n.message,variant:"destructive"}):(i({title:"Success",description:"Vehicle marked as sold"}),u())},ve=async()=>{if(!t)return;const{error:n}=await l.from("inventory").update({assigned_lead_id:null}).eq("id",t);n?i({title:"Error",description:n.message,variant:"destructive"}):(i({title:"Lead unassigned"}),I(null),u())};if(S)return e.jsx(W,{children:e.jsx("div",{className:"flex items-center justify-center min-h-[60vh]",children:e.jsx(De,{className:"w-8 h-8 animate-spin text-primary"})})});if(!s)return null;const we=[{label:"Year",value:s.year},{label:"Make",value:s.make},{label:"Model",value:s.model},{label:"Trim",value:s.trim}
1,{label:"Mileage",value:s.mileage?`${s.mileage.toLocaleString()} KM`:null},{label:"Exterior Color",value:s.exterior_color},{label:"Interior Color",value:s.interior_color},{label:"Body Style",value:s.body_style},{label:"Engine",value:s.engine_type},{label:"Drivetrain",value:s.drivetrain},{label:"Transmission",value:s.transmission},{label:"Fuel Type",value:s.fuel_type}];return e.jsx(W,{children:e.jsxs("div",{className:"p-4 md:p-8 max-w-6xl mx-auto",children:[e.jsxs("div",{className:"mb-4 md:mb-6",children:[e.jsxs("div",{className:"flex items-center gap-2 md:gap-4",children:[e.jsx(c,{variant:"ghost",size:"icon",onClick:()=>a("/inventory"),children:e.jsx(Le,{className:"w-5 h-5"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("h1",{className:"text-xl md:text-3xl font-bold text-foreground truncate",children:[s.year," ",s.make," ",s.model," ",s.trim||""]}),e.jsxs("p",{className:"text-muted-foreground font-mono text-xs md:text-sm mt-0.5 truncate",children:["VIN: ",s.vin,s.stock_number&&e.jsxs(e.Fragment,{children:[" | Stock #: ",s.stock_number]})]})]}),!T&&e.jsxs(e.Fragment,{children:[e.jsxs(Z,{children:[e.jsx(ee,{asChild:!0,children:e.jsxs(c,{variant:"outline",size:"sm",children:["Actions ",e.jsx(le,{className:"w-4 h-4 ml-1"})]})}),e.jsxs(se,{align:"end",children:[e.jsxs(o,{onClick:()=>A(!0),children:[e.jsx(oe,{className:"w-4 h-4 mr-2"})," Edit Vehicle"]}),e.jsxs(o,{onClick:()=>m(!0),children:[e.jsx(te,{className:"w-4 h-4 mr-2"})," Update Cost"]}),e.jsxs(o,{onClick:()=>d(!0),children:[e.jsx(ce,{className:"w-4 h-4 mr-2"})," Upload Photos"]}),e.jsxs(o,{onClick:()=>x(!0),children:[e.jsx($,{className:"w-4 h-4 mr-2"})," Add Note"]}),e.jsxs(o,{onClick:()=>V(!0),children:[e.jsx(de,{className:"w-4 h-4 mr-2"})," Assign to Lead"]}),e.jsxs(o,{onClick:()=>D(!0),children:[e.jsx(me,{className:"w-4 h-4 mr-2"})," Edit Stock Date"]}),s.status!=="sold"&&e.jsxs(o,{onClick:J,children:[e.jsx(xe,{className:"w-4 h-4 mr-2"})," Mark as Sold"]})]})]}),e.jsx(ae,{variant:s.status==="available"?"default":"secondary",className:"text-sm px-3 py-1",children:s.status})]})]}),T&&e.jsxs("div",{className:"flex items-center justify-between mt-2 pl-10",children:[e.jsx(ae,{variant:s.status==="available"?"default":"secondary",className:"text-xs px-2 py-0.5",children:s.status}),e.jsxs(Z,{children:[e.jsx(ee,{asChild:!0,children:e.jsxs(c,{variant:"outline",size:"sm",className:"h-8 text-xs",children:["Actions ",e.jsx(le,{className:"w-3.5 h-3.5 ml-1"})]})}),e.jsxs(se,{align:"end",children:[e.jsxs(o,{onClick:()=>A(!0),children:[e.jsx(oe,{className:"w-4 h-4 mr-2"})," Edit Vehicle"]}),e.jsxs(o,{onClick:()=>m(!0),children:[e.jsx(te,{className:"w-4 h-4 mr-2"})," Update Cost"]}),e.jsxs(o,{onClick:()=>d(!0),children:[e.jsx(ce,{className:"w-4 h-4 mr-2"})," Upload Photos"]}),e.jsxs(o,{onClick:()=>x(!0),children:[e.jsx($,{className:"w-4 h-4 mr-2"})," Add Note"]}),e.jsxs(o,{onClick:()=>V(!0),children:[e.jsx(de,{className:"w-4 h-4 mr-2"})," Assign to Lead"]}),e.jsxs(o,{onClick:()=>D(!0),children:[e.jsx(me,{className:"w-4 h-4 mr-2"})," Edit Stock Date"]}),s.status!=="sold"&&e.jsxs(o,{onClick:J,children:[e.jsx(xe,{className:"w-4 h-4 mr-2"})," Mark as Sold"]})]})]})]})]}),e.jsxs("div",{className:"grid gap-4 md:gap-6 lg:grid-cols-3",children:[e.jsx("div",{className:"lg:col-span-2",children:T?e.jsx(re,{photos:s.photos||[],editable:Y,onDeletePhoto:async n=>{if(!t)return;const g=(s.photos||[]).filter((q,B)=>B!==n),{error:N}=await l.from("inventory").update({photos:g}).eq("id",t);N?i({title:"Error",description:N.message,variant:"destructive"}):(i({title:"Photo deleted"}),u())}}):e.jsxs(w,{children:[e.jsx(y,{className:"pb-3",children:e.jsx(b,{className:"text-lg",children:"Photos"})}),e.jsx(C,{children:e.jsx(re,{photos:s.photos||[],editable:Y,onDeletePhoto:async n=>{if(!t)return;const g=(s.photos||[]).filter((q,B)=>B!==n),{error:N}=await l.from("inventory").update({photos:g}).eq("id",t);N?i({title:"Error",description:N.message,variant:"destructive"}):(i({title:"Photo deleted"}),u())}})})]})}),e.jsxs("div",{className:"space-y-4 md:space-y-6",children:[e.jsxs(w,{children:[e.jsx(y,{className:"p-4 md:p-6 pb-2 md:pb-3",children:e.jsx(b,{className:"text-base md:text-lg",children:"Pricing"})}),e.jsxs(C,{className:"p-3 md:p-6 pt-0 space-y-1.5 md:space-y-3",children:[e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-xs md:text-sm text-muted-foreground",children:"Cost"}),e.jsxs("span",{className:"text-sm md:text-lg font-bold",children:["$",(X=s.cost)==null?void 0:X.toLocaleStr
1ing()]})]}),s.retail_price&&e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-xs md:text-sm text-muted-foreground",children:"Retail Price"}),e.jsxs("span",{className:"text-sm md:text-lg font-bold text-primary",children:["$",(Q=s.retail_price)==null?void 0:Q.toLocaleString()]})]}),s.retail_price&&s.cost&&e.jsxs(e.Fragment,{children:[e.jsx(ne,{}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-xs md:text-sm text-muted-foreground",children:"Potential Profit"}),e.jsxs("span",{className:"text-sm md:text-lg font-bold text-green-600",children:["$",(s.retail_price-s.cost).toLocaleString()]})]})]}),e.jsx(ne,{}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-xs md:text-sm text-muted-foreground",children:"Days in Stock"}),e.jsx("span",{className:"text-sm md:text-lg font-bold",children:Math.floor((Date.now()-new Date(s.date_in_stock||s.created_at).getTime())/864e5)})]}),e.jsxs("div",{className:"flex justify-between items-center",children:[e.jsx("span",{className:"text-xs md:text-sm text-muted-foreground",children:"In Stock Since"}),e.jsx("span",{className:"text-xs md:text-sm font-medium",children:new Date(s.date_in_stock||s.created_at).toLocaleDateString()})]})]})]}),e.jsxs(w,{children:[e.jsx(y,{className:"p-4 md:p-6 pb-2 md:pb-3",children:e.jsx(b,{className:"text-base md:text-lg",children:"Vehicle Specifications"})}),e.jsx(C,{className:"p-4 md:p-6 pt-0",children:e.jsx("div",{className:"grid grid-cols-2 gap-3 md:gap-4",children:we.map(n=>n.value?e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground",children:n.label}),e.jsx("p",{className:"text-xs md:text-sm font-medium",children:n.value})]},n.label):null)})})]}),K&&e.jsx(He,{lead:K,onUnassign:ve}),s.documents&&s.documents.length>0&&e.jsxs(w,{children:[e.jsx(y,{children:e.jsx(b,{className:"text-lg",children:"Documents"})}),e.jsx(C,{className:"space-y-2",children:s.documents.map((n,g)=>e.jsxs("a",{href:n,target:"_blank",rel:"noopener noreferrer",className:"flex items-center gap-2 text-sm text-primary hover:underline",children:[e.jsx(Te,{className:"w-4 h-4"}),"Document ",g+1]},g))})]})]})]}),s.description&&e.jsxs(w,{className:"mt-4 md:mt-6",children:[e.jsx(y,{className:"p-4 md:p-6 pb-2 md:pb-3",children:e.jsx(b,{className:"text-base md:text-lg",children:"Description"})}),e.jsx(C,{className:"p-4 md:p-6 pt-0",children:e.jsx("div",{className:"prose prose-sm dark:prose-invert max-w-none text-muted-foreground",children:e.jsx(qe,{children:s.description})})})]}),e.jsx(ze,{notes:fe}),j&&s&&e.jsx(L,{open:j,onOpenChange:d,children:e.jsxs(E,{className:"max-w-lg",children:[e.jsx(P,{children:e.jsx(M,{children:"Upload Photos"})}),e.jsx(Ee,{vehicleId:s.id,existingPhotos:s.photos||[],onPhotosChange:()=>{},onUploadComplete:()=>{u(),d(!1)}})]})}),e.jsx(L,{open:_,onOpenChange:m,children:e.jsxs(E,{children:[e.jsx(P,{children:e.jsx(M,{children:"Update Vehicle Cost"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(R,{children:"New Cost"}),e.jsx(ie,{type:"number",step:"0.01",value:v,onChange:n=>f(n.target.value)})]}),e.jsxs(U,{children:[e.jsx(c,{variant:"outline",onClick:()=>m(!1),children:"Cancel"}),e.jsx(c,{onClick:Ne,children:"Update Cost"})]})]})}),e.jsx(Be,{open:k,onOpenChange:x,vehicleId:t,onSaved:G}),e.jsx(Re,{open:ue,onOpenChange:V,vehicleId:t,onSaved:u}),e.jsx(L,{open:pe,onOpenChange:D,children:e.jsxs(E,{children:[e.jsx(P,{children:e.jsx(M,{children:"Edit Stock Date"})}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(R,{children:"Date In Stock"}),e.jsx(ie,{type:"date",value:H,onChange:n=>O(n.target.value)})]}),e.jsxs(U,{children:[e.jsx(c,{variant:"outline",onClick:()=>D(!1),children:"Cancel"}),e.jsx(c,{onClick:async()=>{if(!t)return;const{error:n}=await l.from("inventory").update({date_in_stock:H}).eq("id",t);n?i({title:"Error",description:n.message,variant:"destructive"}):(i({title:"Success",description:"Stock date updated"}),u(),D(!1))},children:"Save"})]})]})}),e.jsx(Pe,{open:je,onOpenChange:A,vehicle:s,onSaved:u})]})})}export{fs 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.