// event.jsx — Egna landningssidor för varje event (sandared.se/<event-id>)
// Renderas av app.jsx när URL:ens path matchar ett id i SIGNUP_EVENTS.
// Återanvänder submitSignup, saveReminder och DarkField från signup.jsx.

// ─── Anmälningskort (forest-box med formulär/påminnelse/tack) ──────────
// Fristående och återanvändbart — samma flöde som DesktopAnmal/MobileAnmal,
// men låst till ett enda event och anpassat för en egen sida.
function EventSignupBox({ ev, compact }) {
  const T = SITE_TOKENS;
  const [type, setType]         = React.useState('foretag');
  const [submitted, setSubmitted] = React.useState(false);
  const [remEmail, setRemEmail]   = React.useState('');
  const [remThanks, setRemThanks] = React.useState(false);

  const pad = compact ? 22 : 40;

  return (
    <div id="anmal-form" style={{ background: T.forest, color: T.cream, borderRadius: compact ? 20 : 28, padding: pad, position: 'relative', overflow: 'hidden', scrollMarginTop: 90 }}>
      <div style={{ position: 'absolute', top: -90, right: -90, width: 260, height: 260, borderRadius: '50%', background: ev.accent, opacity: 0.18 }}/>

      {!ev.signupOpen ? (
        <div style={{ position: 'relative', textAlign: 'center', padding: compact ? '8px 0' : '24px 0' }}>
          <div style={{ width: 64, height: 64, borderRadius: '50%', background: 'rgba(255,255,255,0.12)', margin: '0 auto 22px', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke={T.sun} strokeWidth="2" strokeLinecap="round">
              <circle cx="12" cy="12" r="10"/><path d="M12 6 V12 L16 14"/>
            </svg>
          </div>
          <div className="eyebrow" style={{ color: T.sun, marginBottom: 12 }}>Kommer snart</div>
          <h3 style={{ fontSize: compact ? 22 : 30, color: T.cream }}>Anmälan till {ev.title} öppnar senare.</h3>
          <p style={{ fontSize: compact ? 14 : 15, color: T.cream + 'cc', marginTop: 14, maxWidth: 440, margin: '14px auto 0', lineHeight: 1.55 }}>
            {ev.opensAt || 'Anmälan öppnar längre fram.'} Lämna er e-post nedan så får ni en påminnelse så fort vi öppnar.
          </p>
          {remThanks ? (
            <div style={{ marginTop: 24, display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 10, padding: '14px 22px', borderRadius: 14, background: 'rgba(255,255,255,0.12)', maxWidth: 440, marginLeft: 'auto', marginRight: 'auto' }}>
              <svg width="18" height="18" viewBox="0 0 24 24"><path d="M5 13 L10 18 L20 7" stroke={T.sun} strokeWidth="2.5" fill="none" strokeLinecap="round"/></svg>
              <span style={{ fontSize: 14, color: T.cream, fontWeight: 600 }}>Tack! Vi hör av oss när anmälan öppnar.</span>
            </div>
          ) : (
            <div style={{ display: 'flex', gap: 8, marginTop: 24, maxWidth: 440, marginLeft: 'auto', marginRight: 'auto' }}>
              <input
                className="dark-field" placeholder="din@epost.se" type="email" value={remEmail}
                onChange={e => setRemEmail(e.target.value)}
                style={{ flex: 1, padding: '14px 16px', borderRadius: 12, border: 0, background: 'rgba(255,255,255,0.08)', color: T.cream, fontSize: 14, fontFamily: 'inherit', outline: 'none', boxShadow: `inset 0 0 0 1.5px ${T.cream}22` }}
              />
              <button
                className="btn btn-primary" style={{ background: ev.accent, color: '#fff', padding: '0 22px', fontSize: 14, flexShrink: 0 }}
                onClick={async () => { if (!remEmail.trim()) return; try { await saveReminder(ev.id, remEmail.trim()); } catch (e) {} setRemThanks(true); }}
              >Påminn mig</button>
            </div>
          )}
        </div>
      ) : submitted ? (
        <div style={{ position: 'relative', textAlign: 'center', padding: compact ? '20px 0' : '40px 0' }}>
          <div style={{ width: 64, height: 64, borderRadius: '50%', background: ev.accent, margin: '0 auto 22px', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <svg width="32" height="32" viewBox="0 0 24 24"><path d="M5 13 L10 18 L20 7" stroke="#fff" strokeWidth="3" fill="none" strokeLinecap="round"/></svg>
          </div>
          <h3 style={{ fontSize: compact ? 22 : 28, color: T.cream }}>Tack, vi har fått er anmälan!</h3>
          <p style={{ fontSize: 15, color: T.cream + 'cc', marginTop: 14, maxWidth: 400, margin: '14px auto 0', lineHeight: 1.55 }}>
            Vi skickar en bekräftelse till er e-post inom kort. Vi återkommer med detaljer senast två veckor innan eventet.
          </p>
          <button onClick={() => setSubmitted(false)} className="btn btn-secondary" style={{ marginTop: 22 }}>Anmäl ytterligare en</button>
        </div>
      ) : (
        <form style={{ position: 'relative' }} onSubmit={(e) => {
          e.preventDefault();
          submitSignup(e.target, ev, type)
            .then(() => setSubmitted(true))
            .catch(() => alert('Något gick fel när anmälan skulle skickas. Försök igen.'));
        }}>
          <div className="eyebrow" style={{ color: T.sun, marginBottom: 10 }}>Anmäl er till {ev.title}</div>
          <h3 style={{ fontSize: compact ? 22 : 30, color: T.cream }}>Tre minuter, sen är ni med.</h3>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8, marginTop: 18 }}>
            {[['foretag','Företag'],['forening','Förening']].map(([id, label]) => {
              const active = type === id;
              return (
                <button type="button" key={id} onClick={() => setType(id)} style={{ padding: compact ? '10px 12px' : '14px 18px', borderRadius: 12, background: active ? T.cream : 'transparent', color: active ? T.ink : T.cream, boxShadow: active ? 'none' : `inset 0 0 0 1.5px ${T.cream}44`, fontWeight: 600, fontSize: compact ? 13 : 14, textAlign: 'left', display: 'flex', alignItems: 'center', gap: 10 }}>
                  <div style={{ width: 18, height: 18, borderRadius: '50%', background: active ? ev.accent : 'transparent', boxShadow: active ? 'none' : `inset 0 0 0 1.5px ${T.cream}88`, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                    {active && <div style={{ width: 7, height: 7, background: '#fff', borderRadius: '50%' }}/>}
                  </div>
                  {label}
                </button>
              );
            })}
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 14, marginTop: 16 }}>
            <DarkField name="namn"    required label={type === 'foretag' ? 'Företagsnamn' : 'Föreningens namn'} placeholder={type === 'foretag' ? 'Sandareds AB' : 'Sandareds IF'} span={2}/>
            <DarkField name="kontakt" required label="Kontaktperson" placeholder="Anna Andersson" span={compact ? 2 : 1}/>
            <DarkField name="mobil"   required label="Mobilnummer" placeholder="070-123 45 67" type="tel" span={compact ? 2 : 1}/>
            <DarkField name="email"   required label="E-post" placeholder="anna@exempel.se" type="email" span={2}/>
            <DarkField name="plats"   label="Önskad plats" placeholder="T.ex. vid magasinet, skolan, sjön" span={2}/>
            <DarkField name="ovrigt"  label="Övrig information" placeholder="El, vatten, tält, bord, andra önskemål..." textarea span={2}/>
          </div>
          <div style={{ marginTop: 18, padding: '14px 16px', borderRadius: 12, background: 'rgba(0,0,0,0.18)', display: 'flex', alignItems: 'flex-start', gap: 12 }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={T.sun} strokeWidth="2" style={{ flexShrink: 0, marginTop: 2 }}>
              <circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="13"/><line x1="12" y1="16" x2="12" y2="16"/>
            </svg>
            <span style={{ fontSize: 12.5, color: T.cream + 'cc', lineHeight: 1.5 }}>
              När ni skickar får ni ett bekräftelsemejl direkt och vi för in er i vårt anmälningsregister. Vi delar aldrig era uppgifter.
            </span>
          </div>
          <button type="submit" className="btn btn-primary" style={{ width: '100%', justifyContent: 'center', marginTop: 16, background: ev.accent, padding: compact ? '15px 20px' : '18px 24px', fontSize: compact ? 15 : 16, color: '#fff' }}>
            Skicka anmälan →
          </button>
        </form>
      )}
    </div>
  );
}

// ─── Liten översikt över övriga event (länkar till deras egna sidor) ───
function OtherEventsStrip({ currentId, compact }) {
  const T = SITE_TOKENS;
  const others = SIGNUP_EVENTS.filter(e => e.id !== currentId);
  if (!others.length) return null;
  return (
    <div style={{ marginTop: compact ? 28 : 48 }}>
      <div className="eyebrow" style={{ marginBottom: 14 }}>Fler event att anmäla sig till</div>
      <div style={{ display: 'grid', gridTemplateColumns: compact ? '1fr' : 'repeat(auto-fit, minmax(260px, 1fr))', gap: 14 }}>
        {others.map(o => {
          const Illu = window[o.illu];
          return (
            <a key={o.id} href={'/' + o.id} className="card" style={{ display: 'flex', alignItems: 'center', gap: 16, padding: 16, textDecoration: 'none', color: T.ink }}>
              <div style={{ width: 64, height: 64, borderRadius: 14, background: o.tint, display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                {Illu && <Illu size={52}/>}
              </div>
              <div style={{ minWidth: 0, flex: 1 }}>
                <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: 17, fontWeight: 600, lineHeight: 1.2 }}>{o.title}</div>
                <div style={{ fontSize: 13, color: T.muted, marginTop: 3 }}>{o.date}</div>
              </div>
              <span style={{ color: o.accent, fontSize: 20, flexShrink: 0 }}>→</span>
            </a>
          );
        })}
      </div>
    </div>
  );
}

// ─── Topbar för eventsidan (logga + tillbaka till startsidan) ──────────
function EventTopBar({ compact }) {
  const T = SITE_TOKENS;
  return (
    <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: compact ? '12px 20px' : '20px 64px', borderBottom: `1px solid ${T.line}`, background: T.cream + 'EE', backdropFilter: 'blur(8px)', position: 'sticky', top: 0, zIndex: 50 }}>
      <a href="/"><Wordmark size={compact ? 30 : 38}/></a>
      <a href="/" style={{ display: 'inline-flex', alignItems: 'center', gap: 7, fontSize: compact ? 13 : 14, fontWeight: 600, color: T.ink2 }}>
        <span style={{ fontSize: 16 }}>←</span> {compact ? 'sandared.se' : 'Till sandared.se'}
      </a>
    </div>
  );
}

// ─── Kompakt footer för eventsidan ─────────────────────────────────────
function EventFooter({ compact }) {
  const T = SITE_TOKENS;
  return (
    <footer style={{ background: T.forestDk, color: T.cream, padding: compact ? '32px 22px' : '48px 64px' }}>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, justifyContent: 'space-between', alignItems: 'center' }}>
        <a href="/"><Wordmark size={compact ? 32 : 38} dark/></a>
        <div style={{ display: 'flex', gap: 20, flexWrap: 'wrap', fontSize: 13 }}>
          <a href="/" style={{ color: T.cream + 'cc' }}>Startsidan</a>
          <a href="/#kalender" style={{ color: T.cream + 'cc' }}>Kalender</a>
          <a href="mailto:info@sandared.se" style={{ color: T.cream + 'cc' }}>info@sandared.se</a>
          <a onClick={openPolicy} style={{ cursor: 'pointer', color: T.cream + 'cc' }}>Integritetspolicy</a>
        </div>
      </div>
      <div style={{ marginTop: 20, paddingTop: 18, borderTop: `1px solid ${T.cream}22`, fontSize: 12, color: T.cream + '88' }}>
        © 2026 Sandareds Intresseförening · Org.nr 802431-1857
      </div>
    </footer>
  );
}

