← back to Permit Radar

public/index.html

642 lines

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Permit Radar — Residential Remodel Permit Directory</title>
<style>
:root { --cols: 3; --accent: #1d4ed8; --accent-light: #eff6ff; }
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font: 15px/1.5 -apple-system, Segoe UI, Roboto, sans-serif; color: #1a1a1a; background: #f3f4f6; }

/* ── SAMPLE BANNER ── */
.sample-banner {
  background: #fef3c7; border-bottom: 2px solid #f59e0b;
  padding: 8px 20px; font-size: 13px; text-align: center; color: #78350f;
}
.sample-banner strong { color: #92400e; }

/* ── TOP NAV ── */
nav {
  background: #1e293b; color: #fff; padding: 12px 20px;
  display: flex; align-items: center; gap: 16px;
}
.logo { font-size: 18px; font-weight: 800; letter-spacing: -0.5px; color: #fff; text-decoration: none; }
.logo span { color: #60a5fa; }
.nav-links { margin-left: auto; display: flex; gap: 16px; }
.nav-links a { color: #cbd5e1; font-size: 13px; text-decoration: none; }
.nav-links a:hover { color: #fff; }

/* ── HERO ── */
.hero {
  background: linear-gradient(135deg, #1e293b 0%, #1d4ed8 100%);
  color: #fff; padding: 48px 20px 40px; text-align: center;
}
.hero h1 { font-size: 32px; font-weight: 800; margin: 0 0 10px; }
.hero p { font-size: 16px; color: #bfdbfe; margin: 0 0 24px; }
.hero-cta {
  display: inline-block; background: #f59e0b; color: #1a1a1a;
  font-weight: 700; padding: 12px 28px; border-radius: 8px;
  text-decoration: none; font-size: 15px; transition: background .15s;
}
.hero-cta:hover { background: #fbbf24; }

/* ── CONTROLS ── */
.controls {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  padding: 14px 20px; background: #fff; border-bottom: 1px solid #e2e8f0;
  position: sticky; top: 0; z-index: 10;
}
.search-box {
  flex: 1; min-width: 200px; position: relative;
}
.search-box input {
  width: 100%; padding: 8px 12px 8px 36px; border: 1px solid #d1d5db;
  border-radius: 6px; font: inherit; font-size: 14px; outline: none;
}
.search-box input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(29,78,216,.12); }
.search-box::before { content: "🔍"; position: absolute; left: 10px; top: 50%; transform: translateY(-50%); font-size: 14px; }
.filter-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.filter-row select, .filter-row input[type=text] {
  padding: 7px 10px; border: 1px solid #d1d5db; border-radius: 6px;
  font: inherit; font-size: 13px; background: #fff; cursor: pointer;
}
.filter-row select:focus { outline: none; border-color: var(--accent); }
.ctrl-label { font-size: 12px; color: #64748b; display: flex; gap: 5px; align-items: center; }
.count-badge {
  margin-left: auto; background: var(--accent-light); color: var(--accent);
  font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 99px;
}

/* ── GRID ── */
.grid-wrap { padding: 20px; }
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: 14px;
}
.empty { padding: 60px; text-align: center; color: #94a3b8; font-size: 15px; }

/* ── PERMIT CARD ── */
.card {
  background: #fff; border: 1px solid #e2e8f0; border-radius: 10px;
  overflow: hidden; display: flex; flex-direction: column;
  transition: box-shadow .15s, transform .15s;
}
.card:hover { box-shadow: 0 4px 24px rgba(0,0,0,.10); transform: translateY(-1px); }
.card-header {
  padding: 14px 14px 10px; border-bottom: 1px solid #f1f5f9;
  display: flex; justify-content: space-between; align-items: flex-start; gap: 8px;
}
.permit-id { font-size: 11px; color: #94a3b8; font-weight: 600; letter-spacing: .5px; }
.status-chip {
  font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 99px; white-space: nowrap;
}
.status-approved { background: #dcfce7; color: #15803d; }
.status-pending   { background: #fef9c3; color: #854d0e; }
.status-issued    { background: #dbeafe; color: #1d4ed8; }
.type-chip {
  font-size: 11px; font-weight: 600; padding: 3px 9px; border-radius: 4px;
  background: #f1f5f9; color: #475569;
}
.card-body { padding: 12px 14px; flex: 1; }
.card-address { font-size: 14px; font-weight: 700; margin: 0 0 2px; line-height: 1.3; }
.card-city { font-size: 12px; color: #64748b; margin: 0 0 8px; }
.card-meta { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.meta-item { font-size: 12px; color: #475569; }
.meta-item strong { color: #1e293b; }
.card-desc { font-size: 12px; color: #64748b; line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-footer { padding: 10px 14px; border-top: 1px solid #f1f5f9; display: flex; gap: 8px; align-items: center; }
.btn-detail {
  flex: 1; text-align: center; padding: 7px; background: var(--accent-light);
  color: var(--accent); border: 1px solid #bfdbfe; border-radius: 6px;
  font-size: 12px; font-weight: 600; text-decoration: none; cursor: pointer;
}
.btn-detail:hover { background: #dbeafe; }
.btn-lead {
  flex: 1; text-align: center; padding: 7px; background: #f59e0b;
  color: #1a1a1a; border: none; border-radius: 6px;
  font-size: 12px; font-weight: 700; cursor: pointer; transition: background .15s;
}
.btn-lead:hover { background: #fbbf24; }
.valuation { font-size: 14px; font-weight: 700; color: #15803d; }

/* ── LEAD FUNNEL SECTION ── */
.funnel-section {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  color: #fff; padding: 56px 20px; margin-top: 32px; text-align: center;
}
.funnel-section h2 { font-size: 28px; margin: 0 0 10px; }
.funnel-section p { color: #94a3b8; margin: 0 0 32px; font-size: 15px; }
.plan-cards { display: flex; gap: 20px; justify-content: center; flex-wrap: wrap; margin-bottom: 36px; }
.plan-card {
  background: #1e293b; border: 1px solid #334155; border-radius: 12px;
  padding: 24px; width: 220px; text-align: left; position: relative;
}
.plan-card.featured { border-color: #f59e0b; }
.plan-badge {
  position: absolute; top: -10px; left: 50%; transform: translateX(-50%);
  background: #f59e0b; color: #1a1a1a; font-size: 11px; font-weight: 700;
  padding: 2px 12px; border-radius: 99px;
}
.plan-name { font-size: 16px; font-weight: 700; margin: 0 0 4px; }
.plan-price { font-size: 28px; font-weight: 800; color: #60a5fa; margin: 0 0 12px; }
.plan-price span { font-size: 14px; color: #94a3b8; font-weight: 400; }
.plan-features { list-style: none; margin: 0; padding: 0; font-size: 13px; color: #cbd5e1; }
.plan-features li { padding: 3px 0; }
.plan-features li::before { content: "✓ "; color: #60a5fa; font-weight: 700; }
.funnel-form {
  background: #fff; color: #1a1a1a; border-radius: 12px; padding: 28px;
  max-width: 500px; margin: 0 auto; text-align: left;
}
.funnel-form h3 { font-size: 18px; margin: 0 0 16px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.form-row.single { grid-template-columns: 1fr; }
.funnel-form label { display: block; font-size: 12px; font-weight: 600; color: #475569; margin-bottom: 4px; }
.funnel-form select, .funnel-form input {
  width: 100%; padding: 9px 12px; border: 1px solid #d1d5db; border-radius: 6px;
  font: inherit; font-size: 14px; outline: none;
}
.funnel-form select:focus, .funnel-form input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(29,78,216,.1); }
.btn-subscribe {
  width: 100%; padding: 12px; background: var(--accent); color: #fff;
  border: none; border-radius: 8px; font: 700 15px inherit; cursor: pointer; margin-top: 8px;
}
.btn-subscribe:hover { background: #1e40af; }
.form-note { font-size: 11px; color: #94a3b8; text-align: center; margin-top: 8px; }
.success-msg { background: #dcfce7; color: #15803d; border: 1px solid #bbf7d0; border-radius: 6px; padding: 12px; margin-top: 12px; font-size: 13px; text-align: center; }
.error-msg { background: #fee2e2; color: #991b1b; border: 1px solid #fca5a5; border-radius: 6px; padding: 12px; margin-top: 12px; font-size: 13px; text-align: center; }

/* ── LEAD MODAL ── */
.modal-bg {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 100; align-items: center; justify-content: center;
}
.modal-bg.open { display: flex; }
.modal {
  background: #fff; border-radius: 12px; padding: 28px; width: 100%; max-width: 440px;
  margin: 20px; position: relative;
}
.modal h3 { margin: 0 0 6px; font-size: 18px; }
.modal p { color: #64748b; font-size: 13px; margin: 0 0 18px; }
.modal-close {
  position: absolute; top: 14px; right: 16px; font-size: 20px; line-height: 1;
  cursor: pointer; color: #94a3b8; background: none; border: none;
}
.modal-close:hover { color: #1a1a1a; }

/* ── SHOPIFY CROSS-SELL STRIP ── */
.xsell-strip {
  padding: 28px 20px 32px; background: #fff; border-top: 1px solid #e2e8f0;
}
.xsell-header {
  display: flex; align-items: baseline; gap: 10px; margin-bottom: 16px; flex-wrap: wrap;
}
.xsell-label {
  font-size: 11px; font-weight: 700; letter-spacing: 1px; text-transform: uppercase;
  color: #94a3b8; border: 1px solid #e2e8f0; border-radius: 4px; padding: 2px 8px;
}
.xsell-title { font-size: 17px; font-weight: 700; color: #1e293b; margin: 0; }
.xsell-subtext { font-size: 12px; color: #94a3b8; margin-left: auto; }
.xsell-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(155px, 1fr));
  gap: 12px;
}
.xsell-card {
  border: 1px solid #e2e8f0; border-radius: 8px; overflow: hidden; background: #f8fafc;
  transition: box-shadow .15s;
  text-decoration: none; color: inherit; display: flex; flex-direction: column;
}
.xsell-card:hover { box-shadow: 0 3px 16px rgba(0,0,0,.09); }
.xsell-img-wrap { aspect-ratio: 1; overflow: hidden; background: #e2e8f0; }
.xsell-img-wrap img { width: 100%; height: 100%; object-fit: cover; display: block; }
.xsell-info { padding: 8px 10px 10px; flex: 1; }
.xsell-product-title { font-size: 12px; font-weight: 600; color: #1e293b; line-height: 1.3; margin-bottom: 2px;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.xsell-vendor { font-size: 11px; color: #94a3b8; }
.xsell-price { font-size: 13px; font-weight: 700; color: #15803d; margin-top: 4px; }
.xsell-cta-link {
  display: inline-block; margin-top: 14px; font-size: 12px; color: #1d4ed8;
  text-decoration: none; font-weight: 600;
}
.xsell-cta-link:hover { text-decoration: underline; }

/* ── FOOTER ── */
footer { background: #0f172a; color: #64748b; padding: 24px 20px; text-align: center; font-size: 13px; margin-top: 32px; }
footer a { color: #94a3b8; text-decoration: none; }
footer a:hover { color: #fff; }
</style>
</head>
<body>

<!-- SAMPLE DATA BANNER -->
<div class="sample-banner">
  ⚠️ <strong>SAMPLE / REPRESENTATIVE DATA ONLY</strong> — These permits are illustrative, not sourced from any county portal.
  Real-feed seam: <code>scripts/ingest-stub.js</code>
</div>

<!-- NAV -->
<nav>
  <a class="logo" href="/"><span>📡</span> Permit Radar</a>
  <div class="nav-links">
    <a href="/">Directory</a>
    <a href="#get-alerts">Get Alerts</a>
    <a href="/admin">Admin</a>
  </div>
</nav>

<!-- HERO -->
<div class="hero">
  <h1>Residential Remodel Permits — Live</h1>
  <p>High-intent homeowner leads at the moment they start their renovation. Reach them first.</p>
  <a href="#get-alerts" class="hero-cta">Get Permit Alerts in Your Area →</a>
</div>

<!-- CONTROLS (sort + density + filters) -->
<div class="controls">
  <div class="search-box">
    <input id="search" type="text" placeholder="Search address, city, contractor, type…">
  </div>
  <div class="filter-row">
    <select id="sort">
      <option value="date_desc">Sort: Newest Filed</option>
      <option value="date_asc">Oldest Filed</option>
      <option value="valuation_desc">Valuation High→Low</option>
      <option value="valuation_asc">Valuation Low→High</option>
      <option value="city">City A→Z</option>
      <option value="type">Permit Type</option>
      <option value="address">Address A→Z</option>
    </select>
    <select id="filt-jurisdiction">
      <option value="">All Jurisdictions</option>
      <option value="los angeles county">Los Angeles County</option>
      <option value="santa clara county">Santa Clara County</option>
      <option value="san diego county">San Diego County</option>
      <option value="alameda county">Alameda County</option>
      <option value="maricopa county">Maricopa County</option>
      <option value="coconino county">Coconino County</option>
      <option value="cook county">Cook County</option>
      <option value="travis county">Travis County</option>
      <option value="williamson county">Williamson County</option>
      <option value="dallas county">Dallas County</option>
      <option value="multnomah county">Multnomah County</option>
      <option value="king county">King County</option>
      <option value="fulton county">Fulton County</option>
      <option value="suffolk county">Suffolk County</option>
      <option value="middlesex county">Middlesex County</option>
      <option value="broward county">Broward County</option>
      <option value="miami-dade county">Miami-Dade County</option>
      <option value="davidson county">Davidson County</option>
    </select>
    <select id="filt-type">
      <option value="">All Types</option>
      <option value="kitchen_remodel">Kitchen Remodel</option>
      <option value="bath_remodel">Bathroom Remodel</option>
      <option value="addition">Room Addition</option>
      <option value="full_remodel">Full Remodel</option>
      <option value="adu">ADU</option>
      <option value="reroof">Reroof</option>
    </select>
    <select id="filt-status">
      <option value="">All Statuses</option>
      <option value="approved">Approved</option>
      <option value="pending">Pending</option>
    </select>
    <span class="ctrl-label">Density <input id="density" type="range" min="1" max="6" value="3" style="width:70px"></span>
  </div>
  <span class="count-badge" id="count">… permits</span>
</div>

<!-- PERMIT GRID -->
<div class="grid-wrap">
  <div class="grid" id="grid"><div class="empty">Loading permits…</div></div>
</div>

<!-- SHOPIFY CROSS-SELL STRIP -->
<div class="xsell-strip" id="xsell-strip">
  <div class="xsell-header">
    <span class="xsell-label">Trade cross-sell — links to the live DW store</span>
    <p class="xsell-title">Outfit the remodel — shop Designer Wallcoverings</p>
    <span class="xsell-subtext">No checkout here — opens designerwallcoverings.com</span>
  </div>
  <div class="xsell-grid" id="xsell-grid">
    <div style="color:#94a3b8;font-size:13px;padding:16px 0">Loading…</div>
  </div>
  <a class="xsell-cta-link" href="https://www.designerwallcoverings.com" target="_blank" rel="noopener noreferrer">
    Browse full collection at designerwallcoverings.com →
  </a>
</div>

<!-- LEAD FUNNEL SECTION -->
<div class="funnel-section" id="get-alerts">
  <h2>Turn Permits Into Revenue</h2>
  <p>Trades, designers, and home-goods vendors — get alerts the moment a high-value permit is filed in your area.</p>

  <div class="plan-cards">
    <div class="plan-card">
      <div class="plan-name">Basic</div>
      <div class="plan-price">$79<span>/mo</span></div>
      <ul class="plan-features">
        <li>Up to 3 jurisdictions</li>
        <li>Weekly digest emails</li>
        <li>1 permit type</li>
        <li>Permits &gt;$25K valuation</li>
      </ul>
    </div>
    <div class="plan-card featured">
      <div class="plan-badge">MOST POPULAR</div>
      <div class="plan-name">Pro</div>
      <div class="plan-price">$199<span>/mo</span></div>
      <ul class="plan-features">
        <li>Unlimited jurisdictions</li>
        <li>Real-time email alerts</li>
        <li>All permit types</li>
        <li>All valuations</li>
        <li>Contractor contacts</li>
      </ul>
    </div>
    <div class="plan-card">
      <div class="plan-name">Team</div>
      <div class="plan-price">$449<span>/mo</span></div>
      <ul class="plan-features">
        <li>Everything in Pro</li>
        <li>5 team seats</li>
        <li>CRM/CSV export</li>
        <li>DW Trade referrals</li>
        <li>Priority support</li>
      </ul>
    </div>
  </div>

  <div class="funnel-form">
    <h3>Get Permit Alerts</h3>
    <form id="subscribe-form" onsubmit="submitSubscribe(event)">
      <div class="form-row">
        <div>
          <label>Trade Type *</label>
          <select name="trade_type" required>
            <option value="">Select…</option>
            <option value="general_contractor">General Contractor</option>
            <option value="kitchen_bath">Kitchen &amp; Bath Designer</option>
            <option value="interior_designer">Interior Designer</option>
            <option value="roofing">Roofing Contractor</option>
            <option value="plumber">Plumber</option>
            <option value="electrician">Electrician</option>
            <option value="flooring">Flooring Contractor</option>
            <option value="hvac">HVAC</option>
            <option value="home_goods_vendor">Home Goods Vendor</option>
            <option value="wallcovering_vendor">Wallcovering Vendor</option>
            <option value="real_estate">Real Estate Agent</option>
            <option value="other">Other</option>
          </select>
        </div>
        <div>
          <label>Plan</label>
          <select name="plan">
            <option value="basic">Basic — $79/mo</option>
            <option value="pro" selected>Pro — $199/mo</option>
            <option value="team">Team — $449/mo</option>
          </select>
        </div>
      </div>
      <div class="form-row">
        <div>
          <label>Email *</label>
          <input type="email" name="email" placeholder="you@company.com" required>
        </div>
        <div>
          <label>Service Area / City</label>
          <input type="text" name="service_area" placeholder="e.g. Los Angeles, CA">
        </div>
      </div>
      <button type="submit" class="btn-subscribe">Start Getting Leads →</button>
      <div class="form-note">No charge yet — this prototype doesn't process payment. We'll contact you to onboard.</div>
      <div id="sub-msg"></div>
    </form>
  </div>
</div>

<!-- LEAD REQUEST MODAL -->
<div class="modal-bg" id="lead-modal" onclick="if(event.target===this)closeLead()">
  <div class="modal">
    <button class="modal-close" onclick="closeLead()">×</button>
    <h3>Request This Lead</h3>
    <p id="modal-permit-label">Permit details…</p>
    <form id="lead-form" onsubmit="submitLead(event)">
      <input type="hidden" id="modal-permit-id" name="permit_id">
      <div class="form-row">
        <div>
          <label>Your Name</label>
          <input type="text" name="name" placeholder="Jane Smith">
        </div>
        <div>
          <label>Trade Type *</label>
          <select name="trade_type" required>
            <option value="">Select…</option>
            <option value="general_contractor">General Contractor</option>
            <option value="kitchen_bath">Kitchen &amp; Bath</option>
            <option value="interior_designer">Interior Designer</option>
            <option value="roofing">Roofer</option>
            <option value="flooring">Flooring</option>
            <option value="hvac">HVAC</option>
            <option value="home_goods_vendor">Home Goods Vendor</option>
            <option value="wallcovering_vendor">Wallcovering Vendor</option>
            <option value="other">Other</option>
          </select>
        </div>
      </div>
      <div class="form-row">
        <div>
          <label>Email *</label>
          <input type="email" name="email" required placeholder="you@company.com">
        </div>
        <div>
          <label>Phone</label>
          <input type="tel" name="phone" placeholder="555-555-5555">
        </div>
      </div>
      <div class="form-row single">
        <div>
          <label>Message</label>
          <input type="text" name="message" placeholder="What services are you offering?">
        </div>
      </div>
      <button type="submit" class="btn-subscribe">Request This Lead →</button>
      <div id="lead-msg"></div>
    </form>
  </div>
</div>

<footer>
  <p>© 2024 Permit Radar — <a href="#get-alerts">Get Alerts</a> · <a href="/admin">Admin</a></p>
  <p style="margin-top:6px;font-size:11px">⚠️ Sample data only — not sourced from any county permit portal. Real-feed seam: <code>scripts/ingest-stub.js</code></p>
</footer>

<script>
'use strict';
const esc = s => (s||'').replace(/[&<>"]/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c]));
const fmtVal = v => v ? '$' + Number(v).toLocaleString() : '—';
const fmtDate = d => d ? new Date(d + 'T12:00:00').toLocaleDateString('en-US', { month:'short', day:'numeric', year:'numeric' }) : '—';

// ── Persist controls ──────────────────────────────────────────────────────────
const sortSel = document.getElementById('sort');
const dens    = document.getElementById('density');
const search  = document.getElementById('search');
const filtJ   = document.getElementById('filt-jurisdiction');
const filtT   = document.getElementById('filt-type');
const filtS   = document.getElementById('filt-status');

sortSel.value = localStorage.getItem('pr_sort') || 'date_desc';
dens.value    = localStorage.getItem('pr_cols') || '3';

function applyCols() {
  document.documentElement.style.setProperty('--cols', dens.value);
  localStorage.setItem('pr_cols', dens.value);
}
applyCols();

// ── Fetch + render ────────────────────────────────────────────────────────────
let debounce;
async function load() {
  const params = new URLSearchParams({
    sort: sortSel.value,
    q: search.value,
    jurisdiction: filtJ.value,
    permit_type: filtT.value,
    status: filtS.value,
  });
  const r = await fetch('/api/permits?' + params);
  const d = await r.json();
  document.getElementById('count').textContent = d.count + ' permits';
  const grid = document.getElementById('grid');
  if (!d.permits.length) {
    grid.innerHTML = '<div class="empty">No permits match your filters.</div>';
    return;
  }
  grid.innerHTML = d.permits.map(p => {
    const statusClass = 'status-' + (p.status || 'pending');
    const contractor = p.contractor ? `<span class="meta-item"><strong>Contractor:</strong> ${esc(p.contractor)}</span>` : '';
    return `
<div class="card">
  <div class="card-header">
    <div>
      <div class="permit-id">${esc(p.id)}</div>
      <span class="type-chip">${esc(p.permit_type_label || p.permit_type)}</span>
    </div>
    <span class="status-chip ${statusClass}">${esc(p.status)}</span>
  </div>
  <div class="card-body">
    <p class="card-address">${esc(p.address)}</p>
    <p class="card-city">${esc(p.city)}, ${esc(p.state)} ${esc(p.zip)} — ${esc(p.jurisdiction)}</p>
    <div class="card-meta">
      <span class="meta-item"><strong>Filed:</strong> ${fmtDate(p.date_filed)}</span>
      <span class="valuation">${fmtVal(p.valuation)}</span>
      ${contractor}
    </div>
    <div class="card-desc">${esc(p.description)}</div>
  </div>
  <div class="card-footer">
    <a class="btn-detail" href="/permit/${esc(p.id)}">View Details</a>
    <button class="btn-lead" onclick='openLead(${JSON.stringify(p.id)}, ${JSON.stringify(p.address + ", " + p.city)})'>Request Lead</button>
  </div>
</div>`;
  }).join('');
}

sortSel.onchange = () => { localStorage.setItem('pr_sort', sortSel.value); load(); };
dens.oninput = applyCols;
search.oninput = () => { clearTimeout(debounce); debounce = setTimeout(load, 300); };
filtJ.onchange = filtT.onchange = filtS.onchange = load;

load();

// ── Subscribe form ────────────────────────────────────────────────────────────
async function submitSubscribe(e) {
  e.preventDefault();
  const form = e.target;
  const body = Object.fromEntries(new FormData(form));
  const msg = document.getElementById('sub-msg');
  msg.innerHTML = '';
  try {
    const r = await fetch('/api/subscribe', {
      method: 'POST', headers: {'Content-Type':'application/json'},
      body: JSON.stringify(body)
    });
    const d = await r.json();
    if (d.ok) {
      msg.innerHTML = '<div class="success-msg">✓ Subscribed! ID: ' + esc(d.id) + '<br>We\'ll be in touch to complete your onboarding.</div>';
      form.reset();
    } else {
      msg.innerHTML = '<div class="error-msg">Error: ' + esc(d.error || 'Unknown error') + '</div>';
    }
  } catch (err) {
    msg.innerHTML = '<div class="error-msg">Network error — please try again.</div>';
  }
}

// ── Lead modal ────────────────────────────────────────────────────────────────
function openLead(id, label) {
  document.getElementById('modal-permit-id').value = id;
  document.getElementById('modal-permit-label').textContent = label;
  document.getElementById('lead-msg').innerHTML = '';
  document.getElementById('lead-form').reset();
  document.getElementById('modal-permit-id').value = id; // re-set after reset
  document.getElementById('lead-modal').classList.add('open');
}
function closeLead() {
  document.getElementById('lead-modal').classList.remove('open');
}

async function submitLead(e) {
  e.preventDefault();
  const form = e.target;
  const body = Object.fromEntries(new FormData(form));
  const msg = document.getElementById('lead-msg');
  msg.innerHTML = '';
  try {
    const r = await fetch('/api/lead-request', {
      method: 'POST', headers: {'Content-Type':'application/json'},
      body: JSON.stringify(body)
    });
    const d = await r.json();
    if (d.ok) {
      msg.innerHTML = '<div class="success-msg">✓ Lead request submitted! ID: ' + esc(d.id) + '</div>';
      setTimeout(closeLead, 2500);
    } else {
      msg.innerHTML = '<div class="error-msg">Error: ' + esc(d.error) + '</div>';
    }
  } catch (err) {
    msg.innerHTML = '<div class="error-msg">Network error.</div>';
  }
}

// ── Shopify cross-sell strip ──────────────────────────────────────────────────
(async function loadXsell() {
  const grid = document.getElementById('xsell-grid');
  if (!grid) return;
  try {
    const r = await fetch('/api/shopify-crosssell');
    const d = await r.json();
    if (!d.products || !d.products.length) { grid.innerHTML = ''; return; }
    grid.innerHTML = d.products.map(p => `
<a class="xsell-card" href="${esc(p.url)}" target="_blank" rel="noopener noreferrer"
   title="${esc(p.title)}">
  <div class="xsell-img-wrap">
    <img src="${esc(p.image)}" alt="${esc(p.title)}" loading="lazy">
  </div>
  <div class="xsell-info">
    <div class="xsell-product-title">${esc(p.title)}</div>
    <div class="xsell-vendor">${esc(p.vendor)}</div>
    <div class="xsell-price">${esc(p.price_display || '')}</div>
  </div>
</a>`).join('');
  } catch (err) {
    grid.innerHTML = '';
  }
})();
</script>
</body>
</html>