<?php
/**
 * 分类管理模块
 */
$db = getDB();
$categories = $db->query("SELECT * FROM function_categories ORDER BY sort_order")->fetchAll();
?>

<div class="toolbar">
    <div class="toolbar-title">分类管理</div>
    <div class="toolbar-actions">
        <button class="btn-primary" onclick="showCategoryModal()"><i class="fas fa-plus"></i> 新增分类</button>
    </div>
</div>

<div class="panel-card">
    <table class="data-table">
        <thead>
            <tr>
                <th>ID</th>
                <th>名称</th>
                <th>图标</th>
                <th>排序</th>
                <th>功能数量</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($categories as $cat): ?>
            <?php
            $funcCount = $db->prepare("SELECT COUNT(*) as cnt FROM functions WHERE category_id = ?");
            $funcCount->execute([$cat['id']]);
            $cnt = $funcCount->fetch()['cnt'];
            ?>
            <tr>
                <td><?php echo $cat['id']; ?></td>
                <td><?php echo htmlspecialchars($cat['name']); ?></td>
                <td><i class="fas <?php echo htmlspecialchars($cat['icon']); ?>"></i> <?php echo htmlspecialchars($cat['icon']); ?></td>
                <td><?php echo $cat['sort_order']; ?></td>
                <td><?php echo $cnt; ?></td>
                <td>
                    <div class="table-actions">
                        <button class="btn-sm btn-edit" onclick="editCategory(<?php echo $cat['id']; ?>)">编辑</button>
                        <button class="btn-sm btn-del" onclick="deleteCategory(<?php echo $cat['id']; ?>)">删除</button>
                    </div>
                </td>
            </tr>
        <?php endforeach; ?>
        <?php if (empty($categories)): ?>
            <tr><td colspan="6" style="text-align:center;color:#9ca3af;padding:32px;">暂无分类数据</td></tr>
        <?php endif; ?>
        </tbody>
    </table>
</div>

<script>
function showCategoryModal(id, name, icon, sort) {
    id = id || 0;
    name = name || '';
    icon = icon || 'fa-folder';
    sort = sort || 0;
    var isEdit = id > 0;

    var body = '<div class="form-group">' +
        '<label class="form-label">分类名称</label>' +
        '<input class="form-input" id="cat_name" value="' + name + '" placeholder="如：在校常用">' +
        '</div>' +
        '<div class="form-group">' +
        '<label class="form-label">图标 class</label>' +
        '<input class="form-input" id="cat_icon" value="' + icon + '" placeholder="如：fa-school">' +
        '<div class="form-hint">使用 Font Awesome 图标类名</div>' +
        '</div>' +
        '<div class="form-group">' +
        '<label class="form-label">排序权重</label>' +
        '<input class="form-input" id="cat_sort" type="number" value="' + sort + '" placeholder="数字越小越靠前">' +
        '</div>';

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

function editCategory(id) {
    apiRequest('get_category', {id: id}, function(res) {
        if (res.code === 1) {
            showCategoryModal(res.data.id, res.data.name, res.data.icon, res.data.sort_order);
        } else {
            showToast(res.msg, 'error');
        }
    });
}

function saveCategory(id) {
    var name = document.getElementById('cat_name').value;
    var icon = document.getElementById('cat_icon').value;
    var sort = document.getElementById('cat_sort').value;

    if (!name) { showToast('请输入分类名称', 'error'); return; }

    apiRequest(id > 0 ? 'update_category' : 'create_category', {
        id: id, name: name, icon: icon, sort_order: sort
    }, function(res) {
        if (res.code === 1) {
            showToast(res.msg, 'success');
            setTimeout(function() { location.reload(); }, 500);
        } else {
            showToast(res.msg, 'error');
        }
    });
}

function deleteCategory(id) {
    confirmAction('删除分类将同时删除该分类下的所有菜单，确认删除？', 'doDeleteCategory_' + id);
}

function doDeleteCategory_1(id) { /* placeholder */ }
// 动态生成删除函数
<?php foreach ($categories as $cat): ?>
function doDeleteCategory_<?php echo $cat['id']; ?>() {
    apiRequest('delete_category', {id: <?php echo $cat['id']; ?>}, function(res) {
        if (res.code === 1) {
            showToast(res.msg, 'success');
            setTimeout(function() { location.reload(); }, 500);
        } else {
            showToast(res.msg, 'error');
        }
    });
}
<?php endforeach; ?>
</script>