import { createResource, createSignal, Show, For, Accessor } from "solid-js"; import { apiClient } from "../lib/api-client"; import { Input } from "./ui/input"; import { useI18n } from "../providers/i18n-provider"; interface SMSSettingsData { enabled: boolean; senderName: string; monthlyLimit: number; messagesSent: number; totalCostCents: number; available: boolean; } interface SMSReport { yearMonth: string; messageCount: number; totalCostCents: number; stripeInvoiceId?: string; invoiceSentAt?: string; } interface SMSLog { id: string; recipientPhone: string; status: string; costCents: number; createdAt: string; } function formatCents(cents: number) { return `${(cents / 100).toFixed(2)} Kč`; } function formatMonth(yearMonth: string) { const [y, m] = yearMonth.split("-"); return `${m}/${y}`; } interface SMSSettingsProps { token: Accessor; } export function SMSSettings(props: SMSSettingsProps) { const i18n = useI18n(); const token = props.token; const [saving, setSaving] = createSignal(false); const [notice, setNotice] = createSignal(null); const [error, setError] = createSignal(null); const [settings, { refetch: refetchSettings }] = createResource(token, async (bearer) => { if (!bearer || bearer.startsWith("demo.")) { return { enabled: false, senderName: "", monthlyLimit: 0, messagesSent: 0, totalCostCents: 0, available: true, } as SMSSettingsData; } const response = await (apiClient as any).GET("/v1/sms/settings", { headers: { Authorization: `Bearer ${bearer}` }, }); return response.data as SMSSettingsData; }); const [reports] = createResource(token, async (bearer) => { if (!bearer || bearer.startsWith("demo.")) return [] as SMSReport[]; const response = await (apiClient as any).GET("/v1/sms/invoices", { headers: { Authorization: `Bearer ${bearer}` }, }); return (response.data as any)?.reports ?? []; }); const [logs] = createResource(token, async (bearer) => { if (!bearer || bearer.startsWith("demo.")) return [] as SMSLog[]; const response = await (apiClient as any).GET("/v1/sms/history", { headers: { Authorization: `Bearer ${bearer}` }, }); return (response.data as any)?.logs ?? []; }); const handleToggle = async () => { const current = settings(); if (!current) return; const bearer = token(); if (!bearer) return; setSaving(true); setError(null); setNotice(null); try { await (apiClient as any).POST("/v1/sms/settings", { headers: { Authorization: `Bearer ${bearer}` }, body: { enabled: !current.enabled, senderName: current.senderName, monthlyLimit: current.monthlyLimit, }, }); await refetchSettings(); setNotice( i18n.locale() === "cs" ? `SMS ${!current.enabled ? "aktivováno" : "deaktivováno"}` : `SMS ${!current.enabled ? "enabled" : "disabled"}` ); } catch (e: any) { setError( e?.response?.data?.error || (i18n.locale() === "cs" ? "Nepodařilo se uložit nastavení." : "Failed to save settings.") ); } finally { setSaving(false); } }; const handleSaveSettings = async (e: Event) => { e.preventDefault(); const current = settings(); if (!current) return; const bearer = token(); if (!bearer) return; setSaving(true); setError(null); setNotice(null); try { await (apiClient as any).POST("/v1/sms/settings", { headers: { Authorization: `Bearer ${bearer}` }, body: { enabled: current.enabled, senderName: current.senderName, monthlyLimit: current.monthlyLimit, }, }); await refetchSettings(); setNotice(i18n.locale() === "cs" ? "Nastavení uloženo." : "Settings saved."); } catch (e: any) { setError( e?.response?.data?.error || (i18n.locale() === "cs" ? "Nepodařilo se uložit nastavení." : "Failed to save settings.") ); } finally { setSaving(false); } }; const cs = () => i18n.locale() === "cs"; return (

{cs() ? "SMS zprávy" : "SMS Messages"}

{cs() ? "1.50 Kč / SMS. Fakturováno měsíčně přes Stripe." : "1.50 CZK / SMS. Billed monthly via Stripe."}

{cs() ? "Načítání..." : "Loading..."}
}>
{cs() ? "SMS není v této instanci nakonfigurováno. Kontaktujte administrátora." : "SMS is not configured for this instance. Contact your administrator."}
{notice()}
{error()}
{/* Toggle */}

{cs() ? "SMS odesílání" : "SMS sending"}

{settings()?.enabled ? (cs() ? "Aktivní — účtováno za každou zprávu" : "Active — charged per message") : (cs() ? "Neaktivní" : "Inactive")}

{ const s = settings(); if (s) s.senderName = e.currentTarget.value; }} maxLength={11} placeholder="Bookra" /> { const s = settings(); if (s) s.monthlyLimit = parseInt(e.currentTarget.value) || 0; }} min={0} />
{/* Current month stats */}

{cs() ? "Aktuální měsíc" : "Current month"}

{settings()?.messagesSent ?? 0}

{cs() ? "Odeslaných zpráv" : "Messages sent"}

{formatCents(settings()?.totalCostCents ?? 0)}

{cs() ? "Celková cena" : "Total cost"}

{/* Recent logs */} 0}>

{cs() ? "Historie odesílání" : "Send history"}

{(log) => (
{log.recipientPhone} {log.status}
{formatCents(log.costCents)}
)}
{/* Monthly invoice reports */} 0}>

{cs() ? "Fakturační přehledy" : "Invoice reports"}

{(report) => (
{formatMonth(report.yearMonth)} {report.messageCount} SMS
{formatCents(report.totalCostCents)} Stripe
)}
); }