<?php
require_once "../config/session.php";

/* =========================
   GET REDIRECT URL
========================= */
$redirect = $_GET['redirect'] ?? '';

/* =========================
   ALREADY LOGIN
========================= */
if (isset($_SESSION['user_id'])) {

    if (!empty($_GET['redirect'])) {

    $redirect = $_GET['redirect'];

    header("Location: " . $redirect);
    exit();

}

header("Location: ../dashboard.php");
exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login - QRCareTrack</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <style>
        * {
            box-sizing: border-box;
            font-family: 'Segoe UI', sans-serif;
        }

        body {
            margin: 0;
            height: 100vh;
            background: url('../assets/background.png') no-repeat center center/cover;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .login-card {
            width: 400px;
            background: #ffffff;
            padding: 40px 35px;
            border-radius: 12px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.15);
            text-align: center;
        }

        .logo {
            width: 80px;
            margin-bottom: 20px;
        }

        h2 {
            margin-bottom: 25px;
            font-weight: 600;
            color: #333;
        }

        .form-group {
            text-align: left;
            margin-bottom: 18px;
        }

        label {
            font-size: 14px;
            color: #444;
            display: block;
            margin-bottom: 6px;
        }

        input {
            width: 100%;
            padding: 10px 12px;
            border: 1px solid #ccc;
            border-radius: 6px;
            font-size: 14px;
            transition: 0.2s;
        }

        input:focus {
            border-color: #2bbbad;
            outline: none;
            box-shadow: 0 0 0 2px rgba(43,187,173,0.2);
        }

        .btn-login {
            width: 100%;
            padding: 11px;
            background-color: #2bbbad;
            border: none;
            border-radius: 25px;
            color: white;
            font-size: 15px;
            cursor: pointer;
            transition: 0.2s;
            margin-top: 10px;
        }

        .btn-login:hover {
            background-color: #239c92;
        }

        .forgot {
            margin-top: 15px;
            font-size: 13px;
        }

        .forgot a,
        .register-link a {
            text-decoration: none;
            color: #666;
        }

        .forgot a:hover,
        .register-link a:hover {
            text-decoration: underline;
        }

        .register-link{
            margin-top:15px;
            font-size:14px;
        }

        .error {
            background: #ffe6e6;
            color: #d8000c;
            padding: 8px;
            border-radius: 5px;
            margin-bottom: 15px;
            font-size: 13px;
        }

        @media (max-width: 480px) {
            .login-card {
                width: 90%;
                padding: 30px 20px;
            }
        }
    </style>
</head>

<body>

<div class="login-card">

    <img src="../assets/logo.png" class="logo" alt="Logo">

    <?php
    if (isset($_GET['error'])) {
        echo "<div class='error'>Invalid email or password</div>";
    }
    ?>

    <form action="login_process.php" method="POST">

        <!-- SAVE REDIRECT URL -->
        <input type="hidden" name="redirect" value="<?= htmlspecialchars($redirect) ?>">

        <div class="form-group">
            <label>Email</label>
            <input type="email" name="email" required>
        </div>

        <div class="form-group">
            <label>Password</label>
            <input type="password" name="password" required>
        </div>

        <button type="submit" class="btn-login">
            Continue
        </button>

        <div class="forgot">
            <a href="forgot_password.php">Forgot Password?</a>
        </div>

        <div class="register-link">
            Don't have account?
            <a href="register.php?redirect=<?= urlencode($redirect) ?>">
                Register Here
            </a>
        </div>

    </form>

</div>

</body>
</html>