<?php
require_once 'db/config.php';
session_start();
$db = db();

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $stmt = $db->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();
    
    if ($user && password_verify($password, $user['password'])) {
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['username'] = $user['username'];
        $_SESSION['role'] = $user['role'];
        
        if ($user['role'] === 'admin') {
            header("Location: admin/index.php");
        } else {
            header("Location: index.php");
        }
        exit;
    } else {
        $error = '用户名或密码错误';
    }
}

$page_title = '登录 - 豪软世界';
require_once 'includes/header.php';
?>

<main class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-5">
                <div class="bg-dark bg-opacity-50 rounded-5 border border-secondary border-opacity-25 p-5 shadow-lg">
                    <div class="text-center mb-5">
                        <div class="logo-box mx-auto mb-3" style="width: 50px; height: 50px; font-size: 1.8rem;">H</div>
                        <h2 class="text-white fw-bold">欢迎回来</h2>
                        <p class="text-muted">登录以管理您的订单和账户</p>
                    </div>

                    <?php if ($error): ?>
                    <div class="alert alert-danger bg-danger bg-opacity-10 border-0 text-danger rounded-4 small">
                        <i class="bi bi-exclamation-circle me-2"></i> <?php echo $error; ?>
                    </div>
                    <?php endif; ?>

                    <form method="POST">
                        <div class="mb-4">
                            <label class="form-label text-white small fw-bold">用户名</label>
                            <input type="text" name="username" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" placeholder="输入用户名" required>
                        </div>
                        <div class="mb-5">
                            <label class="form-label text-white small fw-bold">密码</label>
                            <input type="password" name="password" class="form-control bg-black bg-opacity-50 border-secondary border-opacity-50 text-white p-3 rounded-3" placeholder="输入密码" required>
                        </div>
                        <button type="submit" class="btn btn-primary btn-lg w-100 py-3 rounded-3 shadow mb-4">立即登录</button>
                        <div class="text-center">
                            <span class="text-muted small">还没有账号? <a href="register.php" class="text-primary text-decoration-none">立即注册</a></span>
                        </div>
                    </form>
                </div>
                <div class="text-center mt-4">
                    <div class="alert alert-info bg-info bg-opacity-10 border-0 text-info rounded-4 small py-2">
                        <i class="bi bi-info-circle me-1"></i> 测试账号: admin / admin123
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<?php require_once 'includes/footer.php'; ?>
