Files
piano-plan/app/templates/templates.html
T

281 lines
11 KiB
HTML

{% extends "base.html" %}
{% block title %}模板管理 - 钢琴练习方案系统{% endblock %}
{% block page_css %}
<style>
.template-card { cursor: pointer; transition: all 0.2s; }
.template-card:hover { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
.template-card.active { border-color: #667eea; background: #f8f9ff; }
.preview-pane { background: white; border-radius: 8px; padding: 20px; min-height: 300px; }
.preview-content { white-space: pre-wrap; font-family: monospace; font-size: 13px; line-height: 1.6; }
</style>
{% endblock %}
{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
<h4><i class="bi bi-file-earmark-text"></i> 模板管理</h4>
<button class="btn btn-primary" onclick="showCreateModal()">
<i class="bi bi-plus"></i> 新建模板
</button>
</div>
<div class="alert alert-info">
<i class="bi bi-info-circle"></i>
模板管理用于维护AI提示词和报告导出格式。只有管理员可以修改。
可用变量在模板中用 <code>{变量名}</code> 格式使用。
</div>
<div class="row">
<div class="col-md-4">
<div class="card">
<div class="card-header">
<h6 class="mb-0"><i class="bi bi-list"></i> 模板列表</h6>
</div>
<div class="card-body p-0" id="templateList"></div>
</div>
</div>
<div class="col-md-8">
<div class="card" id="templateEditor" style="display:none;">
<div class="card-header d-flex justify-content-between align-items-center">
<h6 class="mb-0" id="editorTitle">编辑模板</h6>
<div>
<button class="btn btn-sm btn-success" onclick="saveTemplate()">
<i class="bi bi-save"></i> 保存
</button>
<button class="btn btn-sm btn-outline-danger" onclick="deleteCurrentTemplate()">
<i class="bi bi-trash"></i> 删除
</button>
</div>
</div>
<div class="card-body">
<div class="mb-3">
<label class="form-label">模板名称</label>
<input type="text" class="form-control" id="templateName">
</div>
<div class="mb-3">
<label class="form-label">模板类型</label>
<select class="form-select" id="templateType" disabled>
<option value="ai_prompt">AI提示词模板</option>
<option value="report">报告导出模板</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">模板描述</label>
<input type="text" class="form-control" id="templateDesc" placeholder="模板用途说明">
</div>
<div class="mb-3">
<label class="form-label">排序</label>
<input type="number" class="form-control" id="templateSortOrder" value="0" min="0">
<small class="text-muted">数字越小排越前</small>
</div>
<div class="mb-3">
<label class="form-label">模板内容</label>
<textarea id="templateContent"></textarea>
</div>
</div>
</div>
<div class="text-muted text-center mt-5" id="emptyHint">
<i class="bi bi-arrow-left"></i> 请从左侧选择一个模板进行编辑
</div>
</div>
</div>
<!-- 创建模板弹窗 -->
<div class="modal fade" id="createModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">新建模板</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">模板名称</label>
<input type="text" class="form-control" id="newTemplateName" placeholder="输入模板名称">
</div>
<div class="mb-3">
<label class="form-label">模板类型</label>
<select class="form-select" id="newTemplateType">
<option value="ai_prompt">AI提示词模板</option>
<option value="report">报告导出模板</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">模板描述</label>
<input type="text" class="form-control" id="newTemplateDesc" placeholder="模板用途说明">
</div>
<div class="mb-3">
<label class="form-label">排序</label>
<input type="number" class="form-control" id="newTemplateSortOrder" value="0" min="0">
<small class="text-muted">数字越小排越前</small>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="button" class="btn btn-primary" onclick="createTemplate()">创建</button>
</div>
</div>
</div>
</div>
{% endblock %}
{% block extra_js %}
<script>
let templates = [];
let currentTemplate = null;
let editor = null;
let createModal;
window.pageInit = function() {
createModal = new bootstrap.Modal(document.getElementById('createModal'));
loadTemplates();
}
async function loadTemplates() {
const resp = await fetch('/templates/templates');
templates = await resp.json();
renderTemplateList();
}
function renderTemplateList() {
const list = document.getElementById('templateList');
if (templates.length === 0) {
list.innerHTML = '<div class="p-3 text-muted">暂无模板</div>';
return;
}
// 按类型分组
const groups = {};
templates.forEach(t => {
if (!groups[t.type]) groups[t.type] = [];
groups[t.type].push(t);
});
const typeNames = { 'ai_prompt': 'AI提示词模板', 'report': '报告导出模板' };
let html = '';
Object.keys(groups).forEach(type => {
html += `<div class="mb-3">
<h6 class="text-muted border-bottom pb-1 mb-2">${typeNames[type] || type}</h6>`;
groups[type].forEach(t => {
html += `
<div class="template-card card m-2 p-2 ${currentTemplate && currentTemplate.id === t.id ? 'active' : ''}"
onclick="selectTemplate(${t.id})">
<div class="d-flex justify-content-between align-items-center">
<div>
<strong>${t.name}</strong>
<br><small class="text-muted">${t.description || ''}</small>
</div>
<i class="bi bi-chevron-right"></i>
</div>
</div>`;
});
html += '</div>';
});
list.innerHTML = html;
}
async function selectTemplate(id) {
currentTemplate = templates.find(t => t.id === id);
if (!currentTemplate) return;
document.getElementById('templateEditor').style.display = 'block';
document.getElementById('emptyHint').style.display = 'none';
document.getElementById('templateName').value = currentTemplate.name;
document.getElementById('templateType').value = currentTemplate.type;
document.getElementById('templateDesc').value = currentTemplate.description || '';
document.getElementById('templateSortOrder').value = currentTemplate.sort_order || 0;
if (editor) {
editor.toTextArea();
editor = null;
}
editor = new EasyMDE({
element: document.getElementById('templateContent'),
spellChecker: false,
status: false,
toolbar: ['bold', 'italic', 'heading', '|', 'code', 'quote', 'unordered-list', '|', 'side-by-side', 'fullscreen'],
initialValue: currentTemplate.content
});
renderTemplateList();
}
function showCreateModal() {
document.getElementById('newTemplateName').value = '';
document.getElementById('newTemplateType').value = 'ai_prompt';
document.getElementById('newTemplateDesc').value = '';
document.getElementById('newTemplateSortOrder').value = '0';
createModal.show();
}
async function createTemplate() {
const name = document.getElementById('newTemplateName').value.trim();
const type = document.getElementById('newTemplateType').value;
const desc = document.getElementById('newTemplateDesc').value.trim();
const sort_order = parseInt(document.getElementById('newTemplateSortOrder').value) || 0;
if (!name) { alert('请输入模板名称'); return; }
const resp = await fetch('/templates/templates', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name, type, content: '', description: desc, sort_order})
});
if (resp.ok) {
createModal.hide();
await loadTemplates();
} else {
alert('创建失败');
}
}
async function saveTemplate() {
if (!currentTemplate) return;
const name = document.getElementById('templateName').value.trim();
const content = editor ? editor.value() : document.getElementById('templateContent').value;
const description = document.getElementById('templateDesc').value;
const sort_order = parseInt(document.getElementById('templateSortOrder').value) || 0;
if (!name) { alert('请输入模板名称'); return; }
const resp = await fetch(`/templates/templates/${currentTemplate.id}`, {
method: 'PUT',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name, content, description, sort_order})
});
if (resp.ok) {
currentTemplate.name = name;
currentTemplate.content = content;
currentTemplate.description = description;
currentTemplate.sort_order = sort_order;
alert('保存成功');
renderTemplateList();
} else {
alert('保存失败');
}
}
async function deleteCurrentTemplate() {
if (!currentTemplate) return;
if (!confirm('确定要删除这个模板吗?')) return;
const resp = await fetch(`/templates/templates/${currentTemplate.id}`, {method: 'DELETE'});
if (resp.ok) {
currentTemplate = null;
document.getElementById('templateEditor').style.display = 'none';
document.getElementById('emptyHint').style.display = 'block';
if (editor) { editor.toTextArea(); editor = null; }
await loadTemplates();
} else {
alert('删除失败');
}
}
</script>
{% endblock %}