/**
 * 2026企业CMS - 后台管理JavaScript
 */

// 侧边栏折叠
function toggleSidebar() {
    const sidebar = document.getElementById('sidebar');
    sidebar.classList.toggle('collapsed');
}

// 全选/取消全选
function toggleSelectAll(checkbox) {
    const checkboxes = document.querySelectorAll('input[name="ids[]"]');
    checkboxes.forEach(function(cb) {
        cb.checked = checkbox.checked;
    });
}

// 批量操作确认
function confirmBatch(action) {
    const checked = document.querySelectorAll('input[name="ids[]"]:checked');
    if (checked.length === 0) {
        alert('请至少选择一项！');
        return false;
    }
    if (action === 'delete') {
        return confirm('确定要删除选中的 ' + checked.length + ' 项吗？');
    }
    return true;
}

// 单条删除确认
function confirmDelete(id, name) {
    return confirm('确定要删除「' + (name || '此项') + '」吗？');
}

// 图片预览
function previewImage(input) {
    if (input.files && input.files[0]) {
        const reader = new FileReader();
        reader.onload = function(e) {
            const preview = document.getElementById('image-preview');
            if (preview) {
                preview.src = e.target.result;
                preview.style.display = 'block';
            }
        };
        reader.readAsDataURL(input.files[0]);
    }
}

// AJAX保存设置
function saveSetting(key, value) {
    const formData = new FormData();
    formData.append('key', key);
    formData.append('value', value);
    
    fetch('/admin/api/setting.php', {
        method: 'POST',
        body: formData
    })
    .then(res => res.json())
    .then(data => {
        if (data.success) {
            showToast('保存成功！');
        } else {
            showToast('保存失败！', 'error');
        }
    });
}

// Toast提示
function showToast(message, type = 'success') {
    const toast = document.createElement('div');
    toast.className = 'toast toast-' + type;
    toast.textContent = message;
    toast.style.cssText = 'position:fixed;top:80px;right:20px;padding:12px 20px;border-radius:4px;color:#fff;z-index:9999;transition:0.3s;';
    
    if (type === 'success') {
        toast.style.background = '#52c41a';
    } else {
        toast.style.background = '#f5222d';
    }
    
    document.body.appendChild(toast);
    
    setTimeout(function() {
        toast.style.opacity = '0';
        setTimeout(function() {
            document.body.removeChild(toast);
        }, 300);
    }, 2000);
}

// 富文本编辑器简单封装（可集成CKEditor或TinyMCE）
function initEditor(selector) {
    const textareas = document.querySelectorAll(selector);
    textareas.forEach(function(textarea) {
        // 简易工具栏
        const toolbar = document.createElement('div');
        toolbar.style.cssText = 'background:#f5f5f5;padding:5px;border:1px solid #ddd;border-bottom:none;';
        toolbar.innerHTML = ''
            + '<button type="button" onclick="formatText(this,''bold')">B</button>'
            + '<button type="button" onclick="formatText(this,''italic')"><i>I</i></button>'
            + '<button type="button" onclick="formatText(this,''underline')"><u>U</u></button>'
            + '<button type="button" onclick="insertImage(this)">图片</button>'
            + '<button type="button" onclick="insertLink(this)">链接</button>';
        
        textarea.style.minHeight = '300px';
        textarea.style.width = '100%';
        textarea.style.padding = '10px';
        
        textarea.parentNode.insertBefore(toolbar, textarea);
    });
}

function formatText(btn, command) {
    document.execCommand(command, false, null);
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 自动隐藏提示消息
    const alerts = document.querySelectorAll('.alert');
    alerts.forEach(function(alert) {
        setTimeout(function() {
            alert.style.opacity = '0';
            setTimeout(function() {
                alert.style.display = 'none';
            }, 300);
        }, 3000);
    });
});
