import { qs } from "../modules/dom.js"; import { apiFetch } from "../modules/api.js"; import { showToast, setServiceStatus } from "../modules/ui.js"; import { renderRobots } from "./robotCards.js"; import { store } from "../modules/store.js"; import { loadDashboard } from "./dashboard.js"; export async function loadRobots() { try { const result = await apiFetch("/api/robots"); renderRobots(result, store.dashboardData?.detection); setServiceStatus(true); } catch (error) { setServiceStatus(false); showToast(`无法读取机器人状态:${error.message}`, "error"); } } export async function testRobot(channel, button) { const originalText = button.textContent; button.disabled = true; button.textContent = "正在发送"; try { const result = await apiFetch(`/api/robots/${encodeURIComponent(channel)}/test`, { method: "POST" }); showToast(`${result.robot.name}测试消息发送成功`, "success"); await Promise.all([loadRobots(), loadDashboard()]); } catch (error) { showToast(`测试消息发送失败:${error.message}`, "error"); } finally { button.disabled = false; button.textContent = originalText; } } export function initRobots() { qs("#refreshRobotsButton").addEventListener("click", loadRobots); qs("#robotCardGrid").addEventListener("click", (event) => { const button = event.target.closest("[data-test-robot]"); if (button) testRobot(button.dataset.testRobot, button); }); }