document.addEventListener('DOMContentLoaded', function() {
    // 获取元素
    const tabButtons = document.querySelectorAll('.tab-button');
    const loginForms = document.querySelectorAll('.login-form');
    const userLoginForm = document.getElementById('userLoginForm');
    const adminLoginForm = document.getElementById('adminLoginForm');
    const messageDiv = document.getElementById('message');
    
    // 切换标签页
    tabButtons.forEach(button => {
        button.addEventListener('click', function() {
            // 移除所有活动状态
            tabButtons.forEach(btn => btn.classList.remove('active'));
            loginForms.forEach(form => {
                form.classList.remove('active');
                form.style.display = 'none';
            });
            
            // 添加活动状态到当前点击的标签
            this.classList.add('active');
            const tabId = this.getAttribute('data-tab');
            document.getElementById(tabId + '-login').style.display = 'block';
            document.getElementById(tabId + '-login').classList.add('active');
        });
    });
    
    // 用户登录表单提交
    userLoginForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('userUsername').value;
        const password = document.getElementById('userPassword').value;
        
        // 显示加载状态
        const submitButton = this.querySelector('button[type="submit"]');
        const originalText = submitButton.textContent;
        submitButton.textContent = '登录中...';
        submitButton.disabled = true;
        document.body.classList.add('loading');
        
        // 发送登录请求
        fetch('/shixian/api/user/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: username,
                password: password
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                showMessage('用户登录成功！', 'success');
                // 延迟跳转到主页
                setTimeout(() => {
                    window.location.href = '/shixian/home.html';
                }, 1000);
            } else {
                showMessage(data.message, 'error');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            showMessage('登录请求失败，请稍后重试', 'error');
        })
        .finally(() => {
            // 恢复按钮状态
            submitButton.textContent = originalText;
            submitButton.disabled = false;
            document.body.classList.remove('loading');
        });
    });
    
    // 管理员登录表单提交
    adminLoginForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const username = document.getElementById('adminUsername').value;
        const password = document.getElementById('adminPassword').value;
        
        // 显示加载状态
        const submitButton = this.querySelector('button[type="submit"]');
        const originalText = submitButton.textContent;
        submitButton.textContent = '登录中...';
        submitButton.disabled = true;
        document.body.classList.add('loading');
        
        // 发送登录请求
        fetch('/shixian/api/admin/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                username: username,
                password: password
            })
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                showMessage('管理员登录成功！', 'success');
                // 延迟跳转到主页
                setTimeout(() => {
                    window.location.href = '/shixian/home.html';
                }, 1000);
            } else {
                showMessage(data.message, 'error');
            }
        })
        .catch(error => {
            console.error('Error:', error);
            showMessage('登录请求失败，请稍后重试', 'error');
        })
        .finally(() => {
            // 恢复按钮状态
            submitButton.textContent = originalText;
            submitButton.disabled = false;
            document.body.classList.remove('loading');
        });
    });
    
    // 显示消息函数
    function showMessage(text, type) {
        messageDiv.textContent = text;
        messageDiv.className = 'message ' + type;
        messageDiv.style.display = 'block';
        
        // 3秒后隐藏消息
        setTimeout(() => {
            messageDiv.style.display = 'none';
        }, 3000);
    }
});