// Contact page — temple info, phones, email, contact form
function ContactPage({ t }) {
  const lang = t.locale;
  const [form, setForm] = useState({ name: "", email: "", subject: "", message: "" });
  const [sent, setSent] = useState(false);

  const labelsByLang = {
    th: { name: "ชื่อของท่าน", email: "อีเมล", subject: "หัวข้อ", message: "ข้อความ", send: "ส่งข้อความ", thanks: "ขอขอบพระคุณ ทางวัดจะติดต่อกลับโดยเร็วที่สุด" },
    no: { name: "Navn", email: "E-post", subject: "Emne", message: "Melding", send: "Send melding", thanks: "Takk for henvendelsen. Vi tar kontakt så snart vi kan." },
    en: { name: "Your name", email: "Email", subject: "Subject", message: "Message", send: "Send message", thanks: "Thank you for your message. The temple will contact you as soon as possible." },
  };
  const copyByLang = {
    th: {
      intro: "ท่านสามารถติดต่อวัดได้ในเวลาเปิดทำการ หรือฝากข้อความผ่านแบบฟอร์มด้านล่าง",
      title: "ติดต่อวัดไทยนอร์เวย์",
      eyebrow: "ติดต่อ",
      address: "ที่อยู่",
      directions: "ดูเส้นทาง",
      phone: "โทรศัพท์",
      officeMain: "สำนักงานวัด (หลัก)",
      officeAlt: "สำนักงานวัด (สำรอง)",
      mobile: "มือถือ",
      mobileAlt: "มือถือ (สำรอง)",
      fax: "โทรสาร",
      faxNote: "โทรสาร",
      emailWebsite: "อีเมล / เว็บไซต์",
      general: "สอบถามทั่วไป",
      official: "เว็บไซต์ทางการ",
      follow: "ตามเรา",
      email: "อีเมล",
      website: "เว็บไซต์",
      mailSubject: "ติดต่อจากเว็บไซต์วัด",
      formEyebrow: "ส่งข้อความ",
      formTitle: "ฝากคำถามหรือข้อความถึงวัด",
      sentHint: "✓ เปิดแอปอีเมลให้แล้ว · กรุณากดส่งในแอปเพื่อส่งถึงวัด",
      formHint: "กดส่งแล้วแอปอีเมลจะเปิดขึ้นพร้อมข้อความ · ทางวัดตอบกลับภายใน ๒–๓ วันทำการ",
      hours: "เวลาทำการ",
    },
    no: {
      intro: "Velkommen til å kontakte oss i åpningstiden, eller send oss en melding via skjemaet under.",
      title: "Kontakt Wat Thai Norge",
      eyebrow: "Kontakt",
      address: "Adresse",
      directions: "Veibeskrivelse",
      phone: "Telefon",
      officeMain: "Tempelkontor (hoved)",
      officeAlt: "Tempelkontor (alternativ)",
      mobile: "Mobil",
      mobileAlt: "Mobil (alternativ)",
      fax: "Telefaks",
      faxNote: "Faks",
      emailWebsite: "E-post / nettside",
      general: "Generelle henvendelser",
      official: "Offisiell nettside",
      follow: "Følg oss",
      email: "E-post",
      website: "Nettside",
      mailSubject: "Henvendelse fra tempelnettsiden",
      formEyebrow: "Send melding",
      formTitle: "Har du et spørsmål eller en hilsen til tempelet?",
      sentHint: "✓ E-postprogrammet åpnes · trykk send der for å fullføre",
      formHint: "Trykk send, så åpnes e-postprogrammet med meldingen · Tempelet svarer normalt innen 2-3 virkedager.",
      hours: "Åpningstider",
    },
    en: {
      intro: "You are welcome to contact the temple during opening hours, or send us a message using the form below.",
      title: "Contact Wat Thai Norway",
      eyebrow: "Contact",
      address: "Address",
      directions: "Get directions",
      phone: "Phone",
      officeMain: "Temple office (main)",
      officeAlt: "Temple office (alternate)",
      mobile: "Mobile",
      mobileAlt: "Mobile (alternate)",
      fax: "Fax",
      faxNote: "Fax",
      emailWebsite: "Email / website",
      general: "General enquiries",
      official: "Official website",
      follow: "Follow us",
      email: "Email",
      website: "Website",
      mailSubject: "Message from the temple website",
      formEyebrow: "Send a message",
      formTitle: "Leave a question or message for the temple",
      sentHint: "✓ Your email app has opened · press send there to contact the temple",
      formHint: "Press send and your email app will open with the message · The temple normally replies within 2-3 business days.",
      hours: "Opening hours",
    },
  };
  const labels = labelsByLang[lang] || labelsByLang.th;
  const copy = copyByLang[lang] || copyByLang.th;
  const community = window.WTN_COMMUNITY || {};
  const addressLines = (community.address && (community.address[lang] || community.address.en)) || ["Wat Thai Norway", "Trondheimsvegen 582", "2016 Frogner, Norway"];
  const mapsUrl = community.maps || "https://www.google.com/maps/search/?api=1&query=Wat+Thai+Norway+Trondheimsvegen+582+2016+Frogner";
  const phones = community.phones || { display: { office: "+47 63 82 01 03", officeAlt: "+47 63 82 01 30", mobile: "+47 91 74 66 85", mobileAlt: "+47 93 42 03 87" } };

  return (
    <div>
      <div className="wn-container">
        <PageHeading eyebrow={copy.eyebrow} title={copy.title} intro={copy.intro} />
      </div>

      <section style={{ paddingTop: "40px", paddingBottom: "120px" }}>
        <div className="wn-container wn-r2" style={{ display: "grid", gridTemplateColumns: "1fr 1.2fr", gap: "64px", alignItems: "start" }}>
          {/* Contact details */}
          <div style={{ display: "flex", flexDirection: "column", gap: "24px" }}>
            <ContactCard
              label={copy.address}
              lines={addressLines}
              action={{ label: copy.directions, href: mapsUrl }}
            />
            <ContactCard
              label={copy.phone}
              lines={[
                { tel: phones.display.office, note: copy.officeMain },
                { tel: phones.display.officeAlt, note: copy.officeAlt },
                { tel: phones.display.mobile, note: copy.mobile },
                { tel: phones.display.mobileAlt, note: copy.mobileAlt },
              ]}
            />
            <ContactCard
              label={copy.fax}
              lines={[{ tel: "+47 63 82 01 31", note: copy.faxNote }]}
            />
            <ContactCard
              label={copy.emailWebsite}
              lines={[
                { tel: "info@watthainorway.com", note: copy.general, href: "mailto:info@watthainorway.com" },
                { tel: "watthainorway.com", note: copy.official, href: "https://watthainorway.com" },
              ]}
            />
            <div style={{ marginTop: "8px", padding: "20px 24px", border: "1px solid var(--line)", background: "var(--bg-deep)" }}>
              <div style={{ fontFamily: "var(--font-body)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--accent)", marginBottom: "12px" }}>
                {copy.follow}
              </div>
              <div style={{ display: "flex", gap: "10px", flexWrap: "wrap" }}>
                {[
                  { id: "f", label: "Facebook", href: (window.WTN_COMMUNITY || {}).facebook || "https://www.facebook.com/watthainorway" },
                  { id: "✉", label: "Messenger", href: (window.WTN_COMMUNITY || {}).messenger || "https://m.me/watthainorway" },
                  { id: "W", label: "WhatsApp", href: (window.WTN_COMMUNITY || {}).whatsapp || "https://wa.me/4791746685" },
                  { id: "▶", label: "YouTube", href: community.youtube || "https://www.youtube.com/@watthainorway" },
                  { id: "@", label: copy.email, href: "mailto:" + ((window.WTN_COMMUNITY || {}).email || "info@watthainorway.com") },
                ].map((s) => (
                  <a key={s.id} href={s.href} target="_blank" rel="noopener noreferrer" style={{ minWidth: "44px", height: "44px", padding: "0 12px", borderRadius: "999px", border: "1px solid var(--line)", background: "var(--bg)", display: "flex", alignItems: "center", justifyContent: "center", textDecoration: "none", color: "var(--ink)", fontFamily: "var(--font-body)", fontSize: "13px" }} title={s.label}>
                    {s.id}
                  </a>
                ))}
              </div>
              <p style={{ fontFamily: "var(--font-body)", fontSize: "13px", lineHeight: 1.6, color: "var(--muted)", marginTop: "14px", marginBottom: 0 }}>
                {lang === "th" ? "ข่าวสดมักประกาศบน Facebook ก่อน — ทัก Messenger หรือ WhatsApp ได้สะดวก" : lang === "no" ? "Nyheter kommer ofte først på Facebook — Messenger og WhatsApp er raskeste vei." : "News often appears on Facebook first — Messenger and WhatsApp are the quickest channels."}
              </p>
            </div>
          </div>

          {/* Form */}
          <form
            onSubmit={(e) => {
              e.preventDefault();
              const to = "info@watthainorway.com";
              const subject = encodeURIComponent(form.subject || copy.mailSubject);
              const body = encodeURIComponent(`${form.message}\n\n— ${form.name}${form.email ? " <" + form.email + ">" : ""}`);
              window.location.href = `mailto:${to}?subject=${subject}&body=${body}`;
              setSent(true);
            }}
            style={{ border: "1px solid var(--line)", padding: "48px", background: "var(--bg)" }}
            className="wn-pad-card"
          >
            <div style={{ fontFamily: "var(--font-body)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--accent)", marginBottom: "16px" }}>
              {copy.formEyebrow}
            </div>
            <h2 style={{ fontFamily: "var(--font-display)", fontSize: "32px", fontWeight: 400, color: "var(--ink)", margin: "0 0 32px", lineHeight: 1.2 }}>
              {copy.formTitle}
            </h2>

            <div className="wn-r2" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "20px" }}>
              <Field label={labels.name} value={form.name} onChange={(v) => setForm({ ...form, name: v })} required />
              <Field label={labels.email} type="email" value={form.email} onChange={(v) => setForm({ ...form, email: v })} required />
            </div>
            <div style={{ marginTop: "20px" }}>
              <Field label={labels.subject} value={form.subject} onChange={(v) => setForm({ ...form, subject: v })} />
            </div>
            <div style={{ marginTop: "20px" }}>
              <Field label={labels.message} value={form.message} onChange={(v) => setForm({ ...form, message: v })} multiline required />
            </div>

            <div className="wn-wrap" style={{ marginTop: "32px", paddingTop: "24px", borderTop: "1px solid var(--line)", display: "flex", justifyContent: "space-between", alignItems: "center", gap: "16px" }}>
              <div style={{ fontFamily: "var(--font-body)", fontSize: "12px", color: "var(--muted)", lineHeight: 1.5 }}>
                {sent ? copy.sentHint : copy.formHint}
              </div>
              <button type="submit" style={{ background: "var(--ink)", color: "var(--bg)", border: "none", padding: "16px 28px", borderRadius: "999px", fontFamily: "var(--font-body)", fontSize: "14px", letterSpacing: "0.04em", cursor: "pointer", display: "inline-flex", alignItems: "center", gap: "10px", whiteSpace: "nowrap" }}>
                <span style={{ width: "6px", height: "6px", borderRadius: "50%", background: "var(--accent)" }} />
                {sent ? "✓" : labels.send}
              </button>
            </div>
          </form>
        </div>
      </section>

      {/* Hours strip */}
      <section style={{ paddingTop: "60px", paddingBottom: "60px", backgroundColor: "var(--bg-deep)" }}>
        <div className="wn-container wn-r-today" style={{ display: "grid", gridTemplateColumns: "auto 1fr 1fr 1fr", gap: "48px", alignItems: "center", padding: "32px 0", borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)" }}>
          <div style={{ fontFamily: "var(--font-body)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--muted)" }}>
            {copy.hours}
          </div>
          {t.visit.hours.map((h, i) => (
            <div key={i}>
              <div style={{ fontFamily: "var(--font-body)", fontSize: "12px", letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--accent)", marginBottom: "6px" }}>
                {h.day}
              </div>
              <div style={{ fontFamily: "var(--font-display)", fontSize: "22px", color: "var(--ink)" }}>
                {h.time}
              </div>
            </div>
          ))}
        </div>
      </section>
    </div>
  );
}

