<?php
/**
 * 课节管理模块 — 创建课节来匹配知岁OS的课节数据源
 */
$db = getDB();
$sections = $db->query("SELECT * FROM course_sections ORDER BY section_number")->fetchAll();
?>
<div class="toolbar">
    <div class="toolbar-title">课节管理 <span style="font-size:13px;color:#9ca3af;">(用于匹配知岁OS课节数据源)</span></div>
    <div class="toolbar-actions">
        <button class="btn-primary" onclick="showSectionModal()"><i class="fas fa-plus"></i> 新增课节</button>
    </div>
</div>

<div class="panel-card">
    <div style="padding:12px 0 8px;font-size:13px;color:#9ca3af;margin-bottom:16px;">
        课节时间配置决定了前端课表中的时间显示。当知岁OS导入课表数据时，课节的序号(start_section/end_section)将与此处配置的课节序号对应。请根据本校实际作息时间调整。
    </div>
    <table class="data-table">
        <thead>
            <tr>
                <th>序号</th>
                <th>课节名称</th>
                <th>开始时间</th>
                <th>结束时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($sections as $sec): ?>
            <tr>
                <td><?php echo $sec['section_number']; ?></td>
                <td><?php echo htmlspecialchars($sec['section_name']); ?></td>
                <td><?php echo $sec['start_time']; ?></td>
                <td><?php echo $sec['end_time']; ?></td>
                <td>
                    <div class="table-actions">
                        <button class="btn-sm btn-edit" onclick="editSection(<?php echo $sec['id']; ?>)">编辑</button>
                        <button class="btn-sm btn-del" onclick="deleteSection(<?php echo $sec['id']; ?>)">删除</button>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php if (empty($sections)): ?>
            <tr><td colspan="5" style="text-align:center;color:#9ca3af;padding:32px;">暂无课节数据，请先添加</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>

<script>
function showSectionModal(id, num, name, start, end) {
    id = id || 0;
    num = num || '';
    name = name || '';
    start = start || '';
    end = end || '';
    var isEdit = id > 0;

    var body = '<div class="form-group">' +
        '<label class="form-label">课节序号</label>' +
        '<input class="form-input" id="sec_num" type="number" value="' + num + '" placeholder="如：1 (对应知岁OS的start_section)">' +
        '<div class="form-hint">知岁OS课表数据中的节次编号与此对应</div>' +
        '</div>' +
        '<div class="form-group">' +
        '<label class="form-label">课节名称</label>' +
        '<input class="form-input" id="sec_name" value="' + name + '" placeholder="如：第1节">' +
        '</div>' +
        '<div class="form-group">' +
        '<label class="form-label">开始时间</label>' +
        '<input class="form-input" id="sec_start" type="time" value="' + start + '" step="300">' +
        '</div>' +
        '<div class="form-group">' +
        '<label class="form-label">结束时间</label>' +
        '<input class="form-input" id="sec_end" type="time" value="' + end + '" step="300">' +
        '</div>';

    showModal(isEdit ? '编辑课节' : '新增课节', body,
        '<button class="btn-secondary" onclick="closeModal()">取消</button>' +
        '<button class="btn-primary" onclick="saveSection(' + id + ')">保存</button>');
}

function editSection(id) {
    apiRequest('get_section', {id: id}, function(res) {
        if (res.code === 1) {
            showSectionModal(res.data.id, res.data.section_number, res.data.section_name, res.data.start_time, res.data.end_time);
        } else { showToast(res.msg, 'error'); }
    });
}

function saveSection(id) {
    var num = document.getElementById('sec_num').value;
    var name = document.getElementById('sec_name').value;
    var start = document.getElementById('sec_start').value;
    var end = document.getElementById('sec_end').value;
    if (!num) { showToast('请输入课节序号', 'error'); return; }
    if (!start || !end) { showToast('请填写开始和结束时间', 'error'); return; }

    apiRequest(id > 0 ? 'update_section' : 'create_section', {
        id: id, section_number: num, section_name: name, start_time: start, end_time: end
    }, function(res) {
        if (res.code === 1) { showToast(res.msg, 'success'); setTimeout(function(){location.reload();}, 500); }
        else { showToast(res.msg, 'error'); }
    });
}

function deleteSection(id) {
    window._deleteSecId = id;
    confirmAction('确认删除该课节？', 'doDeleteSection');
}

function doDeleteSection() {
    apiRequest('delete_section', {id: window._deleteSecId}, function(res) {
        if (res.code === 1) { showToast(res.msg, 'success'); setTimeout(function(){location.reload();}, 500); }
        else { showToast(res.msg, 'error'); }
    });
}
</script>