1import{j as e}from"./ui-vendor-B12AOeKi.js";import{r as l,e as Fe}from"./react-vendor-DZt7wVAJ.js";import{Q as ce,E as ue,q as Se,y as Ce,aq as Ae,L as x,ac as Pe,I as S,g as R,aa as Le,s as v,a4 as Te,D as oe,b as de,d as me,B as k,am as Ie,X as $e,F as De,a3 as ke,i as y,ap as Ve,u as qe,aZ as Be,c as he,e as pe,h as xe,ar as Oe,a1 as Re,ab as ge,v as Ue,w as ze,l as Ke,C as Ye,at as He,ao as Qe}from"./index-CUOC2qQu.js";import{f as Ge}from"./phoneFormatter-CWyUoJyv.js";import{S as Je,f as Xe,a as Ze,b as We,c as es}from"./sheet-D40jqZ33.js";import{T as Me,b as Ee,c as W,a as ee}from"./tabs-Cesl5pOW.js";import{g as ss,P as ts,a as as}from"./PreWriteUserSettings-DxUAVW5m.js";import{R as rs,a as le}from"./radio-group-5gLL5TFZ.js";import{C as ns,S as je,a as fe,b as ve,c as Ne,d as D}from"./select-B8RrBf7t.js";import{A as is,a as ls,b as cs,c as os,d as ds,e as ms,g as us,f as hs}from"./alert-dialog-Dj2y0brn.js";import{G as ps}from"./gauge-D7Jb2mFC.js";import{V as xs}from"./VinScanner-DNxCZ51K.js";import{S as gs}from"./save-CgF44BVm.js";import{u as js,M as ye}from"./MaintenancePage-DRg833PK.js";import{u as fs,A as _e}from"./useAppDisabled-93JpaqRn.js";import{L as vs}from"./lock-DGJx2UZG.js";import{M as Ns}from"./menu-BthHLreT.js";import{a as ys}from"./addDays-BsMZix8N.js";import{s as _s}from"./subDays-COAzsAQX.js";import{i as we,a as be}from"./isBefore-Dfnipc6t.js";import{i as ws}from"./isToday-d216me-m.js";import"./charts-vendor-B6q1L48j.js";import"./supabase-vendor-dOy44Gxq.js";import"./animation-vendor-BAVy02gd.js";import"./placeholder-logo-Ca5gyopp.js";import"./htmlReportGenerator-OdpaSbGl.js";import"./circle-check-big-Cq5oAhbT.js";import"./printer-BNAVOmS6.js";import"./switch-Doi0_Deq.js";import"./index-D9T2E487.js";import"./scan-dQzSwIvz.js";import"./camera-DGoi0Dsr.js";import"./constructNow-Bjn_O6S1.js";import"./isSameDay-D98FJCfs.js";const bs=({selectedServices:d,onServicesChange:E,currentMileage:_,vin:se,vehicleMake:U=null})=>{var B;const[L,G]=l.useState(new Set),[c,M]=l.useState(null),[te,m]=l.useState(new Set),{data:g}=ce({queryKey:["prewrite-services"],queryFn:async()=>{const{data:s,error:r}=await v.from("prewrite_services").select("*").eq("is_active",!0).order("display_order");if(r)throw r;return s}}),{data:z}=ce({queryKey:["service-history-mpi",se],queryFn:async()=>{if(!se)return[];const{data:s,error:r}=await v.from("prewrite_record_services").select("*, prewrite_records!inner(vin), prewrite_services(*)").eq("prewrite_records.vin",se).eq("status","declined").order("created_at",{ascending:!1});if(r)throw r;return s},enabled:!!se});l.useEffect(()=>{if(g&&g.length>0&&d.length===0){const s=g.find(r=>{var a;return(a=r.service_name)==null?void 0:a.includes("Multi-Point Inspection")});s&&E([{service_id:s.id,status:"due",prewrite_services:s}])}},[g]);const{data:F}=ce({queryKey:["prewrite-settings"],queryFn:async()=>{const{data:s,error:r}=await v.from("prewrite_settings").select("setting_key, setting_value").in("setting_key",["show_pricing"]);if(r)throw r;const a={show_pricing:!0};return s==null||s.forEach(o=>{o.setting_key==="show_pricing"&&(a.show_pricing=o.setting_value==="true")}),a}}),j=(s,r)=>{E(r?[...d,{service_id:s.id,status:"due",prewrite_services:s}]:d.filter(a=>a.service_id!==s.id))},K=(s,r)=>{m(a=>{const o=new Set(a);return o.delete(s),o}),E(d.map(a=>a.service_id===s?{...a,status:r,...r!=="completed"?{completed_date:null,completed_mileage:null}:{}}:a))},T=(s,r,a)=>{const o=d.map(u=>u.service_id===s?{...u,[r]:a}:u);E(o)},V=s=>{if(te.has(s))return;const r=d.find(o=>o.service_id===s),a=g==null?void 0:g.find(o=>o.id===s);if(r&&a&&r.status==="completed"&&r.completed_date&&r.completed_mileage&&r.completed_mileage.toString().length>=3&&!isNaN(parseInt(r.completed_mileage))&&_){const o=new Date(r.completed_date),u=new Date,w=(u.getFullYear()-o.getFullYear())*12+(u.getMonth()-o.getMonth()),C=_-parseInt(r.completed_mileage),A=a.recommended_interval_months&&w>=a.recommended_interval_months,O=a.recommended_interval_miles&&C>=a.recommended_interval_miles;(A||O)&&M({show:!0,serviceId:s,serviceName:a.service_name,completedDate:r.completed_date,completedMileage:parseInt(r.completed_mileage),intervalMonths:a.recommended_interval_months,intervalMiles:a.recommended_interval_miles})}},I=s=>
1{m(r=>{const a=new Set(r);return a.delete(s),a}),setTimeout(()=>V(s),100)},Y=s=>d.some(r=>r.service_id===s),N=s=>d.find(r=>r.service_id===s),J=s=>{G(r=>{const a=new Set(r);return a.has(s)?a.delete(s):a.add(s),a})},X=s=>{switch(s){case"completed":return"bg-green-500/10 text-green-700 border-green-500/20";case"accepted":return"bg-blue-500/10 text-blue-700 border-blue-500/20";case"due":return"bg-yellow-500/10 text-yellow-700 border-yellow-500/20";case"upcoming":return"bg-purple-500/10 text-purple-700 border-purple-500/20";case"declined":return"bg-red-500/10 text-red-700 border-red-500/20";default:return"bg-muted text-muted-foreground"}},H=(s,r)=>{var u;if((u=s.service_name)!=null&&u.includes("Multi-Point Inspection")||!_)return null;const a=s.recommended_interval_miles,o=s.recommended_interval_months;return!a&&!o?null:(r==null||r.status,a&&o?e.jsxs(R,{variant:"outline",className:"bg-orange-500/10 text-orange-700 border-orange-500/20",children:[e.jsx(ue,{className:"h-3 w-3 mr-1"}),"Due By Time & Mileage"]}):a?e.jsxs(R,{variant:"outline",className:"bg-blue-500/10 text-blue-700 border-blue-500/20",children:[e.jsx(ps,{className:"h-3 w-3 mr-1"}),"Due By Mileage"]}):o?e.jsxs(R,{variant:"outline",className:"bg-purple-500/10 text-purple-700 border-purple-500/20",children:[e.jsx(Le,{className:"h-3 w-3 mr-1"}),"Due By Time"]}):null)},q=s=>z?z.find(a=>a.service_id===s):null;if(!g||g.length===0)return e.jsx("p",{className:"text-center text-muted-foreground py-4",children:"No services available"});const Q=[...g].sort((s,r)=>{var u,w;const a=(u=s.service_name)==null?void 0:u.includes("Multi-Point Inspection"),o=(w=r.service_name)==null?void 0:w.includes("Multi-Point Inspection");return a&&!o?-1:!a&&o?1:(s.display_order||0)-(r.display_order||0)});return e.jsxs(e.Fragment,{children:[e.jsx(is,{open:(c==null?void 0:c.show)||!1,onOpenChange:s=>{s||M(null)},children:e.jsxs(ls,{children:[e.jsxs(cs,{children:[e.jsxs(os,{className:"flex items-center gap-2",children:[e.jsx(ue,{className:"h-5 w-5 text-orange-500"}),"Service May Be Due Again"]}),e.jsxs(ds,{className:"space-y-2",children:[e.jsx("p",{children:"Based on the completion date and mileage you entered, this service appears to be due again:"}),e.jsxs("div",{className:"bg-muted p-3 rounded-md space-y-1 text-xs",children:[e.jsx("p",{children:e.jsx("strong",{children:c==null?void 0:c.serviceName})}),e.jsxs("p",{children:["Completed: ",c!=null&&c.completedDate?new Date(c.completedDate).toLocaleDateString():""," at ",(B=c==null?void 0:c.completedMileage)==null?void 0:B.toLocaleString()," miles"]}),e.jsxs("p",{children:["Current Mileage: ",_==null?void 0:_.toLocaleString()," miles"]}),(c==null?void 0:c.intervalMonths)&&e.jsxs("p",{children:["Interval: Every ",c.intervalMonths," months"]}),(c==null?void 0:c.intervalMiles)&&e.jsxs("p",{children:["Interval: Every ",c.intervalMiles.toLocaleString()," miles"]})]}),e.jsx("p",{className:"text-orange-600 font-semibold",children:'This service should be marked as "Due Now" instead of "Completed"'})]})]}),e.jsxs(ms,{children:[e.jsx(us,{onClick:()=>{c!=null&&c.serviceId&&m(s=>new Set(s).add(c.serviceId)),M(null)},children:"Change Date/Mileage"}),e.jsx(hs,{onClick:()=>{c!=null&&c.serviceId&&m(s=>{const r=new Set(s);return r.delete(c.serviceId),r}),M(null)},children:"Keep As Completed"})]})]})}),e.jsx("div",{className:"space-y-3",children:Q.map(s=>{var C,A,O;const r=Y(s.id),a=N(s.id),o=L.has(s.id),u=(C=s.service_name)==null?void 0:C.includes("Multi-Point Inspection"),w=q(s.id);return e.jsx(Se,{className:`${r?"border-primary":""} ${u?"border-2 border-blue-500 bg-blue-50/50":""}`,children:e.jsx(Ce,{className:"p-4",children:e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(Ae,{id:`service-${s.id}`,checked:r,onCheckedChange:h=>j(s,h)}),e.jsxs("div",{className:"flex-1 cursor-pointer",onClick:()=>r&&J(s.id),children:[e.jsxs(x,{htmlFor:`service-${s.id}`,className:"font-semibold cursor-pointer flex items-center gap-2",children:[u&&e.jsx("span",{className:"text-green-600",children:"â"}),s.service_name,u&&e.jsx("span",{className:"text-xs font-bold text-green-600",children:"(COMPLIMENTARY)"})]}),(F==null?void 0:F.show_pricing)&&s.price&&s.price>0&&e.jsxs("p",{className:"text-sm font-semibold text-primary",children:["$",s.price.toFixed(2)]}),u&&w&&e.jsx("p",{className:"text-xs text-red-600 font-semibold",children:"Previously Declined"})]}),r&&e.jsx("button",{onClick:()=>J(s.id),className:"p-1",children:o?e.jsx(ns,{className:"h-4 w-4"}):e.jsx(Pe,{className:"h-4 w-4"})})]}),r&&o&&e.jsxs("div",{className:"ml-7 space-y-3 pt-2 border-t",children:[e.jsxs("div",{children:[e.jsx("p",{className:"text-xs text-muted-foreground mb-2",children:ss(s.service_name,s.service_description,U)}),s.recommended_interval_miles&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Every ",s.recommended_interval_miles.toLocaleStr
1ing()," miles"]}),s.recommended_interval_months&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Every ",s.recommended_interval_months," months"]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{className:"text-xs",children:"Service Status"}),e.jsxs(rs,{value:(a==null?void 0:a.status)||"due",onValueChange:h=>K(s.id,h),className:"space-y-2",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{value:"completed",id:`${s.id}-completed`}),e.jsx(x,{htmlFor:`${s.id}-completed`,className:"text-xs cursor-pointer",children:"Completed"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{value:"accepted",id:`${s.id}-accepted`}),e.jsx(x,{htmlFor:`${s.id}-accepted`,className:"text-xs cursor-pointer",children:"Accepted"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{value:"due",id:`${s.id}-due`}),e.jsx(x,{htmlFor:`${s.id}-due`,className:"text-xs cursor-pointer",children:"Due Now"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{value:"upcoming",id:`${s.id}-upcoming`}),e.jsx(x,{htmlFor:`${s.id}-upcoming`,className:"text-xs cursor-pointer",children:"Upcoming"})]}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(le,{value:"declined",id:`${s.id}-declined`}),e.jsx(x,{htmlFor:`${s.id}-declined`,className:"text-xs cursor-pointer",children:"Declined"})]})]}),(a==null?void 0:a.status)==="completed"&&e.jsxs("div",{className:"mt-3 space-y-3 p-3 bg-muted/50 rounded-lg",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{className:"text-xs font-semibold",children:"When was this completed?"}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsx(x,{className:"text-xs text-muted-foreground",children:"Year"}),e.jsxs(je,{value:((A=a==null?void 0:a.completed_date)==null?void 0:A.split("-")[0])||"",onValueChange:h=>{const P=((a==null?void 0:a.completed_date)||"").split("-")[1]||"01";T(s.id,"completed_date",`${h}-${P}-01`)},onOpenChange:h=>{h||I(s.id)},children:[e.jsx(fe,{className:"h-9",children:e.jsx(ve,{placeholder:"Year"})}),e.jsx(Ne,{children:Array.from({length:5},(h,Z)=>new Date().getFullYear()-Z).map(h=>e.jsx(D,{value:h.toString(),children:h},h))})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(x,{className:"text-xs text-muted-foreground",children:"Month"}),e.jsxs(je,{value:((O=a==null?void 0:a.completed_date)==null?void 0:O.split("-")[1])||"",onValueChange:h=>{const P=((a==null?void 0:a.completed_date)||"").split("-")[0]||new Date().getFullYear().toString();T(s.id,"completed_date",`${P}-${h}-01`)},onOpenChange:h=>{h||I(s.id)},children:[e.jsx(fe,{className:"h-9",children:e.jsx(ve,{placeholder:"Month"})}),e.jsxs(Ne,{children:[e.jsx(D,{value:"01",children:"January"}),e.jsx(D,{value:"02",children:"February"}),e.jsx(D,{value:"03",children:"March"}),e.jsx(D,{value:"04",children:"April"}),e.jsx(D,{value:"05",children:"May"}),e.jsx(D,{value:"06",children:"June"}),e.jsx(D,{value:"07",children:"July"}),e.jsx(D,{value:"08",children:"August"}),e.jsx(D,{value:"09",children:"September"}),e.jsx(D,{value:"10",children:"October"}),e.jsx(D,{value:"11",children:"November"}),e.jsx(D,{value:"12",children:"December"})]})]})]})]})]}),e.jsxs("div",{className:"space-y-1",children:[e.jsx(x,{className:"text-xs text-muted-foreground",children:"Mileage"}),e.jsx(S,{type:"number",placeholder:"Enter mileage",value:(a==null?void 0:a.completed_mileage)||"",onChange:h=>T(s.id,"completed_mileage",h.target.value),onBlur:()=>I(s.id),className:"h-9"})]})]})]})]}),r&&!o&&a&&e.jsxs("div",{className:"ml-7 flex gap-2 flex-wrap",children:[e.jsx(R,{variant:"outline",className:X(a.status),children:a.status}),H(s,a)]}),!r&&_&&e.jsx("div",{className:"ml-7",children:H(s,null)})]})})},s.id)})})]})},Ss=({recordId:d,isOpen:E,onClose:_,currentUser:se})=>{const[U,L]=l.useState(""),[G,c]=l.useState(""),[M,te]=l.useState(""),[m,g]=l.useState(""),[z,F]=l.useState(null),[j,K]=l.useState(""),[T,V]=l.useState(""),[I,Y]=l.useState(""),[N,J]=l.useState(null),[X,H]=l.useState(""),[q,Q]=l.useState([]),[B,s]=l.useState(!1),[r,a]=l.useState(d),[o,u]=l.useState(null),[w,C]=l.useState(!1),[A,O]=l.useState("customer"),[h,Z]=l.useState(!1),P=Te(),{data:b}=ce({queryKey:["prewrite-record",d],queryFn:async()=>
1{if(!d)return null;const{data:n,error:i}=await v.from("prewrite_records").select("*").eq("id",d).maybeSingle();if(i)throw i;return n},enabled:!!d}),{data:re}=ce({queryKey:["prewrite-record-services",d],queryFn:async()=>{if(!d)return[];const{data:n,error:i}=await v.from("prewrite_record_services").select("*, prewrite_services(*)").eq("record_id",d);if(i)throw i;return n},enabled:!!d});l.useEffect(()=>{b&&(L(b.customer_name||""),c(b.customer_phone||""),te(b.customer_email||""),g(b.vin||""),F(b.year),K(b.make||""),V(b.model||""),Y(b.trim||""),J(b.current_mileage),H(b.appointment_date||""))},[b]),l.useEffect(()=>{re&&(Q(re),C(!0))},[re]),l.useEffect(()=>{(async()=>{if(A==="services"&&!w&&j&&m&&!j.toLowerCase().includes("volkswagen")&&!j.toLowerCase().includes("vw"))try{console.log("Auto-loading manufacturer services for",j),y.info("Loading recommended services...");const{data:i,error:t}=await v.functions.invoke("get-manufacturer-services",{body:{vin:m,make:j,currentMileage:N}});if(t)throw t;if(i!=null&&i.recommendedServices&&i.recommendedServices.length>0){const p=i.recommendedServices.map(f=>({service_id:f.service_id,status:"pending",notes:f.reason,prewrite_services:{id:f.service_id,service_name:f.service_name,service_category:f.service_category,service_description:f.service_description,price:f.price,recommended_interval_miles:f.recommended_interval_miles}}));Q(p),C(!0);const $=i.hasHistory?"based on service history":"based on current mileage";y.success(`${i.recommendedServices.length} ${j} services recommended ${$}`,{duration:4e3})}else C(!0),y.info("No services due at this time")}catch(i){console.error("Error loading manufacturer services:",i),C(!0)}})()},[A,w,j,m,N]),l.useEffect(()=>{(async()=>{if(!m||!N){u(null);return}try{const{data:i,error:t}=await v.from("prewrite_records").select("current_mileage, created_at").eq("vin",m).order("created_at",{ascending:!1}).limit(1);if(t)throw t;if(i&&i.length>0&&i[0].current_mileage){const p=i[0].current_mileage;N<p?u(`Warning: Current mileage (${N.toLocaleString()}) is less than previous (${p.toLocaleString()})`):u(null)}}catch(i){console.error("Error checking mileage:",i)}})()},[m,N]);const ne=async n=>{if(F(n.year),K(n.make),V(n.model),Y(n.trim),n.make&&!n.make.toLowerCase().includes("volkswagen")&&!n.make.toLowerCase().includes("vw"))try{y.info("Checking manufacturer service recommendations...");const{data:i,error:t}=await v.functions.invoke("get-manufacturer-services",{body:{vin:m,make:n.make,currentMileage:N}});if(t)throw t;if(i!=null&&i.recommendedServices&&i.recommendedServices.length>0){const p=i.recommendedServices.map(f=>({service_id:f.service_id,status:"upcoming",notes:f.reason,prewrite_services:{id:f.service_id,service_name:f.service_name,service_category:f.service_category,service_description:f.service_description,price:f.price,recommended_interval_miles:f.recommended_interval_miles}}));Q(p);const $=i.hasHistory?"Based on service history":"Based on current mileage";y.success(`${i.recommendedServices.length} ${n.make} services recommended. ${$}.`,{duration:5e3})}else i!=null&&i.message&&y.info(i.message)}catch(i){console.error("Error fetching manufacturer services:",i),y.error("Could not load manufacturer recommendations")}},ae=async()=>{if(!U||!G||!m){y.error("Please fill in customer name, phone, and VIN");return}try{const n={customer_name:U,customer_phone:G,customer_email:M,vin:m,year:z,make:j,model:T,trim:I,current_mileage:N,appointment_date:X||null};let i=r;if(r){const{error:t}=await v.from("prewrite_records").update(n).eq("id",r);if(t)throw t}else{const{data:t,error:p}=await v.from("prewrite_records").insert([n]).select().maybeSingle();if(p)throw p;if(!t)throw new Error("Failed to create record");i=t.id,a(t.id)}if(i&&q.length>0){await v.from("prewrite_record_services").delete().eq("record_id",i);const t=q.map($=>({record_id:i,service_id:$.service_id||$.id,status:$.status||"upcoming",completed_date:$.completed_date||null,completed_mileage:$.completed_mileage||null,notes:$.notes||null})),{error:p}=await v.from("prewrite_record_services").insert(t);if(p)throw p}return y.success("Saved successfully"),i}catch(n){return console.error("Error saving record:",n),y.error(n.message||"Failed to save record"),null}},ie=async()=>{const n=await ae();n&&(await P.invalidateQueries({queryKey:["prewrite-record",n]}),await P.invalidateQueries({queryKey:["prewrite-record-services",n]}),await new Promise(i=>setTimeout(i,300)),s(!0))};return B&&r?e.jsx(oe,{open:E,onOpenChange:_,children:e.jsx(de,{className:"p-0 overflow-auto",children:e.jsx(ts,{recordId:r,onBack:()=>s(!1),onClose:_})})}):e.jsx(oe,{open:E,onOpenChange:_,children:e.jsxs(de,{className:"p-0 overflow-auto",children:[e.jsxs("div",{className:"sticky top-0 z-10 bg-background border-b p-4",children:[e.jsxs("div",{className:"flex items-center justify-between mb-2",children:[e.jsx(me,{className:"text-lg",children:d?"Edit Sell Sheet":"New Sell Sheet"}),e.jsxs("div",{className:"flex gap-2",children:[e.jsx(k,{variant:"ghost",size:"icon",onClick:()=>Z(!0),children:e.jsx(Ie,{className:"h-5 w-5"})}),e.jsx(k,{variant:"ghost",size:"icon",onClick:_,children:e.jsx($e,{className:"h-5 w-5"})})]})]}),e.jsxs("div",{className:"flex gap-2",children:[e.jsxs(k,{variant:"outline",size:"sm",onClick:ae,children:[e.jsx(gs,{className:"mr-2 h-4 w-4"}),"Save"]}),e.jsxs(k,{size:"sm",onClick:ie,children:[e.jsx(De,{className:"mr-2 h-4 w-4"}),"Sell Sheet"]})]})]}),e.jsx("div",{className:"p-4 space-y-4",children:e.jsxs(Me,{value:A,onValueChange:O,className:"w-full",children:[e.jsxs(Ee,{className:"grid w-full grid-cols-3",children:[e.jsx(W,{value:"customer",children:"Customer"}),e.jsx(W,{value:"vehicle",children:"Vehicle"}),e.jsx(W,{value:"services",children:"Services"})]}
1),e.jsxs(ee,{value:"customer",className:"space-y-4 mt-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"customerName",children:"Customer Name *"}),e.jsx(S,{id:"customerName",value:U,onChange:n=>L(n.target.value),placeholder:"John Doe"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"customerPhone",children:"Phone Number *"}),e.jsx(S,{id:"customerPhone",type:"tel",value:G,onChange:n=>c(n.target.value),placeholder:"(555) 123-4567"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"customerEmail",children:"Email Address"}),e.jsx(S,{id:"customerEmail",type:"email",value:M,onChange:n=>te(n.target.value),placeholder:"[email protected]"})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"appointmentDate",children:"Appointment Date"}),e.jsx(S,{id:"appointmentDate",type:"date",value:X,onChange:n=>H(n.target.value)})]})]}),e.jsxs(ee,{value:"vehicle",className:"space-y-4 mt-4",children:[e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"vin",children:"VIN *"}),e.jsx(S,{id:"vin",value:m,onChange:n=>g(n.target.value.toUpperCase()),placeholder:"Enter 17-character VIN",maxLength:17}),e.jsx("div",{className:"flex gap-2",children:e.jsxs(k,{type:"button",variant:"outline",size:"sm",onClick:async()=>{if(!m||m.length!==17){y.error("Please enter a valid 17-character VIN");return}try{y.info("Decoding VIN...");const{data:n,error:i}=await v.functions.invoke("decode-vin",{body:{vin:m}});if(i)throw i;ne(n),y.success("VIN decoded successfully")}catch(n){console.error("Error decoding VIN:",n),y.error(n.message||"Failed to decode VIN")}},disabled:!m||m.length!==17,className:"flex-1",children:[e.jsx(ke,{className:"mr-2 h-4 w-4"}),"Decode VIN"]})}),e.jsx(xs,{onVinScanned:g})]})}),e.jsxs("div",{className:"grid grid-cols-2 gap-4",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"year",children:"Year"}),e.jsx(S,{id:"year",type:"number",value:z||"",onChange:n=>F(Number(n.target.value)||null)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"make",children:"Make"}),e.jsx(S,{id:"make",value:j,onChange:n=>K(n.target.value)})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"model",children:"Model"}),e.jsx(S,{id:"model",value:T,onChange:n=>V(n.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"trim",children:"Trim"}),e.jsx(S,{id:"trim",value:I,onChange:n=>Y(n.target.value)})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"currentMileage",children:"Current Mileage"}),e.jsx(S,{id:"currentMileage",type:"text",inputMode:"numeric",value:N?N.toLocaleString():"",onChange:n=>{const i=n.target.value.replace(/,/g,""),t=parseInt(i,10);J(isNaN(t)?null:t)},placeholder:""}),o&&e.jsxs("div",{className:"flex items-start gap-2 p-2 bg-yellow-500/10 border border-yellow-500/20 rounded-md",children:[e.jsx(Ve,{className:"h-4 w-4 text-yellow-500 mt-0.5 flex-shrink-0"}),e.jsx("p",{className:"text-xs text-yellow-700 dark:text-yellow-400",children:o})]})]})]}),e.jsx(ee,{value:"services",className:"mt-4",children:e.jsx(bs,{selectedServices:q,onServicesChange:Q,currentMileage:N,vin:m,vehicleMake:j})})]})}),e.jsx(as,{open:h,onClose:()=>Z(!1)})]})})},it=()=>{const d=Fe(),[E,_]=l.useState([]),[se,U]=l.useState(!0),[L,G]=l.useState(""),[c,M]=l.useState(!1),[te,m]=l.useState(!0),[g,z]=l.useState(""),[F,j]=l.useState(!1),[K,T]=l.useState(""),[V,I]=l.useState(""),[Y,N]=l.useState(""),[J,X]=l.useState(!1),[H,q]=l.useState(""),[Q,B]=l.useState(null),{currentUser:s,hasAppAccess:r}=qe(),{isMaintenanceMode:a,isLoading:o}=js(),{isDisabled:u,isLoading:w}=fs("prewrite");l.useEffect(()=>{if(s&&r("pre_write")){I(s.name),j(!0),m(!1);return}const t=sessionStorage.getItem("prewrite-user");if(t)try{const p=JSON.parse(t);I(p.name),j(!0),m(!1)}catch(p){console.error("Error parsing user data:",p)}},[s,r]),l.useEffect(()=>{if(!F)return;C();const t=v.channel("prewrite_mobile_changes").on("postgres_changes",{event:"*",schema:"public",table:"prewrite_records"},()=>{C()}).subscribe();return()=>{v.removeChannel(t)}},[F]);const C=async()=>{try{U(!0);const{data:t,error:p}=await v.from("prewrite_records").select("*").eq("archived",!1).order("appointment_date",{ascending:!0});if(p)throw p;_(t||[])}catch(t){console.error("Error fetching records:",t),y.error("Failed to load records")}finally{U(!1)}},A=()=>{g==="prewrite"?(m(!1),T(""),V?j(!0):X(!0)):(T("Incorrect passcode. Please try again."),z(""))},O=()=>{const t=Y.trim();if(!t){q("Please enter your first name");return}I(t),X(!1),j(!0),q("")},h=L.trim()?E.filter(t=>t.customer_name.toLowerCase().includes(L.toLowerCase())||t.vin.toLowerCase().includes(L.toLowerCase())||t.customer_phone.includes(L)):E,P=Be(ne
1w Date),b=ys(P,7),re=_s(P,30),ne=h.filter(t=>{if(!t.appointment_date)return!1;const p=new Date(t.appointment_date);return we(p,P)&&be(p,re)}),ae=h.filter(t=>t.appointment_date?ws(new Date(t.appointment_date)):!1),ie=h.filter(t=>{if(!t.appointment_date)return!1;const p=new Date(t.appointment_date);return be(p,P)&&we(p,b)}),n=h.filter(t=>!t.appointment_date),i=({record:t})=>e.jsxs(Se,{className:"mb-3",onClick:()=>{B(t.id),M(!0)},children:[e.jsx(Ue,{className:"pb-3",children:e.jsxs("div",{className:"flex items-start justify-between",children:[e.jsx(ze,{className:"text-lg cursor-pointer hover:text-primary transition-colors",children:t.customer_name}),t.appointment_date&&e.jsx(R,{variant:"outline",className:"ml-2",children:Ke(new Date(t.appointment_date),"MMM dd")})]})}),e.jsxs(Ce,{className:"space-y-2 text-sm",children:[e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground",children:[e.jsx(Ye,{className:"h-4 w-4"}),e.jsxs("span",{children:[t.year," ",t.make," ",t.model]})]}),t.customer_phone&&e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground",children:[e.jsx(He,{className:"h-4 w-4"}),e.jsx("span",{children:Ge(t.customer_phone)})]}),t.customer_email&&e.jsxs("div",{className:"flex items-center gap-2 text-muted-foreground truncate",children:[e.jsx(Qe,{className:"h-4 w-4 flex-shrink-0"}),e.jsx("span",{className:"truncate",children:t.customer_email})]}),t.current_mileage&&e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Current Mileage: ",t.current_mileage.toLocaleString()," miles"]}),e.jsx("p",{className:"text-xs text-primary pt-1",children:"Tap to edit or view sell sheet"})]})]});return F?o||w?e.jsx("div",{className:"min-h-screen bg-background flex items-center justify-center",children:e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-muted-foreground",children:"Loading..."})})}):a?e.jsx(ye,{}):u?e.jsx(_e,{appName:"Pre-Write"}):e.jsxs("div",{className:"min-h-screen bg-background pb-20",children:[e.jsxs("header",{className:"sticky top-0 z-50 bg-background border-b",children:[e.jsxs("div",{className:"flex items-center justify-between p-4",children:[e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx(k,{variant:"ghost",size:"icon",onClick:()=>d("/"),children:e.jsx(Oe,{className:"h-5 w-5"})}),e.jsx(Re,{variant:"compact"})]}),e.jsxs("div",{className:"flex-1 text-center",children:[e.jsx("h1",{className:"text-lg font-semibold",children:"Pre-Write Mobile"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Welcome, ",V]})]}),e.jsxs(Je,{children:[e.jsx(Xe,{asChild:!0,children:e.jsx(k,{variant:"ghost",size:"icon",children:e.jsx(Ns,{className:"h-5 w-5"})})}),e.jsxs(Ze,{children:[e.jsx(We,{children:e.jsx(es,{children:"Menu"})}),e.jsx("div",{className:"mt-4 space-y-2",children:e.jsxs(k,{variant:"outline",className:"w-full",onClick:()=>{B(null),M(!0)},children:[e.jsx(ge,{className:"h-4 w-4 mr-2"}),"New Sell Sheet"]})})]})]})]}),e.jsx("div",{className:"px-4 pb-4",children:e.jsxs("div",{className:"relative",children:[e.jsx(ke,{className:"absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground"}),e.jsx(S,{placeholder:"Search by name, VIN, or phone...",value:L,onChange:t=>G(t.target.value),className:"pl-10"})]})})]}),e.jsx("main",{className:"p-4",children:e.jsxs(Me,{defaultValue:"today",className:"w-full",children:[e.jsxs(Ee,{className:"grid w-full grid-cols-4 mb-4",children:[e.jsxs(W,{value:"today",className:"text-xs",children:["Today",e.jsx(R,{variant:"secondary",className:"ml-1",children:ae.length})]}),e.jsxs(W,{value:"next7",className:"text-xs",children:["Next 7 Days",e.jsx(R,{variant:"secondary",className:"ml-1",children:ie.length})]}),e.jsxs(W,{value:"previous",className:"text-xs",children:["Previous",e.jsx(R,{variant:"secondary",className:"ml-1",children:ne.length})]}),e.jsxs(W,{value:"no-appt",className:"text-xs",children:["No Appt",e.jsx(R,{variant:"secondary",className:"ml-1",children:n.length})]})]}),e.jsx(ee,{value:"today",children:ae.length===0?e.jsxs("div",{className:"text-center py-8 text-muted-foreground",children:[e.jsx(De,{className:"h-12 w-12 mx-auto mb-4 opacity-50"}),e.jsx("p",{children:"No appointments scheduled for today"})]}):ae.map(t=>e.jsx(i,{record:t},t.id))}),e.jsx(ee,{value:"next7",children:ie.length===0?e.jsx("p",{className:"text-center text-muted-foreground py-8",children:"No appointments in the next 7 days"}):ie.map(t=>e.jsx(i,{record:t},t.id))}),e.jsx(ee,{value:"previous",children:ne.length===0?e.jsx("p",{className:"text-center text-muted-foreground py-8",children:"No previous appointments (last 30 days)"}):ne.map(t=>e.jsx(i,{record:t},t.id))}),e.jsx(ee,{value:"no-appt",children:n.length===0?e.jsx("p",{className:"text-center text-muted-foreground py-8",children:"All records have appointment dates"}):n.map(t=>e.jsx(i,{record:t},t.id))})]})}),e.jsx(k,{size:"icon",className:"fixed bottom-6 right-6 h-14 w-14 roun
1ded-full shadow-lg",onClick:()=>{B(null),M(!0)},children:e.jsx(ge,{className:"h-6 w-6"})}),c&&e.jsx(Ss,{recordId:Q,isOpen:c,onClose:()=>{M(!1),B(null),C()},currentUser:V})]}):e.jsxs(e.Fragment,{children:[(o||w)&&e.jsx("div",{className:"min-h-screen bg-background flex items-center justify-center",children:e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-muted-foreground",children:"Loading..."})})}),!o&&a&&e.jsx(ye,{}),!o&&!a&&!w&&u&&e.jsx(_e,{appName:"Pre-Write"}),!o&&!a&&!w&&!u&&e.jsxs(e.Fragment,{children:[e.jsx(oe,{open:te,onOpenChange:()=>d("/"),children:e.jsxs(de,{className:"",children:[e.jsxs(he,{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[e.jsx(vs,{className:"h-5 w-5"}),e.jsx(me,{children:"Protected Access"})]}),e.jsx(pe,{children:"Enter the passcode to access Pre-Write Mobile"})]}),e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"passcode",children:"Passcode"}),e.jsx(S,{id:"passcode",type:"password",value:g,onChange:t=>{z(t.target.value),T("")},onKeyPress:t=>t.key==="Enter"&&A(),placeholder:"Enter passcode",autoFocus:!0}),K&&e.jsx("p",{className:"text-sm text-destructive",children:K})]})}),e.jsxs(xe,{className:"gap-2",children:[e.jsx(k,{variant:"outline",onClick:()=>d("/"),children:"Cancel"}),e.jsx(k,{onClick:A,children:"Submit"})]})]})}),e.jsx(oe,{open:J,onOpenChange:()=>d("/"),children:e.jsxs(de,{className:"",children:[e.jsxs(he,{children:[e.jsx(me,{children:"Welcome!"}),e.jsx(pe,{children:"Please enter your first name for activity tracking"})]}),e.jsx("div",{className:"space-y-4",children:e.jsxs("div",{className:"space-y-2",children:[e.jsx(x,{htmlFor:"userName",children:"First Name"}),e.jsx(S,{id:"userName",type:"text",value:Y,onChange:t=>{N(t.target.value),q("")},onKeyPress:t=>t.key==="Enter"&&O(),placeholder:"Enter your first name",autoFocus:!0}),H&&e.jsx("p",{className:"text-sm text-destructive",children:H})]})}),e.jsxs(xe,{className:"gap-2",children:[e.jsx(k,{variant:"outline",onClick:()=>d("/"),children:"Cancel"}),e.jsx(k,{onClick:O,children:"Continue"})]})]})})]})]})};export{it 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.