<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 会话导出系统</title>
    <link rel="stylesheet" href="/static/style.css">
</head>
<body class="login-body">
    <div class="login-container">
        <div class="login-card">
            <div class="login-header">
                <h1>会话导出系统</h1>
                <p>登录以访问导出功能</p>
            </div>

            <form id="loginForm" class="login-form">
                <div class="form-group">
                    <label for="eid">工号</label>
                    <input
                        type="text"
                        id="eid"
                        name="eid"
                        required
                        autocomplete="username"
                        placeholder="请输入工号">
                </div>

                <div class="form-group">
                    <label for="password">密码</label>
                    <input
                        type="password"
                        id="password"
                        name="password"
                        required
                        autocomplete="current-password"
                        placeholder="请输入密码">
                </div>

                <div id="errorMessage" class="error-message" style="display: none;"></div>

                <button type="submit" class="btn btn-primary btn-block" id="loginBtn">
                    登录
                </button>
            </form>
        </div>
    </div>

    <script src="/static/script.js"></script>
    <script>
        const form = document.getElementById('loginForm');
        const eidInput = document.getElementById('eid');
        const passwordInput = document.getElementById('password');
        const errorMessage = document.getElementById('errorMessage');
        const loginBtn = document.getElementById('loginBtn');

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

            const eid = eidInput.value.trim();
            const password = passwordInput.value;

            if (!eid || ! password) {
                showError('请输入工号和密码');
                return;
            }

            hideError();
            loginBtn.disabled = true;
            loginBtn.textContent = '登录中...';

            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ eid, password }),
                });

                const data = await response.json();

                if (response.ok && data.success) {
                    loginBtn.textContent = '登录成功';
                    setTimeout(() => {
                        window.location.href = '/';
                    }, 200);
                } else {
                    showError(data.error || '登录失败，请检查工号和密码');
                }
            } catch (error) {
                showError('网络错误，请稍后重试');
            } finally {
                if (! errorMessage.style.display || errorMessage.style.display === 'none') {
                    loginBtn.disabled = false;
                    loginBtn.textContent = '登录';
                }
            }
        });

        function showError(message) {
            errorMessage.textContent = message;
            errorMessage.style.display = 'block';
        }

        function hideError() {
            errorMessage.style.display = 'none';
        }

        eidInput.focus();
    </script>
</body>
</html>
