<?php
session_start();
if (isset($_SESSION['logged_in'])) {
    header("Location: dashboard");
    exit;
}

include 'config.php';
$error = '';

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    // Get user input
    $email = $_POST['email'] ?? '';
    $password = $_POST['password'] ?? '';
    $recaptchaResponse = $_POST['g-recaptcha-response'] ?? '';

    // Validate email format
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = "Invalid email format.";
    } elseif (empty($recaptchaResponse)) {
        $error = "Please complete the reCAPTCHA.";
    } else {
        // reCAPTCHA verification
        $recaptchaSecret = '6LeTD3YrAAAAAHJrIYAbOe-OAzo_DSsJTvglfW2z';
        $verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
        $response = file_get_contents($verifyUrl . '?secret=' . $recaptchaSecret . '&response=' . $recaptchaResponse . '&remoteip=' . $_SERVER['REMOTE_ADDR']);
        $responseKeys = json_decode($response, true);

        if (!$responseKeys['success']) {
            $error = "CAPTCHA verification failed. Try again.";
        } else {
            // Proceed with DB login check
            $sql = "SELECT * FROM accountManage WHERE email = ? AND type = 'S' AND status = 'A' LIMIT 1";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param('s', $email);
            $stmt->execute();
            $result = $stmt->get_result();

            if ($result && $result->num_rows === 1) {
                $user = $result->fetch_assoc();

                if (base64_encode($password) === $user['passw']) {
                    $_SESSION['logged_in'] = true;
                    $_SESSION['SID'] = $user['connectAcid'];
                    header("Location: dashboard");
                    exit;
                } else {
                    $error = "Invalid email or password.";
                }
            } else {
                $error = "Invalid email or password.";
            }

            $stmt->close();
        }
    }
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
    <style>
        body { font-family: Arial; background: #f0f2f5; display: flex; justify-content: center; align-items: center; height: 100vh; }
        .login-box { background: white; padding: 30px; border-radius: 10px; box-shadow: 0 0 10px #ccc; }
        input[type=email], input[type=password] {
            width: 100%; padding: 10px; margin: 10px 0; border-radius: 5px; border: 1px solid #ddd;
        }
        button { padding: 10px 20px; border: none; background: #007BFF; color: white; border-radius: 5px; cursor: pointer; }
        .error { color: red; font-size: 14px; }
    </style>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>

</head>
<body>
<div class="login-box">
    <h2>Login</h2>
    <?php if (!empty($error)) echo "<p class='error'>$error</p>"; ?>
    <form method="POST">
        <input type="email" name="email" placeholder="Email" required />
        <input type="password" name="password" placeholder="Password" required />
        <div class="g-recaptcha" data-sitekey="6LeTD3YrAAAAADUJTMO3qj-DiroRonbahHnSH_67"></div>

        <button type="submit">Login</button>
    </form>
</div>
</body>
</html>