// ─── Hela eventsidan ───────────────────────────────────────────────────
function EventPage({ ev, isMobile }) {
  const T = SITE_TOKENS;
  const Illu = window[ev.illu];
  const compact = isMobile;

  React.useEffect(() => {
    const prev = document.title;
    document.title = ev.title + ' · Sandareds Intresseförening';
    return () => { document.title = prev; };
  }, [ev.title]);

  const facts = [
    ['Datum', ev.date],
    ['Tid', ev.time],
    ['Plats', ev.place],
    ev.audience ? ['Besökare', ev.audience] : null,
  ].filter(Boolean);

  return (
    <div style={{ background: T.cream, minHeight: '100vh' }}>
      <EventTopBar compact={compact}/>

      {/* ── Hero ── */}
      <section style={{ position: 'relative', overflow: 'hidden', padding: compact ? '32px 22px 24px' : '70px 64px 60px' }}>
        <div style={{ position: 'absolute', top: -120, right: -120, width: 420, height: 420, borderRadius: '50%', background: `radial-gradient(circle at 30% 30%, ${ev.accent}44, transparent 70%)` }}/>
        <div style={{ position: 'relative', display: 'grid', gridTemplateColumns: compact ? '1fr' : '1.05fr 1fr', gap: compact ? 28 : 60, alignItems: 'center' }}>
          <div>
            <div className="chip" style={{ marginBottom: 18 }}>
              <span className="chip-dot" style={{ background: ev.signupOpen ? ev.accent : T.muted }}/>
              {ev.signupOpen ? 'Anmälan öppen' : (ev.opensAt || 'Anmälan öppnar snart')}
            </div>
            <h1 style={{ fontSize: compact ? 40 : 76, lineHeight: 0.98, fontWeight: 700, letterSpacing: '-0.035em' }}>
              {ev.title}
            </h1>
            <p style={{ fontSize: compact ? 16 : 20, color: T.ink2, marginTop: compact ? 16 : 26, maxWidth: 540, lineHeight: 1.5 }}>
              {ev.intro}
            </p>
            <div style={{ display: 'flex', gap: 12, marginTop: compact ? 22 : 34, flexWrap: 'wrap' }}>
              <a href="#anmal-form" className="btn btn-primary" style={{ padding: compact ? '15px 22px' : '18px 28px', fontSize: 16, background: ev.accent, color: '#fff' }}>
                {ev.signupOpen ? 'Anmäl er →' : 'Få en påminnelse →'}
              </a>
              <a href="/#kalender" className="btn btn-secondary" style={{ padding: compact ? '15px 22px' : '18px 28px', fontSize: 16 }}>Se hela kalendern</a>
            </div>
          </div>
          <div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
            <div style={{ width: compact ? 220 : 360, height: compact ? 220 : 360, borderRadius: '50%', background: ev.tint, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
              {Illu && <Illu size={compact ? 170 : 280}/>}
            </div>
          </div>
        </div>

        {/* Faktarad */}
        <div style={{ display: 'grid', gridTemplateColumns: compact ? '1fr 1fr' : `repeat(${facts.length + 1}, 1fr)`, gap: 14, marginTop: compact ? 26 : 44 }}>
          {facts.map(([label, value]) => (
            <div key={label} className="card" style={{ padding: compact ? 16 : 22 }}>
              <div className="eyebrow" style={{ fontSize: 10, marginBottom: 6 }}>{label}</div>
              <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: compact ? 17 : 20, fontWeight: 600, color: T.ink, lineHeight: 1.2 }}>{value}</div>
            </div>
          ))}
          <div className="card" style={{ padding: compact ? 16 : 22, background: ev.accent + '14', boxShadow: `inset 0 0 0 1.5px ${ev.accent}44` }}>
            <div className="eyebrow" style={{ fontSize: 10, marginBottom: 6, color: ev.accent }}>Deadline</div>
            <div style={{ fontFamily: 'Bricolage Grotesque', fontSize: compact ? 17 : 20, fontWeight: 700, color: ev.accent, lineHeight: 1.2 }}>{ev.deadline}</div>
          </div>
        </div>
      </section>

      {/* ── Om eventet + det här ingår ── */}
      <section style={{ padding: compact ? '8px 22px 32px' : '20px 64px 60px' }}>
        <div style={{ display: 'grid', gridTemplateColumns: compact ? '1fr' : '1fr 1fr', gap: compact ? 20 : 32, alignItems: 'start' }}>
          <div className="card" style={{ padding: compact ? 22 : 36 }}>
            <div className="eyebrow" style={{ marginBottom: 8, color: ev.accent }}>Om eventet</div>
            <h2 style={{ fontSize: compact ? 26 : 34 }}>Var med på {ev.title}</h2>
            <p style={{ fontSize: compact ? 15 : 17, color: T.ink2, marginTop: 16, lineHeight: 1.65 }}>
              Vi möts i <strong style={{ color: T.ink }}>{ev.place}</strong> mellan <strong style={{ color: T.ink }}>{ev.time}</strong>
              {ev.audience ? <> — förra året kom drygt {ev.audience.replace('+','')} besökare förbi.</> : '.'} En perfekt dag att möta nya kunder, värva medlemmar eller bara visa upp vad ni gör i Sandared.
            </p>
            <p style={{ fontSize: compact ? 14 : 15, color: T.ink2, marginTop: 14, lineHeight: 1.65 }}>
              Som anmäld förening eller företag får ni en egen yta att inreda som ni vill. Vi hjälper er med praktiska detaljer i ett uppstartsmejl närmare datumet.
            </p>
            {Array.isArray(ev.inkluderar) && ev.inkluderar.length > 0 && (
              <div style={{ marginTop: 22, paddingTop: 18, borderTop: `1px solid ${T.line}` }}>
                <div className="eyebrow" style={{ marginBottom: 12, fontSize: 11 }}>Det här ingår</div>
                {ev.inkluderar.map(it => (
                  <div key={it} style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 8 }}>
                    <div style={{ width: 18, height: 18, borderRadius: '50%', background: ev.accent + '22', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
                      <svg width="10" height="10" viewBox="0 0 12 12"><path d="M2 6 L5 9 L10 3" stroke={ev.accent} strokeWidth="2.5" fill="none" strokeLinecap="round"/></svg>
                    </div>
                    <span style={{ fontSize: 14, color: T.ink2 }}>{it}</span>
                  </div>
                ))}
              </div>
            )}
          </div>

          <EventSignupBox ev={ev} compact={compact}/>
        </div>

        <OtherEventsStrip currentId={ev.id} compact={compact}/>
      </section>

      <EventFooter compact={compact}/>
    </div>
  );
}

Object.assign(window, { EventPage, EventSignupBox });
