"use client"; import React, { useCallback, useEffect, useState } from "react"; import Link from "next/link"; import { useParams } from "next/navigation"; import { ArrowLeft, Copy, KeyRound } from "lucide-react"; import { api, errorText } from "@/lib/api"; import { ago, fmtDate } from "@/lib/format"; import { PLANS } from "@/lib/tiers"; import type { Customer, Licence, LicenceStatus } from "@/lib/types"; import { Badge, Loading, Message, PageTitle } from "@/components/ui"; type Msg = { type: "ok" | "err"; text: string } | null; const dateInput = (d: string | null) => (d ? d.slice(0, 10) : ""); /** One licence: its key, and the settings that go into the next token the install receives. */ function LicenceCard({ licence, onSaved, setMsg }: { licence: Licence; onSaved: () => void; setMsg: (m: Msg) => void }) { const [form, setForm] = useState({ status: licence.status, plan: licence.plan, expiresAt: dateInput(licence.expiresAt), graceDays: String(licence.graceDays), aiRand: String(licence.aiMonthlyCapCents / 100), aiMarkup: licence.aiMarkupPercent == null ? "" : String(licence.aiMarkupPercent), notes: licence.notes || "", }); const [busy, setBusy] = useState(false); const save = async () => { if (form.status !== licence.status && form.status !== "active" && !confirm(`${form.status === "revoked" ? "Revoke" : "Suspend"} this licence? The site becomes read-only after its next heartbeat (tickets still scan, data can still be exported).`)) return; setBusy(true); try { await api(`/api/admin/licences/${licence.id}`, { method: "PUT", body: { status: form.status, plan: form.plan, // End of the chosen day, South African time. expiresAt: form.expiresAt ? `${form.expiresAt}T23:59:59+02:00` : null, graceDays: form.graceDays, aiMonthlyCapCents: Math.round((parseFloat(form.aiRand) || 0) * 100), aiMarkupPercent: form.aiMarkup.trim() === "" ? null : form.aiMarkup, notes: form.notes, }, }); setMsg({ type: "ok", text: "Licence saved. The site picks it up with its next heartbeat (within 15 minutes)." }); onSaved(); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } finally { setBusy(false); } }; const release = async () => { if (!confirm("Release this licence from its site? The site's heartbeats are refused from now on, and the key can be registered again from a new address.")) return; try { await api(`/api/admin/licences/${licence.id}/release`, { method: "POST" }); setMsg({ type: "ok", text: "Released." }); onSaved(); } catch (e) { setMsg({ type: "err", text: errorText(e) }); } }; return (
{licence.key}
{licence.install ? <>Used by {licence.install.url} · last heartbeat {ago(licence.install.lastHeartbeatAt)} : "Not used yet — enter this key in the church's Cross Events setup (or Site settings → Licence)."}

Shown on the church's Licence page. Founding discount: note it below.

setForm({ ...form, expiresAt: e.target.value })} />

{form.expiresAt ? "" : "No end date"}

setForm({ ...form, graceDays: e.target.value })} />
setForm({ ...form, aiRand: e.target.value })} />

0 = off (no AI buttons). Counted in charged rand.

setForm({ ...form, aiMarkup: e.target.value })} />

Blank uses the default (Settings → AI).

setForm({ ...form, notes: e.target.value })} placeholder="e.g. founding church, paid annually, invoice #123" />
{licence.install && } Created {fmtDate(licence.createdAt, false)}
); } export default function CustomerPage() { const { id } = useParams<{ id: string }>(); const [c, setC] = useState(null); const [form, setForm] = useState({ name: "", contactName: "", email: "", phone: "", notes: "" }); const [newLicence, setNewLicence] = useState({ expiresAt: "", graceDays: "14" }); const [msg, setMsg] = useState(null); const load = useCallback(() => api(`/api/admin/customers/${id}`).then(x => { setC(x); setForm({ name: x.name, contactName: x.contactName || "", email: x.email || "", phone: x.phone || "", notes: x.notes || "" }); }).catch(e => setMsg({ type: "err", text: errorText(e) })), [id]); useEffect(() => { load(); }, [load]); if (!c) return msg ? : ; const saveDetails = async (e: React.FormEvent) => { e.preventDefault(); try { await api(`/api/admin/customers/${id}`, { method: "PUT", body: form }); setMsg({ type: "ok", text: "Saved." }); load(); } catch (err) { setMsg({ type: "err", text: errorText(err) }); } }; const generate = async (e: React.FormEvent) => { e.preventDefault(); try { const l = await api(`/api/admin/customers/${id}/licences`, { method: "POST", body: { expiresAt: newLicence.expiresAt ? `${newLicence.expiresAt}T23:59:59+02:00` : null, graceDays: newLicence.graceDays }, }); setMsg({ type: "ok", text: `Licence ${l.key} created. Send it to the church for their setup.` }); load(); } catch (err) { setMsg({ type: "err", text: errorText(err) }); } }; return ( <> Customers

Details

setForm({ ...form, name: e.target.value })} required />
setForm({ ...form, contactName: e.target.value })} />
setForm({ ...form, email: e.target.value })} />
setForm({ ...form, phone: e.target.value })} />