async function loadSidebar() {
    try {
        const response = await fetch('aside.html');
        const html = await response.text();
        document.getElementById('sidebar-container').innerHTML = html;
    } catch (error) {
        console.error('加载侧边栏失败:', error);
    }
}

// 批量上传功能
function initBatchUpload() {
    const batchUploadBtn = document.getElementById('batch-upload-btn');
    const batchUploadModal = document.getElementById('batch-upload-modal');
    const closeBatchUploadModal = document.getElementById('close-batch-upload-modal');
    const cancelBatchUploadBtn = document.getElementById('cancel-batch-upload-btn');
    const startUploadBtn = document.getElementById('start-upload-btn');

    if (batchUploadBtn && batchUploadModal) {
        // 显示批量上传模态框
        batchUploadBtn.addEventListener('click', () => {
            batchUploadModal.style.display = 'flex';
        });

        // 关闭批量上传模态框
        function closeUploadModal() {
            batchUploadModal.style.display = 'none';
        }

        if (closeBatchUploadModal) {
            closeBatchUploadModal.addEventListener('click', closeUploadModal);
        }
        
        if (cancelBatchUploadBtn) {
            cancelBatchUploadBtn.addEventListener('click', closeUploadModal);
        }

        // 点击模态框外部区域关闭模态框
        batchUploadModal.addEventListener('click', (event) => {
            if (event.target === batchUploadModal) {
                closeUploadModal();
            }
        });
    }

    // 文件上传功能
    const uploadArea = document.getElementById('upload-area');
    const batchFileInput = document.getElementById('batch-file-input');
    const fileNameDisplay = document.getElementById('file-name-display');
    const progressSection = document.getElementById('progress-section');

    if (uploadArea && batchFileInput) {
        // 点击上传区域触发文件选择
        uploadArea.addEventListener('click', () => {
            batchFileInput.click();
        });

        // 文件选择事件
        batchFileInput.addEventListener('change', (event) => {
            if (event.target.files.length > 0) {
                const file = event.target.files[0];
                if (fileNameDisplay) {
                    fileNameDisplay.textContent = `已选择文件: ${file.name}`;
                    fileNameDisplay.style.display = 'block';
                }
                
                if (startUploadBtn) {
                    startUploadBtn.disabled = false;
                }
                
                if (progressSection) {
                    progressSection.style.display = 'block';
                }
            }
        });

        // 拖拽上传功能
        uploadArea.addEventListener('dragover', (event) => {
            event.preventDefault();
            uploadArea.classList.add('dragover');
        });

        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });

        uploadArea.addEventListener('drop', (event) => {
            event.preventDefault();
            uploadArea.classList.remove('dragover');
            
            if (event.dataTransfer.files.length > 0) {
                const file = event.dataTransfer.files[0];
                // 检查文件类型
                if (file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || 
                    file.type === 'application/vnd.ms-excel' || 
                    file.type === 'text/csv') {
                    
                    // 创建新的FileList来更新input的值
                    batchFileInput.files = event.dataTransfer.files;
                    
                    if (fileNameDisplay) {
                        fileNameDisplay.textContent = `已选择文件: ${file.name}`;
                        fileNameDisplay.style.display = 'block';
                    }
                    
                    if (startUploadBtn) {
                        startUploadBtn.disabled = false;
                    }
                    
                    if (progressSection) {
                        progressSection.style.display = 'block';
                    }
                } else {
                    alert('请上传Excel或CSV文件');
                }
            }
        });
    }
}

document.addEventListener('DOMContentLoaded', () => {
    loadSidebar();
    initBatchUpload();
});