/* Survey sau học phần: học viên/agent gửi feedback, nhận sitemap để ôn lại đúng bài. */ (function () { function ModuleFeedbackPanel({ moduleData, visible }) { const R = React; const [rating, setRating] = R.useState(5); const [clarity, setClarity] = R.useState(5); const [support, setSupport] = R.useState(false); const [stuck, setStuck] = R.useState([]); const [text, setText] = R.useState(""); const [sent, setSent] = R.useState(false); const [busy, setBusy] = R.useState(false); if (!visible || !moduleData) return null; const scope = () => { const raw = (localStorage.getItem("ag_token") || localStorage.getItem("token") || "anon").slice(-24); let h = 0; for (let i = 0; i < raw.length; i += 1) h = ((h << 5) - h + raw.charCodeAt(i)) | 0; return String(Math.abs(h)); }; const key = "module_feedback_" + moduleData.id + "_" + scope(); if (!sent && localStorage.getItem(key) === "1") return null; const lessons = moduleData.lessons || []; const toggle = (no) => setStuck((a) => a.indexOf(no) >= 0 ? a.filter((x) => x !== no) : a.concat([no])); const payload = () => ({ module_id: moduleData.id, rating, clarity, stuck_lessons: stuck, support_needed: support, feedback: text, agent_summary: "Học viên phản hồi sau học phần " + moduleData.name }); const submit = async () => { setBusy(true); try { const res = await (window.AG && window.AG.moduleFeedback ? window.AG.moduleFeedback(payload()) : Promise.resolve({ ok: false })); if (res && res.ok) { localStorage.setItem(key, "1"); setSent(true); } else if (window.showToast) window.showToast((res && res.msg) || "Chưa gửi được feedback, thử lại sau."); } catch (e) { if (window.showToast) window.showToast("Mạng đang lỗi, feedback chưa gửi. Bạn thử lại sau nhé."); } finally { setBusy(false); } }; const agentPrompt = () => { const map = lessons.map((l) => "- Bài " + l.no + ": " + l.title + " — " + l.url).join("\n"); return "Bạn là AI Agent học viên. Hãy hỏi tôi feedback sau học phần '" + moduleData.name + "': mức hiểu 1-5, chỗ kẹt, cần hỗ trợ gì. Nếu tôi kẹt, mở đúng link trong sitemap này để tôi xem lại:\n" + map + "\nSau đó gửi feedback về /agent/api/module-feedback bằng token học viên."; }; const copyPrompt = () => { try { navigator.clipboard.writeText(agentPrompt()); } catch (e) {} if (window.showToast) window.showToast("Đã copy prompt feedback cho AI Agent."); }; if (sent) return