function ContactCard({ label, lines, action }) {
  return (
    <div style={{ borderTop: "1px solid var(--line)", paddingTop: "24px" }}>
      <div style={{ fontFamily: "var(--font-body)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--accent)", marginBottom: "12px" }}>
        {label}
      </div>
      {lines.map((l, i) => {
        if (typeof l === "string") {
          return (
            <div key={i} style={{ fontFamily: "var(--font-body)", fontSize: "16px", lineHeight: 1.6, color: "var(--ink)" }}>
              {l}
            </div>
          );
        }
        const content = (
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: "12px", padding: "10px 0", borderBottom: "1px solid var(--line)" }}>
            <div style={{ minWidth: 0 }}>
              <div style={{ fontFamily: "var(--font-display)", fontSize: "20px", lineHeight: 1.35, color: "var(--ink)", fontVariantNumeric: "tabular-nums", overflowWrap: "anywhere" }}>{l.tel}</div>
              <div style={{ fontFamily: "var(--font-body)", fontSize: "12px", color: "var(--muted)", marginTop: "4px" }}>{l.note}</div>
            </div>
            <span style={{ color: "var(--accent)", fontSize: "18px", flexShrink: 0 }}>→</span>
          </div>
        );
        const href = l.href || (/^\+?[\d\s]+$/.test(l.tel || "") ? "tel:" + l.tel.replace(/\s/g, "") : null);
        return href
          ? <a key={i} href={href} style={{ textDecoration: "none", display: "block" }}>{content}</a>
          : <div key={i}>{content}</div>;
      })}
      {action && (
        <a href={action.href} target="_blank" rel="noopener noreferrer" style={{ display: "inline-block", marginTop: "16px", background: "transparent", color: "var(--ink)", border: "1px solid var(--ink)", padding: "10px 18px", borderRadius: "999px", fontFamily: "var(--font-body)", fontSize: "12px", letterSpacing: "0.04em", cursor: "pointer", textDecoration: "none" }}>
          {action.label} →
        </a>
      )}
    </div>
  );
}

function Field({ label, value, onChange, type = "text", multiline = false, required = false }) {
  const Tag = multiline ? "textarea" : "input";
  return (
    <label style={{ display: "block" }}>
      <div style={{ fontFamily: "var(--font-body)", fontSize: "11px", letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--muted)", marginBottom: "8px" }}>
        {label}{required ? " *" : ""}
      </div>
      <Tag
        type={type}
        value={value}
        required={required}
        onChange={(e) => onChange(e.target.value)}
        rows={multiline ? 5 : undefined}
        style={{
          width: "100%",
          background: "transparent",
          border: "none",
          borderBottom: "1px solid var(--line)",
          padding: "10px 0",
          fontFamily: "var(--font-body)",
          fontSize: "16px",
          color: "var(--ink)",
          outline: "none",
          resize: multiline ? "vertical" : undefined,
        }}
        onFocus={(e) => (e.target.style.borderBottomColor = "var(--accent)")}
        onBlur={(e) => (e.target.style.borderBottomColor = "var(--line)")}
      />
    </label>
  );
}

window.ContactPage = ContactPage;
