<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Login</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"/>
    <style>
        /* Global styles */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Arial', sans-serif;
        }

        body {
            background: url('media/admin-login/admin-back.png') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            color: #fff;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
        }

        /* Login box */
        .login-box {
            background-color: rgba(255, 255, 255, 0.8); /* Transparent white background */
            padding: 30px;
            border-radius: 10px;
            width: 100%;
            max-width: 380px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
        }

        h2 {
            color: #333;
            margin-bottom: 20px;
            font-size: 30px;
            font-weight: 600;
        }

        /* Textbox input fields */
        .textbox {
            position: relative;
            margin-bottom: 20px;
        }

        .textbox input {
            width: 100%;
            padding: 15px;
            font-size: 16px;
            color: #333;
            border: 1px solid #ddd;
            border-radius: 5px;
            outline: none;
            transition: border-color 0.3s ease;
        }

        .textbox input:focus {
            border-color: #ff4081;
        }

        /* Forgot password link container */
        .forgot-password {
            display: flex;
            justify-content: center;
            margin-top: 10px;
        }

        .forgot-password a {
            font-size: 14px;
            color: #ff4081;
            text-decoration: none;
            transition: color 0.3s ease;
        }

        .forgot-password a:hover {
            color: #d50064;
        }

        /* Button */
        .btn {
            width: 100%;
            padding: 15px;
            font-size: 18px;
            color: white;
            background-color: #ff4081;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            margin-top: 10px;
        }

        .btn:hover {
            background-color: #ff80ab;
        }

        /* Status message */
        #statusMessage {
            font-size: 16px;
            margin-top: 10px;
            margin-bottom: 10px;

            display: none; /* Initially hidden */
        }

        .processing {
            color: #ff4081;
        }

        .success {
            color: #4caf50;
        }

        .error {
            color: #f44336;
        }

        /* Responsive Design */
        @media screen and (max-width: 768px) {
            .login-box {
                padding: 20px;
                width: 90%;
            }
        }

        @media screen and (max-width: 480px) {
            h2 {
                font-size: 24px;
            }

            .btn {
                font-size: 16px;
                padding: 12px;
            }

            .textbox input {
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="login-box">
        <h2>Admin Login</h2>
        <!-- Status message span -->
        <span id="statusMessage"></span>

        <form id="loginForm">
            <div class="textbox">
                <input type="email" placeholder="Enter Email" name="email" id="email" required>
            </div>
            <div class="textbox">
                <input type="password" placeholder="Enter Password" name="password" id="password" required>
            </div>
            
            <button type="button" class="btn" id="loginBtn">
              <i class="fas fa-user-shield"></i> Super Admin Login
            </button>
            <button type="button" class="btn" id="admin_user_login_btn">
              <i class="fas fa-user"></i> Admin/User Login
            </button>

            <div class="forgot-password">
                <a href="#">Forgot Password?</a>
            </div>
        </form>
    </div>

<script>
    // Function to handle Super Admin Login
    document.getElementById('loginBtn').addEventListener('click', function () {
        const email = document.getElementById('email').value;
        const password = document.getElementById('password').value;

        if (!email || !password) {
            displayStatusMessage("Please enter both email and password.", "error");
            return;
        }

        displayStatusMessage("Processing...", "processing");

        const loginData = { email, password };

        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'php/login/login.php', true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onload = function () {
            if (xhr.status === 200) {
                const response = JSON.parse(xhr.responseText);
                if (response.message === 'Login successful!') {
                    const sessionKey = generateSessionKey();
                    const sessionTime = new Date().toISOString();

                    localStorage.setItem('sessionKey', sessionKey);
                    localStorage.setItem('sessionTime', sessionTime);
                    localStorage.setItem('sessionChecked', 'yes');
                    localStorage.setItem('sessionEmail', email); // ✅ Store email
                    localStorage.setItem('sessionName', 'Spice Up Foods'); // ✅ Store email
                    localStorage.setItem('sessionRole', 'superadmin');

                    displayStatusMessage("Login successful!", "success");
                    window.location.href = 'index.php';
                } else {
                    displayStatusMessage(response.message, "error");
                }
            } else {
                displayStatusMessage('Error: ' + xhr.statusText, "error");
            }
        };

        xhr.send(JSON.stringify(loginData));
    });

    // Function to handle Admin/User Login
    document.getElementById('admin_user_login_btn').addEventListener('click', function () {
        const email = document.getElementById('email').value;
        const password = document.getElementById('password').value;

        if (!email || !password) {
            displayStatusMessage("Please enter both email and password.", "error");
            return;
        }

        displayStatusMessage("Processing...", "processing");

        const loginData = { email, password };

        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'php/login/user_login.php', true);
        xhr.setRequestHeader('Content-Type', 'application/json');

        xhr.onload = function () {
            if (xhr.status === 200) {
                const response = JSON.parse(xhr.responseText);
                if (response.message === 'Login successful!') {
                    const sessionKey = generateSessionKey();
                    const sessionTime = new Date().toISOString();

                    localStorage.setItem('sessionKey', sessionKey);
                    localStorage.setItem('sessionTime', sessionTime);
                    localStorage.setItem('sessionChecked', 'yes');
                    localStorage.setItem('sessionEmail', email); // ✅ Store email
                    localStorage.setItem('sessionName', name); // ✅ Store email
                    localStorage.setItem('sessionRole', response.role);

                    displayStatusMessage("Login successful!", "success");
                    window.location.href = 'index.php';
                } else {
                    displayStatusMessage(response.message, "error");
                }
            } else {
                displayStatusMessage('Error: ' + xhr.statusText, "error");
            }
        };

        xhr.send(JSON.stringify(loginData));
    });

    // Function to display status message
    function displayStatusMessage(message, type) {
        const statusMessageElement = document.getElementById('statusMessage');
        statusMessageElement.style.display = 'block';
        statusMessageElement.textContent = message;
        statusMessageElement.classList.remove('processing', 'success', 'error');
        statusMessageElement.classList.add(type);
    }

    // Function to generate a random session key
    function generateSessionKey() {
        return Math.random().toString(36).substring(2, 15) +
               Math.random().toString(36).substring(2, 15);
    }

    // Function to check session status
    function checkSession() {
        const sessionKey = localStorage.getItem('sessionKey');
        const sessionChecked = localStorage.getItem('sessionChecked');

        if (sessionChecked === 'yes' && sessionKey && sessionKey.trim() !== '') {
            window.location.href = 'index.php';
        }
    }

    // Run session check every second
    setInterval(checkSession, 1000);
</script>
</body>
</html>
