<?php
session_start();

// Include users.php with absolute path
include_once $_SERVER['DOCUMENT_ROOT'] . '/users.php';

$error = '';
$lockout_message = '';
$success_message = '';

// Check if account is locked
if (isset($_SESSION['lockout_time']) && time() < $_SESSION['lockout_time']) {
    $lockout_message = "Account temporarily locked due to multiple failed attempts. Please try again later.";
} else {
    unset($_SESSION['lockout_time']);
    unset($_SESSION['login_attempts']);
}

// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($lockout_message)) {
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';

    // Check honeypot field
    if (!empty($_POST['honeypot'])) {
        $error = "Spam detected.";
    } else {
        // Validate credentials
        if (isset($users[$email]) && $users[$email]['password'] === $password) {
            // Login successful - set all session variables
            $_SESSION['email'] = $email;
            $_SESSION['username'] = $users[$email]['username'];
            $_SESSION['dashboard'] = $users[$email]['dashboard'];
            $_SESSION['full_name'] = $users[$email]['full_name']; // New session variable
            $_SESSION['phone'] = $users[$email]['phone']; // New session variable
            
            // Reset failed attempts
            unset($_SESSION['login_attempts']);
            unset($_SESSION['lockout_time']);
            
            $success_message = "Login successful! Redirecting...";
            
            // Redirect after delay
            header("Refresh: 1.5; url=" . $_SESSION['dashboard']);
        } else {
            $error = "Invalid email or password!";
            
            // Track failed attempts
            $_SESSION['login_attempts'] = isset($_SESSION['login_attempts']) ? $_SESSION['login_attempts'] + 1 : 1;
            
            // Lock account after 3 failed attempts for 15 minutes
            if ($_SESSION['login_attempts'] >= 3) {
                $_SESSION['lockout_time'] = time() + (15 * 60); // 15 minutes
                $lockout_message = "Too many failed attempts. Account has been temporarily locked.";
            }
        }
    }
}
?>
<!doctype html>
<html lang="en" dir="ltr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="icon" href="https://gofundbiz.com/assets/images/gallery/biz-logo.png" type="image/x-icon"/>
    <title>:: Gofundbiz :: Login Portal</title>
    <!-- Bootstrap Core and vendor -->
    <link rel="stylesheet" href="../assets/plugins/bootstrap/css/bootstrap.min.css" />
    <!-- Core css -->
    <link rel="stylesheet" href="../assets/css/style.min.css"/>
    <style>
        .error-message {
            color: #dc3545;
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        .lockout-message {
            color: #856404;
            background-color: #fff3cd;
            border: 1px solid #ffeaa7;
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        .success-message {
            color: #155724;
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
    </style>
</head>
<body class="font-muli theme-blush">
<div class="auth option2">
    <div class="auth_left">
        <div class="card">
            <div class="card-body">
                <div class="text-center">
                   <a class="header-brand" href="https://gofundbiz.com">
                       <img src="../assets/images/gallery/biz-logo.png" 
                            alt="Brand Logo" 
                            class="brand-logo" 
                            style="background-color: transparent;width: 170px;">
                   </a><br>
                    <br><h2 style="color: #04457d;"> Welcome back !</h2>
                    <p>Great to see you. Let's fund South Africa's future, one business at a time again.</p><br>
                </div>
                
                <!-- Message container for dynamic messages -->
                <div id="messageContainer">
                    <?php if (!empty($error)): ?>
                        <div class="error-message"><?= htmlspecialchars($error) ?></div>
                    <?php endif; ?>
                    
                    <?php if (!empty($lockout_message)): ?>
                        <div class="lockout-message"><?= htmlspecialchars($lockout_message) ?></div>
                    <?php endif; ?>
                    
                    <?php if (!empty($success_message)): ?>
                        <div class="success-message"><?= htmlspecialchars($success_message) ?></div>
                    <?php endif; ?>
                </div>
                
                <form id="loginForm" method="POST">
                    <!-- Honeypot field for spam protection -->
                    <input type="text" name="honeypot" style="display:none" tabindex="-1" autocomplete="off">
                    
                    <div class="form-group">
                        <input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" required>
                    </div>
                    <div class="form-group">
                        <label class="form-label"><a href="#" class="float-right small">I forgot password</a></label>
                        <input type="password" name="password" class="form-control" id="password" placeholder="Password" required>
                    </div>
                    <div class="form-group">
                        <label class="custom-control custom-checkbox">
                        <input type="checkbox" class="custom-control-input" id="rememberMe" />
                        <span class="custom-control-label" style="color: #232323;">Activate AI Password Manager</span>
                        </label>
                    </div>
                    <div class="text-center">
                        <button type="submit" class="btn btn-primary btn-block" style="background-color: #04457d;">SIGN IN</button>
                        <div class="text-muted mt-4">Don't have account yet? <a href="https://gofundbiz.com/user/en/register">Sign up</a></div>
                    </div>
                </form>
            </div>
        </div>        
    </div>
</div>

<!-- Start Main project js, jQuery, Bootstrap -->
<script src="../assets/bundles/lib.vendor.bundle.js"></script>
<!-- Start project main js and page js -->
<script src="../assets/js/core.js"></script>

<script>
// Handle "Remember Me" functionality
document.getElementById('rememberMe').addEventListener('change', function() {
    if (this.checked) {
        console.log('Remember me enabled');
        // In a real application, this would set a longer session timeout
    }
});
</script>
</body>
</html>