<?php

$sql_user = "dwsnizvz_newadminpanel";
$sql_pwd  = "newadminpanel";
$Host     = "127.0.0.1:3306";
$Database = "dwsnizvz_newadminpanel";

session_start();

// Check if the user is already logged in
if (isset($_SESSION["isValid"]) && $_SESSION['isValid'] == 1) {
    header("Location: /adminpanel");
    exit();
}

$error = false;

if (isset($_POST["email"]) && isset($_POST["password"])) {
    try {
        // Connect to the database
        $conn = new PDO("mysql:host=$Host;dbname=$Database", $sql_user, $sql_pwd);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        // Prepare and execute the SQL statement
        $accPtr = $conn->prepare("SELECT password FROM users WHERE email = ?");
        $accPtr->execute([$_POST["email"]]);

        $account = $accPtr->fetch(PDO::FETCH_ASSOC);

        // Check if the account exists and if the password matches
        if ($account && password_verify($_POST["password"], $account["password"])) {
            $_SESSION["isValid"] = 1;
            header("Location: /adminpanel");
            exit();
        } else {
            $error = true;
        }
    } catch (PDOException $e) {
        // Log the error to a file or output for debugging (optional)
        error_log("Database error: " . $e->getMessage());
        $error = true;
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
    <!--<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">-->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
    <style>
        body {
            background: #f7f9fc;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }
        .login-card {
            padding: 30px;
            border: none;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            background-color: #ffffff;
        }
        .login-card img {
            display: block;
            margin: 0 auto 20px;
            width: 150px;
        }
        .login-card h3 {
            margin-bottom: 30px;
            font-weight: 700;
            text-align: center;
        }
        .login-card .form-group {
            margin-bottom: 20px;
        }
        .login-card .btn-primary {
            background-color: #007bff;
            border: none;
        }
        .login-card .btn-primary:hover {
            background-color: #0056b3;
        }
        .btn-primary {
            justify-content: center;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="card login-card col-6">
        <?php if ($error): ?>
        <div class="alert alert-danger" role="alert">
            Error: Invalid email or password.
        </div>
        <?php endif; ?>
        <img src="https://kiiltavatpinnat.fi/logo.svg" alt="Logo">
        <h3>Login</h3>
        <form class="loginForm" method="post" novalidate>
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="text" class="form-control" id="email" name="email" placeholder="Enter Email" required autocomplete="off">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" id="password" name="password" placeholder="Enter Password" required autocomplete="off">
                <p class="font-weight-bold text-danger d-none" id="invalidError"></p>
            </div>
            <button class="btn btn-primary btn-bloc col-4 text-center" type="submit">Log in</button>
        </form>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <!--<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>-->
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <!--<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>-->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.min.js"></script>
</body>
</html>
