import { ApiClient, type AdminBooking, type BookingStatus } from "@personal-brand/api-client"; export interface AdminDashboardOptions { client: ApiClient; root: HTMLElement } export class AdminDashboard { private bookings: AdminBooking[] = []; private selected: AdminBooking | undefined; constructor(private readonly options: AdminDashboardOptions) {} async load(): Promise { const { root, client } = this.options; const [bookings, unread, brand, services] = await Promise.all([client.listAdminBookings(), client.getAdminUnreadCount(), client.getAdminBrand(), client.listServices()]); this.bookings = bookings; root.innerHTML = `

品牌后台

未读订单:${unread.count}

品牌配置

服务配置

选择订单查看详情
`; root.querySelector("#refresh")?.addEventListener("click", () => void this.load()); root.querySelector("#status")?.addEventListener("change", () => void this.filter()); root.querySelector("#save-brand")?.addEventListener("click", () => void this.saveBrand()); root.querySelectorAll("[data-service-save]").forEach((item) => item.addEventListener("click", () => void this.saveService(item.dataset.serviceSave))); root.querySelectorAll("[data-id]").forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id))); } private listHtml(): string { return this.bookings.map((item) => `
  • `).join("") || "
  • 暂无订单
  • "; } private async filter(): Promise { const status = (this.options.root.querySelector("#status") as HTMLSelectElement).value as BookingStatus | ""; this.bookings = await this.options.client.listAdminBookings(status ? { status } : {}); const list = this.options.root.querySelector("#bookings"); if (list) list.innerHTML = this.listHtml(); } private select(id: string | undefined): void { this.selected = this.bookings.find((item) => item.id === id); if (!this.selected) return; const detail = this.options.root.querySelector("#detail"); if (!detail) return; detail.innerHTML = `

    ${this.selected.bookingNo}

    客户:${this.selected.customerName}

    `; detail.querySelector("#save")?.addEventListener("click", () => void this.save()); } private async save(): Promise { if (!this.selected) return; const detail = this.options.root.querySelector("#detail")!; const status = (detail.querySelector("#detail-status") as HTMLSelectElement).value as BookingStatus; const note = (detail.querySelector("#note") as HTMLTextAreaElement).value; this.selected = await this.options.client.updateAdminBookingStatus(this.selected.id, status, note); await this.load(); } private async saveBrand(): Promise { const root = this.options.root; await this.options.client.updateAdminBrand({ name: (root.querySelector("#brand-name") as HTMLInputElement).value, description: (root.querySelector("#brand-description") as HTMLTextAreaElement).value, contactPhone: (root.querySelector("#brand-phone") as HTMLInputElement).value, contactWechat: (root.querySelector("#brand-wechat") as HTMLInputElement).value }); } private async saveService(id: string | undefined): Promise { if (!id) return; const root = this.options.root; await this.options.client.updateAdminService(id, { name: (root.querySelector(`[data-service-name="${id}"]`) as HTMLInputElement).value, priceText: (root.querySelector(`[data-service-price="${id}"]`) as HTMLInputElement).value }); } } export const adminAppName = "personal-brand-admin"; export function bootstrapAdmin(root: HTMLElement, baseUrl = "http://localhost:3001"): AdminDashboard { const token = localStorage.getItem("adminToken"); const client = new ApiClient({ baseUrl, fetcher: (input, init) => fetch(input, init), ...(token ? { token } : {}) }); const dashboard = new AdminDashboard({ client, root }); void dashboard.load(); return dashboard; }