<?php
session_start();
$file_db = 'users.json'; // File chứa dữ liệu user

if (isset($_POST['login'])) {
    $username = $_POST['user'];
    $password = $_POST['pass'];

    // Lấy dữ liệu từ file
    $users = file_exists($file_db) ? json_decode(file_get_contents($file_db), true) : [];

    if (isset($users[$username]) && $users[$username]['password'] === $password) {
        $_SESSION['user'] = $users[$username]; // Lưu phiên đăng nhập
        header("Location: index.html"); // Chuyển về trang chủ
        exit();
    } else {
        $error = "Sai tài khoản hoặc mật khẩu!";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Đăng nhập</title>
    <link rel="stylesheet" href="style.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div class="container" style="display:flex; justify-content:center; align-items:center; height:100vh;">
        <div class="glass-panel">
            <h2 style="text-align:center; color: var(--neon);">ĐĂNG NHẬP</h2>
            <?php if(isset($error)) echo "<p style='color:red; text-align:center'>$error</p>"; ?>
            <form method="POST">
                <input type="text" name="user" class="input-group" placeholder="Tài khoản" required>
                <input type="password" name="pass" class="input-group" placeholder="Mật khẩu" required>
                <button type="submit" name="login" class="btn btn-primary" style="width:100%">Vào Web</button>
            </form>
            <p style="text-align:center; margin-top:15px;">
                <a href="register.php" style="color:#aaa;">Chưa có nick? Đăng ký ngay</a>
            </p>
            <p style="text-align:center;"><a href="index.html" style="color:var(--neon);">← Về Trang Chủ</a></p>
        </div>
    </div>
</body>
</html>
