const { useState, useEffect } = React;

function Contact({ query }) {
  const { t, L, lang } = useT();
  const prefill = query && query.model ? query.model : "";
  const [sent, setSent] = useState(false);
  const [form, setForm] = useState({
    name: "",
    company: "",
    city: "",
    email: "",
    model: prefill,
    qty: "",
    message: "",
  });

  useEffect(() => {
    if (prefill) setForm((f) => ({ ...f, model: prefill }));
  }, [prefill]);

  const set = (k) => (e) => setForm({ ...form, [k]: e.target.value });
  const submit = (e) => {
    e.preventDefault();
    setSent(true);
  };

  const cities = ["Jakarta", "Surabaya", "Bandung", "Semarang", "Medan", "Denpasar", "Makassar", "Batam", "Lainnya"];

  if (sent) {
    return (
      <main>
        <PageHead crumbs={[{ label: t.nav.home, href: "/" }, { label: t.nav.contact }]} title={t.contact.title} />
        <section className="section" style={{ paddingTop: 40 }}>
          <div className="wrap" style={{ maxWidth: 760 }}>
            <div className="success">
              <span className="eyebrow">RFQ · {new Date().toLocaleDateString(lang === "id" ? "id-ID" : "en-GB")}</span>
              <h2>{t.contact.successTitle}</h2>
              <p>{t.contact.successBody}</p>
              <dl className="spec-table" style={{ width: "100%", marginTop: 10 }}>
                <div className="spec-row"><dt>{t.contact.name}</dt><dd>{form.name || "—"}</dd></div>
                <div className="spec-row"><dt>{t.contact.city}</dt><dd>{form.city || "—"}</dd></div>
                <div className="spec-row"><dt>{t.contact.product}</dt><dd>{form.model || t.contact.productAll}</dd></div>
                <div className="spec-row"><dt>{t.contact.qty}</dt><dd>{form.qty || "—"}</dd></div>
              </dl>
              <div style={{ display: "flex", gap: 10, flexWrap: "wrap", marginTop: 12 }}>
                <button className="btn btn--amber" onClick={() => setSent(false)}>{t.contact.another}</button>
                <a className="btn btn--ghost" href="#/katalog">{t.nav.catalog}</a>
              </div>
            </div>
          </div>
        </section>
      </main>
    );
  }

  return (
    <main>
      <PageHead
        crumbs={[{ label: t.nav.home, href: "/" }, { label: t.nav.contact }]}
        title={t.contact.title}
        sub={t.contact.lead}
      />
      <div className="wrap contact">
        <form className="form" onSubmit={submit}>
          <div className="form__row">
            <div className="field">
              <label htmlFor="f-name">{t.contact.name}</label>
              <input id="f-name" required value={form.name} onChange={set("name")} placeholder="Budi Santoso" />
            </div>
            <div className="field">
              <label htmlFor="f-company">{t.contact.company}</label>
              <input id="f-company" value={form.company} onChange={set("company")} placeholder="PT Taman Asri" />
            </div>
          </div>
          <div className="form__row">
            <div className="field">
              <label htmlFor="f-city">{t.contact.city}</label>
              <select id="f-city" value={form.city} onChange={set("city")}>
                <option value="">—</option>
                {cities.map((c) => <option key={c} value={c}>{c}</option>)}
              </select>
            </div>
            <div className="field">
              <label htmlFor="f-email">{t.contact.email}</label>
              <input id="f-email" required value={form.email} onChange={set("email")} placeholder="+62 812 0000 0000" />
            </div>
          </div>
          <div className="form__row">
            <div className="field">
              <label htmlFor="f-model">{t.contact.product}</label>
              <select id="f-model" value={form.model} onChange={set("model")}>
                <option value="">{t.contact.productAll}</option>
                {window.PRODUCTS.map((p) => (
                  <option key={p.id} value={p.id}>{p.id} · {L(p.name)}</option>
                ))}
              </select>
            </div>
            <div className="field">
              <label htmlFor="f-qty">{t.contact.qty}</label>
              <input id="f-qty" type="number" min="1" value={form.qty} onChange={set("qty")} placeholder="20" />
            </div>
          </div>
          <div className="field">
            <label htmlFor="f-msg">{t.contact.message}</label>
            <textarea id="f-msg" value={form.message} onChange={set("message")} placeholder={t.contact.messagePh}></textarea>
          </div>
          <div className="form__foot">
            <button className="btn btn--amber" type="submit">{t.contact.submit} <Icons.Arrow /></button>
            <span className="form__note">{t.product.priceNote}</span>
          </div>
        </form>

        <aside className="aside">
          <div className="aside__block">
            <div className="eyebrow">{t.contact.direct}</div>
            <b>WhatsApp</b>
            <a href="https://wa.me/8618066824092" target="_blank" rel="noopener noreferrer" style={{ fontSize: 12.5, color: "var(--muted)", lineHeight: 1.6, textDecoration: "none" }}>+86 180 6682 4092</a>
            <b style={{ marginTop: 8 }}>Email</b>
            <a href="mailto:bingbingone95@gmail.com" style={{ fontSize: 12.5, color: "var(--muted)", lineHeight: 1.6, textDecoration: "none" }}>bingbingone95@gmail.com</a>
            <span>{t.contact.hours}</span>
          </div>
          <div className="aside__block" style={{ borderTop: "1px solid var(--line)", paddingTop: 18 }}>
            <b>{t.contact.addressTitle}</b>
            <span>{t.contact.address}</span>
            <span>{t.contact.addressNote}</span>
          </div>
          <div className="aside__block" style={{ borderTop: "1px solid var(--line)", paddingTop: 18 }}>
            <b>{t.brand.cnFull}</b>
            <span>{t.brand.legalEn}</span>
            <span>{t.brand.market}</span>
          </div>
          <a className="btn btn--ghost btn--full" href="#/pengiriman">{t.nav.shipping} <Icons.Arrow /></a>
        </aside>
      </div>
    </main>
  );
}
window.Contact = Contact;
