<?php
/**
 * 用户登录页面
 * 物业工单管理系统
 */

require_once '../includes/functions.php';

// 如果用户已登录，重定向到仪表盘
if (isLoggedIn()) {
    redirect('../dashboard.php');
}

$errors = [];

// 处理登录表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitizeInput(isset($_POST['username']) ? $_POST['username'] : '');
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    
    // 验证输入
    if (empty($username)) {
        $errors[] = '用户名不能为空';
    }
    
    if (empty($password)) {
        $errors[] = '密码不能为空';
    }
    
    // 如果没有错误，尝试登录
    if (empty($errors)) {
        try {
            $pdo = getDBConnection();
            
            // 查询用户（支持用户名或邮箱登录）
            $stmt = $pdo->prepare("SELECT id, username, email, password FROM users WHERE username = ? OR email = ?");
            $stmt->execute([$username, $username]);
            $user = $stmt->fetch();
            
            if ($user && password_verify($password, $user['password'])) {
                // 登录成功，创建会话
                startSession();
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['username'] = $user['username'];
                
                showSuccess('登录成功！欢迎回来，' . $user['username'] . '！');
                redirect('../dashboard.php');
            } else {
                $errors[] = '用户名或密码错误';
            }
            
        } catch (PDOException $e) {
            $errors[] = '数据库错误，请稍后重试';
        }
    }
}

$messages = getMessages();
?>
<!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="../assets/css/style.css">
</head>
<body>
    <div class="header">
        <div class="container">
            <h1>物业工单管理系统</h1>
            <p>用户登录</p>
        </div>
    </div>

    <div class="container">
        <div class="card">
            <h2>登录您的账户</h2>
            
            <?php if (!empty($messages['success'])): ?>
                <div class="alert alert-success"><?php echo $messages['success']; ?></div>
            <?php endif; ?>
            
            <?php if (!empty($errors)): ?>
                <?php foreach ($errors as $error): ?>
                    <div class="alert alert-error"><?php echo $error; ?></div>
                <?php endforeach; ?>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label for="username">用户名或邮箱 *</label>
                    <input type="text" id="username" name="username" class="form-control" 
                           value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>" 
                           required>
                </div>
                
                <div class="form-group">
                    <label for="password">密码 *</label>
                    <input type="password" id="password" name="password" class="form-control" required>
                </div>
                
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">登录</button>
                    <a href="register.php" class="btn btn-secondary">没有账户？去注册</a>
                </div>
                
                <div class="form-group">
                    <a href="forgot_password.php" class="btn btn-secondary">忘记密码？</a>
                </div>
            </form>
        </div>
    </div>

    <div class="footer">
        <div class="container">
            <p>&copy; 2024 物业工单管理系统. 保留所有权利.</p>
        </div>
    </div>
</body>
</html>
