<!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 rel="icon" type="image/webp" href="images/favicon.webp">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            overflow: hidden; /* Prevent scrolling */
        }

        .container {
            background-color: #fff;
            padding: 30px;
            height: 355px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 270px;
            text-align: center;
            transform: translateY(-20px); /* Moves the container up by 20px */
        }

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

        .input-group {
            margin-bottom: 20px;
            text-align: left;
        }

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

        input[type="text"], input[type="password"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            box-sizing: border-box;
            transition: border-color 0.3s ease;
        }

        input[type="text"]:focus, input[type="password"]:focus {
            border-color: #4CAF50;
            outline: none;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        input[type="submit"]:hover {
            background-color: #45a049;
        }

        .forgot-password {
            font-size: 14px;
            color: #555;
            text-decoration: none;
            margin-top: 10px;
            display: block;
        }

        .forgot-password:hover {
            text-decoration: underline;
        }

        footer {
            margin-top: 0px;
            font-size: 0.9em;
            color: #aaa;
            text-align: center;
            padding: 0px;
            text-align: center;
            width: 100%;
            position: fixed;
            bottom: 0;
        }
        .footer-content {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        /* Mobile responsiveness */
        @media (max-width: 480px) {
            .container {
                padding: 20px;
                max-width: 100%;
                height: 370px;
                margin: 0 15px;
            }

            h2 {
                font-size: 24px;
            }

            input[type="submit"] {
                font-size: 14px;
                padding: 12px;
            }
        }

        /* Modal Popup */
        .modal {
            display: none; /* Hidden by default */
            position: fixed;
            z-index: 1;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.99);
            padding-top: 60px;
        }

        .modal-content {
            background-color: #fff;
            margin: 5% auto;
            padding: 20px;
            border: 1px solid #888;
            width: 80%;
            max-width: 350px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .modal-content h3 {
            color: #3a3b3c;
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="/login.html">
            <img src="/images/logo-tr.webp" width="250" style="margin-bottom: -5px;" alt="Logo">
        </a>
        <h3 style="color: #3a3b3c;">Login Credentials</h3>
        <form id="loginForm">
            <div class="input-group">
                <label for="username">Username:</label>
                <input type="text" id="usernameInput" name="username" required autocomplete="off">
            </div>
            <div class="input-group">
                <label for="password">Password:</label>
                <input type="password" id="passwordInput" name="password" required autocomplete="off">
            </div>
            <input type="submit" value="Submit"><br><br>
            <a href="https://wa.me/966505305180" target="_blank" class="forgot-password">Forgot password?</a><br><br>
        </form>
    </div>

    <footer style="width: 70%; padding: 0px; font-size: 0.7em;">
        <div class="footer-content" style="margin-top: 0px;">
            <span style="margin-bottom: 10px; font-size: 12px">‎&copy; 2024 Al-Fares Transportation Co.<br>All Rights Reserved<br></span>
        </div>
    </footer>

    <!-- Modal Popup -->
    <div id="welcomeModal" class="modal">
        <div class="modal-content">
            <h3 style="margin-bottom: -10px;">Welcome, <span id="fullName"></span></h3>
            <p>Logged in successfully.</p>
        </div>
    </div>

    <script>
        document.getElementById('loginForm').addEventListener('submit', function(event) {
            event.preventDefault(); // Prevent the form from submitting

            // Get the values entered by the user
            var enteredUsername = document.getElementById('usernameInput').value;
            var enteredPassword = document.getElementById('passwordInput').value;

            // Send a POST request to login.php
            fetch('login.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({
                    'username': enteredUsername,
                    'password': enteredPassword
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // Store the login state, username, and full name in sessionStorage
                    sessionStorage.setItem('isLoggedIn', 'true');
                    sessionStorage.setItem('username', data.username);
                    sessionStorage.setItem('fullName', data.full_name); // Store full name

                    // Show the welcome popup with the user's full name
                    document.getElementById('fullName').textContent = data.full_name;
                    document.getElementById('welcomeModal').style.display = "block";

                    // Close the modal and redirect after a short delay
                    setTimeout(function() {
    // Hide the modal
    document.getElementById('welcomeModal').style.display = "none";

    // Hide the login form
    document.querySelector('.container').style.display = "none";
    
    // Change the background to the desired image with gradient
    document.body.style.background = `
        linear-gradient(to bottom, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.5) 100%),
        url('https://ftco.iceiy.com/images/ruk5.webp') no-repeat center center fixed
    `;
    document.body.style.backgroundSize = "cover";

    // Wait for 1 second, then redirect
    setTimeout(function() {
        window.location.href = "/indexapp.html";
    }, 0.01);
}, 2000); // Wait 3 seconds before starting the transition

                } else {
                    alert(data.message); // Display error message from PHP
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('An error occurred. Please try again.');
            });
        });

        // Close the modal if the user clicks outside of it
        window.onclick = function(event) {
            if (event.target == document.getElementById('welcomeModal')) {
                document.getElementById('welcomeModal').style.display = "none";
            }
        }
    </script>
</body>
</html>