<?php
session_start();

// Check if user is already logged in
if (isset($_SESSION['username'])) {
    // If already logged in, redirect to display.php (or caller.php)
    header("Location: display.php");
    exit();
}

// Login logic
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $userData = file_get_contents('users.json');
    $data = json_decode($userData, true); // Convert the JSON to an associative array

    // Check if the username and password are set in the POST request
    if (isset($_POST['username']) && isset($_POST['password'])) {
        $username = $_POST['username'];
        $password = $_POST['password'];

        $loginSuccess = false;
        foreach ($data['users'] as $user) {
            if ($user['username'] === $username && $user['password'] === $password) {
                // Set session variables on successful login
                $_SESSION['username'] = $username;
                $loginSuccess = true;
                break;
            }
        }

        if ($loginSuccess) {
            header("Location: caller.php"); // Redirect to display.php after login
            exit();
        } else {
            $errorMessage = "Invalid username or password.";
        }
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #fafafa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: #ffffff;
            border-radius: 5px;
            width: 350px;
            padding: 40px;
            box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        h2 {
            margin-bottom: 20px;
            font-size: 24px;
            color: #333;
        }

        input[type="text"],
        input[type="password"] {
            width: 90%;
            padding: 12px;
            margin: 8px 0;
            border: 1px solid #ccc;
            border-radius: 3px;
            font-size: 14px;
            background-color: #f9f9f9;
        }

        button {
            width: 100%;
            padding: 14px;
            background-color: #3897f0;
            color: white;
            font-size: 16px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #4fa3f7;
        }

        .error-message {
            color: red;
            font-size: 14px;
            margin-bottom: 15px;
        }

        /* .footer {
            margin-top: 20px;
            font-size: 14px;
            color: #888;
        }

        .footer a {
            text-decoration: none;
            color: #3897f0;
        }

        .footer a:hover {
            text-decoration: underline;
        }

        .social-login {
            margin-top: 20px;
        }

        .social-login a {
            text-decoration: none;
            display: inline-block;
            padding: 10px;
            margin: 5px 0;
            width: 100%;
            background-color: #385185;
            color: white;
            border-radius: 3px;
            font-size: 14px;
        }

        .social-login a:hover {
            background-color: #4c77a1;
        } */
    </style>
</head>

<body>

    <div class="login-container">
        <h2> Login</h2>

        <?php if (isset($errorMessage)): ?>
            <p class="error-message"><?php echo $errorMessage; ?></p>
        <?php endif; ?>

        <form method="POST" action="login.php">
            <input type="text" name="username" placeholder="Username" required><br>
            <input type="password" name="password" placeholder="Password" required><br>
            <button type="submit">Log In</button>
        </form>




    </div>

</body>

</html>