"use client";
import React, { useState } from "react";
import { api, errorText } from "@/lib/api";
import { centsFrom, day, money, randInput } from "@/lib/billing";
import { FormError, Modal } from "@/components/ui";
import { Field, Fields, ModalButtons, ReadCard } from "./ReadCard";
import { ADDRESS_OPTIONS, changed, type AddressType, type CustomerBillingData, type Done } from "./shared";
/** Whether the included domain is due for renewal within 30 days. */
const renewalDue = (b: CustomerBillingData) => b.addressType === "included" && !!b.domainRenewsOn && b.domainRenewsOn <= new Date(Date.now() + 30 * 864e5).toISOString().slice(0, 10);
/**
* The church's address: one for all its products, billed once per invoice and never discounted.
* The popup is opened from here or from the Address tile in the header, so the page holds it.
*/
export function AddressCard({ b, onEdit, onDone }: { b: CustomerBillingData; onEdit: () => void; onDone: Done }) {
const renewed = async () => {
if (!confirm(`Mark ${b.domain || "the domain"} as renewed? The renewal date moves on a year.`)) return;
try {
const r = await api<{ message: string }>(`/api/admin/billing/customers/${b.id}/domain-renewed`, { method: "POST" });
onDone({ type: "ok", text: r.message });
} catch (err) { onDone({ type: "err", text: errorText(err) }); }
};
return (
Renewed : null}>
{ADDRESS_OPTIONS[b.addressType].label}{b.domain}{b.addressMonthlyCents ? `${money(b.addressMonthlyCents)} / month` : "Free"}{b.addressCustomCents != null ? " (custom)" : ""}
{b.addressType === "included" && {b.domainRenewsOn && day(b.domainRenewsOn)}}
{renewalDue(b) &&
{b.domain || "The domain"} is due for renewal on {day(b.domainRenewsOn)}.