document.addEventListener('DOMContentLoaded',()=>{
    const loginForm    = document.getElementById('loginForm');
    const errorMessage = document.getElementById('errorMessage');

    loginForm.addEventListener('submit',e=>{
        e.preventDefault();

        const username = document.getElementById('username').value.trim();
        const password = document.getElementById('password').value.trim();

        if(!username || !password){
            errorMessage.textContent = '用户名和密码不能为空';
            errorMessage.style.display = 'block';
            return;
        }

        /* 这里可替换为真实校验逻辑 —— 目前直接视为成功 */
        errorMessage.style.display = 'none';

        /* 写入 cookie，1 天有效 */
        const expire = new Date();
        expire.setDate(expire.getDate() + 1);
        document.cookie = `username=${encodeURIComponent(username)}; expires=${expire.toUTCString()}; path=/`;

        /* 登录后跳回首页（商品详情页） */
        window.location.href = 'index.html';
    });
});
