function renderDoctorCard(doctor) { const fields = (doctor.fields || []).join("、"); const card = document.createElement("article"); card.className = "doctor-card"; card.setAttribute("role", "button"); card.setAttribute("tabindex", "0"); card.dataset.doctorId = doctor.id; card.innerHTML = `
${escapeHtml(doctor.name)}
${escapeHtml(doctor.name)} ${escapeHtml(doctor.role || "咨询师")}

${escapeHtml(doctor.hospital || "")}

专业领域${escapeHtml(fields)}

`; card.addEventListener("click", () => goConsult(doctor.id)); card.addEventListener("keydown", (e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); goConsult(doctor.id); } }); return card; } function escapeHtml(str) { const div = document.createElement("div"); div.textContent = str; return div.innerHTML; } function goConsult(doctorId) { const url = doctorId ? `consult.php?doctorId=${encodeURIComponent(doctorId)}` : "consult.php"; window.location.href = url; } async function initHome() { const allowed = await initAccessGuard(); if (!allowed) return; try { const config = await loadAppConfig(); document.title = config.siteTitle || "心理咨询"; } catch (e) { console.warn(e); } const listEl = document.getElementById("doctor-list"); const ctaBtn = document.getElementById("cta-book"); try { const doctors = await loadDoctors(); if (doctors.length === 0) { listEl.innerHTML = '

暂无咨询师信息

'; } else { doctors.forEach((d) => listEl.appendChild(renderDoctorCard(d))); } } catch (e) { listEl.innerHTML = '

加载失败,请刷新重试

'; showToast(e.message || "加载失败"); } ctaBtn.addEventListener("click", () => goConsult()); } document.addEventListener("DOMContentLoaded", initHome);