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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mechnet User Login</title>
    <link rel="icon" type="image/png" href="./Images/logo.png">
    <style>
        * {
            margin: 0;
        }

        body {
            background-color: black;
            color: white;
        }

        nav {
            height: 60px;
            box-sizing: border-box;
            width: 100vw;
            display: flex;
            align-items: center;
            background-color: #2b2c2e;
            justify-content: space-between;
            padding: 0 2em;
        }

        .SoftName {
            display: flex;
            align-items: center;
            font-size: 1.5em;
            gap: 1em;
            cursor: pointer;
        }

        .SoftName img {
            width: 40px;
            border-radius: 50%;
        }

        .loginCont {
            margin: 50px auto;
            width: fit-content;
            display: flex;
            flex-direction: column;
            padding: 20px;
            gap: 40px;
            height: auto;
            border-radius: 10px;
            box-sizing: border-box;
            border: 1px solid white;
        }

        .logintext {
            text-align: center;
            margin: 0px;
            padding: 1px;
            font-size: 20px;
        }

        .username-inputbox {
            padding: 0px 20px;
        }

        .passsword-inputbox {
            padding: 0px 20px;
        }

        input {
            width: 100%;
            padding: 8px;
            margin: 5px 0 15px 0;
            box-sizing: border-box;
            border: 1px solid #f3dfdf;
            border-radius: 10px;
            background-color: transparent;
            text-decoration: none;
            color: white;
            font-size: 20px;
            font-weight: 600;
        }

        .LoginButton {
            text-align: center;
        }

        button {
            background-color: rgb(15, 15, 140);
            width: fit-content;
            color: white;
            padding: 6px;
            margin: 30px auto;
            border-radius: 10px;
            cursor: pointer;
            width: 100px;
            margin-bottom: 20px;
            border: none;
        }

        button:hover {
            background-color: #110f0f;
        }
    </style>
</head>

<body>
    <div class="navbar">
        <nav>
            <div class="SoftName">
                <img src="./Images/logo.png" alt="Mechnet Logo">
                <label for="SoftName">Mechnet</label>
            </div>
        </nav>
    </div>
    <div class="loginCont">
        <div class="logintext">
            <h2>Login to Mechnet</h2>
        </div>
        <form id="loginForm">
            <div class="username-inputbox">
                <label for="uname"><b>Username: </b></label><br>
                <input type="text" id="username" placeholder="Enter Username" name="uname" required><br>
            </div>
            <div class="passsword-inputbox">
                <label for="psw"><b>Password: </b></label><br>
                <input type="password" id="password" placeholder="Enter Password" name="psw" required><br>
            </div>
            <div class="LoginButton">
                <button type="submit">Login</button>
            </div>
            <div class="login-status" id="login-status"></div>
        </form>
    </div>
    <script>
        document.getElementById("loginForm").addEventListener("submit", function (event) {
            event.preventDefault();
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            var loginStatus = document.getElementById("login-status");
            var xhr = new XMLHttpRequest();
            xhr.open("POST", "verify_login.php", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.message) {
                        loginStatus.innerHTML = "<p style='color: green; text-align: center;'>" + response.message + "</p>";
                        window.location.href = response.redirect;
                    } else {
                        loginStatus.innerHTML = "<p style='color: red;'>" + response.error + "</p>";
                    }
                }
            };
            xhr.send("username=" + encodeURIComponent(username) + "&password=" + encodeURIComponent(password));
        });
    </script>
</body>

</html>