refactor: 简化各页面模板,移除重复代码;添加plan_common.js
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// 方案相关公共函数(currentPlanId 在各页面自行声明)
|
||||
|
||||
// 查看方案详情
|
||||
async function viewPlan(planId) {
|
||||
currentPlanId = planId;
|
||||
const response = await fetch(`/api/plans/${planId}`);
|
||||
const data = await response.json();
|
||||
|
||||
let html = `
|
||||
<div class="mb-3">
|
||||
<strong>学员:</strong>${data.student_name}
|
||||
<strong>练习时间:</strong>${data.content.practice_time}
|
||||
<strong>生成时间:</strong>${data.created_at}
|
||||
</div>
|
||||
<h6>问题诊断</h6>
|
||||
<div class="mb-3">
|
||||
`;
|
||||
|
||||
data.content.problems.forEach(p => {
|
||||
html += `<span class="problem-tag severity-${p.severity}">${p.name}(${p.severity})</span> `;
|
||||
});
|
||||
|
||||
html += `</div>`;
|
||||
|
||||
if (data.content.ai_report) {
|
||||
const aiReportHtml = marked.parse(data.content.ai_report);
|
||||
html += `
|
||||
<h6>AI个性化练习报告</h6>
|
||||
<div class="mb-3 p-3 bg-light rounded" style="max-height: 500px; overflow-y: auto;">${aiReportHtml}</div>
|
||||
`;
|
||||
} else if (data.content.ai_report_error) {
|
||||
html += `
|
||||
<h6>AI报告</h6>
|
||||
<div class="mb-3 p-3 bg-warning rounded">AI生成失败: ${data.content.ai_report_error}</div>
|
||||
`;
|
||||
}
|
||||
|
||||
html += `
|
||||
<h6>每日练习计划(共${data.content.total_daily_minutes}分钟)</h6>
|
||||
<table class="table table-sm">
|
||||
<thead><tr><th>环节</th><th>时长</th><th>内容</th><th>目的</th></tr></thead>
|
||||
<tbody>
|
||||
`;
|
||||
|
||||
data.content.daily_schedule.forEach(item => {
|
||||
html += `<tr><td>${item.phase}</td><td>${item.duration}</td><td>${item.content}</td><td>${item.purpose}</td></tr>`;
|
||||
});
|
||||
|
||||
html += '</tbody></table>';
|
||||
|
||||
document.getElementById('planDetailContent').innerHTML = html;
|
||||
new bootstrap.Modal(document.getElementById('planDetailModal')).show();
|
||||
}
|
||||
|
||||
// 下载PDF
|
||||
function downloadPDF() {
|
||||
const templateId = document.getElementById('reportTemplateSelect')?.value;
|
||||
const url = templateId ? `/api/plans/${currentPlanId}/pdf?template_id=${templateId}` : `/api/plans/${currentPlanId}/pdf`;
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
|
||||
// 下载MD
|
||||
function downloadMD() {
|
||||
const templateId = document.getElementById('reportTemplateSelect')?.value;
|
||||
const url = templateId ? `/api/plans/${currentPlanId}/md?template_id=${templateId}` : `/api/plans/${currentPlanId}/md`;
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
|
||||
// 别名(兼容旧代码)
|
||||
const downloadPlanPDF = downloadPDF;
|
||||
const downloadPlanMD = downloadMD;
|
||||
|
||||
// 预览报告模板
|
||||
async function previewReportTemplate() {
|
||||
if (!currentPlanId) {
|
||||
alert('请先选择一个方案');
|
||||
return;
|
||||
}
|
||||
const templateId = document.getElementById('reportTemplateSelect')?.value;
|
||||
const url = templateId ? `/api/plans/${currentPlanId}/md?template_id=${templateId}` : `/api/plans/${currentPlanId}/md`;
|
||||
try {
|
||||
const resp = await fetch(url);
|
||||
if (resp.ok) {
|
||||
const md = await resp.text();
|
||||
document.getElementById('reportPreviewContent').innerHTML = marked.parse(md);
|
||||
new bootstrap.Modal(document.getElementById('reportPreviewModal')).show();
|
||||
} else {
|
||||
alert('预览失败');
|
||||
}
|
||||
} catch (e) {
|
||||
alert('预览失败: ' + e.message);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user