import { fetchAllBookings } from "../api"; import type { Booking } from "../types"; import { formatCurrency, formatDateTime } from "../utils"; import { renderBookingStatusBadge, renderCalendarSyncBadge, renderPaymentStatusBadge, } from "./StatusBadge"; export function renderAllBookings(container: HTMLElement): void { container.innerHTML = `

Historial General de Reservaciones

Registro completo de citas, cancelaciones y liberaciones de espacio.

`; const contentDiv = container.querySelector( "#all-bookings-content", ); const statusSelect = container.querySelector("#status-filter"); const refreshBtn = container.querySelector("#refresh-all-btn"); let cachedBookings: Booking[] = []; const loadData = async () => { if (!contentDiv) return; contentDiv.innerHTML = `
`; try { cachedBookings = await fetchAllBookings(); applyFilter(); } catch (err: unknown) { contentDiv.innerHTML = `
⚠️

Error al cargar reservaciones

${err instanceof Error ? err.message : "Error desconocido"}

`; } }; const applyFilter = () => { if (!contentDiv) return; const filter = statusSelect?.value || "ALL"; const filtered = filter === "ALL" ? cachedBookings : cachedBookings.filter((b) => b.booking_status === filter); if (filtered.length === 0) { contentDiv.innerHTML = `
🔍

Sin resultados

No se encontraron reservaciones con el filtro seleccionado.

`; return; } contentDiv.innerHTML = `
${filtered .map( (b) => ` `, ) .join("")}
Folio Paciente Servicio Horario Monto Estado Pago Google Cal
${b.booking_code}
${b.client_name || "Cliente"}
📱 ${b.client_phone || ""}
${b.service_name || "Consulta"} ${formatDateTime(b.scheduled_at)} ${formatCurrency(b.amount_mxn)} ${renderBookingStatusBadge(b.booking_status)} ${renderPaymentStatusBadge(b.payment_status, b.payment_arrangement)} ${renderCalendarSyncBadge(b.gcal_sync_status, b.id)}
`; }; statusSelect?.addEventListener("change", () => applyFilter()); refreshBtn?.addEventListener("click", () => loadData()); loadData(); }