Stage {String(FSTAGES.indexOf(st) + 1).padStart(2, "0")} · {st.n}
{st.does}
{st.why}
const { useState, useEffect, useRef } = React;
function now() { const d = new Date(); return d.toLocaleTimeString("en-MY", { hour: "2-digit", minute: "2-digit", second: "2-digit" }); }
const LS = "mc_funnel_v1";
function App() {
const saved = (() => { try { return JSON.parse(localStorage.getItem(LS)) || {}; } catch (e) { return {}; } })();
// deep links from the website: ?screen=quiz | ?screen=car&car=CH-102
const qp = new URLSearchParams(location.search);
if (qp.get("screen")) { saved.screen = qp.get("screen"); saved.source = "Website"; }
if (qp.get("car") && FSTOCK.some(c => c.id === qp.get("car"))) saved.carId = qp.get("car");
if (qp.toString()) history.replaceState(null, "", location.pathname);
// The public page is the buyer journey only; the dealer console is served from the admin panel.
const view = FMODE === "dealer" ? "dealer" : "journey";
const [screen, setScreen] = useState(saved.screen || "ad");
const [carId, setCarId] = useState(saved.carId || "MC-208");
const [source, setSource] = useState(saved.source || "Social ad");
const [quiz, setQuiz] = useState(saved.quiz || { monthly: 1800, charge: null, km: null });
// Stock always comes from the live site; only demo status changes (e.g. a reservation) are kept.
const [stock, setStock] = useState(() => FSTOCK.map(c => { const o = (saved.stock || []).find(x => x.id === c.id); return o && c.status === "available" ? { ...c, status: o.status } : c; }));
const [leads, setLeads] = useState(saved.leads || FSEED_LEADS);
const [sess, setSess] = useState(saved.sess || { reach: 0, visits: 0, matched: 0, proof: 0, leads: 0, reserved: 0, sold: 0, nurture: 0, sellers: 0 });
const [events, setEvents] = useState(saved.events || []);
const [ctx, setCtx] = useState({});
const [lastLead, setLastLead] = useState(null);
const [sell, setSell] = useState(null);
const [toast, setToast] = useState("");
const [pulse, setPulse] = useState(null);
useEffect(() => { localStorage.setItem(LS, JSON.stringify({ screen, carId, source, quiz, stock, leads, sess, events })); }, [screen, carId, source, quiz, stock, leads, sess, events]);
useEffect(() => { if (!toast) return; const t = setTimeout(() => setToast(""), 3200); return () => clearTimeout(t); }, [toast]);
const car = stock.find(c => c.id === carId) || stock[0];
const stage = SCREEN_STAGE[screen];
const bump = (k, n = 1) => { setSess(s => ({ ...s, [k]: s[k] + n })); setPulse(k); setTimeout(() => setPulse(null), 700); };
const log = (st, text) => setEvents(e => [{ t: now(), st, text }, ...e].slice(0, 40));
const addLead = (l) => {
const id = "L-0" + (142 + leads.length - FSEED_LEADS.length);
const lead = { id, ago: "now", source, ...l };
setLeads(ls => [lead, ...ls]); bump("leads");
return lead;
};
const go = (s, opts = {}) => {
if (opts.source) setSource(opts.source);
if (opts.carId) setCarId(opts.carId);
const nextCtx = { ...ctx, ...opts }; setCtx(nextCtx);
// tracking
if (screen === "ad" && s !== "ad") { bump("reach", 1); bump("visits"); log("attract", `New visitor from ${opts.source || source}`); }
if (s === "results") { bump("matched"); log("match", `Finder done: ${RM(quiz.monthly)}/mo, prefers “${quiz.charge}”, for “${quiz.km}”`); }
if (s === "car" && screen !== "reserve" && screen !== "test" && screen !== "chat") { bump("proof"); log("prove", `Viewed proof pack for ${opts.carId || carId}`); }
if (s === "nurture" && screen !== "nurture") { bump("nurture"); log("nurture", "Buyer entered 4-touch follow-up sequence"); }
if (s === "after") { bump("sold"); log("retain", `${car.id} delivered → owner hub activated`); }
if (s === "reserve" && opts.waitlist) { setScreen("waitlist"); return; }
setScreen(s);
};
const submitForm = (kind) => (f) => {
const type = kind === "reserve" ? "Reservation" : kind === "waitlist" ? "Waitlist" : "Test drive";
const lead = addLead({ name: f.name, phone: f.phone, type, car: `${car.id} ${car.year} ${car.make} ${car.model}`, stage: kind === "reserve" ? "Reserved" : kind === "test" ? "Viewing" : "New", intent: kind === "reserve" ? "hot" : kind === "test" ? "warm" : "cool", note: f.loan + (f.date ? " · " + f.date : "") });
setLastLead(lead);
if (kind === "reserve") { setStock(st => st.map(c => c.id === car.id ? { ...c, status: "reserved" } : c)); bump("reserved"); log("commit", `${car.id} RESERVED by ${f.name} · deposit ${RM(FCONFIG.deposit)} · stock status updated everywhere`); }
else log("commit", `${type} lead: ${f.name} for ${car.id}`);
setCtx({ ...ctx, kind }); setScreen("confirm");
};
const onChat = (text) => {
addLead({ name: "(via WhatsApp)", phone: "—", type: ctx.chatKind === "car" ? "WhatsApp" : "Sourcing request", car: ctx.chatKind === "car" ? `${car.id} ${car.year} ${car.make} ${car.model}` : `Wants ≤${RM(quiz.monthly)}/mo`, stage: "New", intent: "cool", note: text.slice(0, 60) + "…" });
log("commit", `WhatsApp opened with pre-filled ${ctx.chatKind === "car" ? "stock no. " + car.id : "sourcing request"}`);
if (FCONFIG.whatsapp) window.open(`https://wa.me/${FCONFIG.whatsapp}?text=${encodeURIComponent(text)}`, "_blank", "noopener");
};
const onSell = (f) => {
addLead({ name: f.name, phone: f.phone, type: "Sell / consign", car: `${f.year} ${f.car} · ${(+f.km).toLocaleString()} km`, stage: "New", intent: "warm", note: f.mode });
bump("sellers"); log("source", `Owner ${f.mode === "Consign with you" ? "consignment" : "sell"} offer: ${f.year} ${f.car} → stock pipeline`);
setSell(f); setScreen("sellDone");
};
const jumpStage = (st) => {
const s = FSTAGES.find(x => x.k === st);
if (st === "commit") { setScreen("car"); setCtx({ ...ctx }); setToast("Pick Reserve, Test drive or WhatsApp at the bottom of the listing."); return; }
setScreen(s.first);
};
const reset = () => { localStorage.removeItem(LS); location.reload(); };
const renderScreen = () => {
switch (screen) {
case "ad": return
Stage {String(FSTAGES.indexOf(st) + 1).padStart(2, "0")} · {st.n}
{st.why}