function goTo(url) {
	window.location.href = url
}
function show_add_modal(modal) {
	modal = modal || '.modal'
	if (typeof modal == 'string') {
		modal = document.querySelector(`${modal}`)
	}
	modal.querySelector('.modal-header .modal-title span').innerHTML = '添加'
	show_modal(modal)
}
function show_edit_modal(modal) {
	modal = modal || '.modal'
	if (typeof modal == 'string') {
		modal = document.querySelector(`${modal}`)
	}
	modal.querySelector('.modal-header .modal-title span').innerHTML = '修改'
	show_modal(modal)
}
function show_modal(modal) {
	modal = modal || '.modal'
	if (typeof modal == 'string') {
		modal = document.querySelector(`${modal}`)
	}
	modal.classList.add('active')
}
function hide_modal(modal) {
	modal = modal || '.modal'
	if (typeof modal == 'string') {
		modal = document.querySelector(`${modal}`)
	}
	modal.classList.remove('active')
}


function _confirm(text, url) {
	if (confirm(text)) {
		fetch(url, {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
			},
		})
			.then((response) => response.json())
			.then((data) => {
				if (data.success) {
					alert('操作成功')
					window.location.reload()
				} else {
					alert('操作失败:' + data.error)
				}
			})
			.catch((error) => {})
	}
}

function toggleUserMenu() {
    const menu = document.getElementById('userMenu');
    menu.classList.toggle('show');

    // 点击其他区域关闭菜单
    document.addEventListener('click', function closeMenu(e) {
        if (!e.target.closest('.user-info')) {
            menu.classList.remove('show');
            document.removeEventListener('click', closeMenu);
        }
    });
}

function showChangePasswordModal() {
    const modal = document.getElementById('modal-password');
    show_modal(modal)
}

function changePassword() {
    const oldPassword = document.getElementById('old_password').value;
    const newPassword = document.getElementById('new_password').value;
    const confirmPassword = document.getElementById('confirm_password').value;

    if (!oldPassword || !newPassword || !confirmPassword) {
        alert('请填写所有密码字段');
        return;
    }

    if (newPassword !== confirmPassword) {
        alert('新密码和确认密码不匹配');
        return;
    }

    fetch('/update_user_password', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            old_password: oldPassword,
            new_password: newPassword
        })
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('密码修改成功');
            hide_modal('#modal-password');
            window.location.href='/login';
        } else {
            alert(data.error || '密码修改失败');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('密码修改失败');
    });
}

function logout() {
    if(confirm('确定要注销吗？')){
        window.location.href = '/logout';
    }
    // fetch('/logout', {
    //     method: 'POST',
    //     headers: {
    //         'Content-Type': 'application/json',
    //     }
    // })
    // .then(response => response.json())
    // .then(data => {
    //     if (data.success) {
    //         window.location.href = '/login';
    //     } else {
    //         alert(data.message || '注销失败');
    //     }
    // })
    // .catch(error => {
    //     console.error('Error:', error);
    //     alert('注销失败');
    // });
}
