<?php
session_start();

$table_users = $GLOBALS['arif_users'];

$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


// Handle form submission
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $userId = trim($_POST['user_id'] ?? '');
    $password = $_POST['pwd'] ?? '';

    if (empty($userId) || empty($password)) {
        $error = "Please fill in all fields.";
    } else {
        $stmt = $conn->prepare("SELECT * FROM $table_users WHERE user_id = ?");
        if (!$stmt) {
            $error = "Internal server error.";
        } else {
            $stmt->bind_param("s", $userId);
            $stmt->execute();
            $result = $stmt->get_result();

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

                if (password_verify($password, $user['password'])) {
                    $_SESSION['user_id'] = $user['user_id'];
                    $_SESSION['type'] = $user['type'];
                    $_SESSION['name'] = $user['user_name'];

                    $success = "Login successful. Redirecting...";
                    echo "<script>setTimeout(() => { window.location.href = '/Agent/Dashboard'; }, 1000);</script>";
                } else {
                    $error = "Invalid password.";
                }
            } else {
                $error = "No agent account found with this User ID.";
            }

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

<div class="bg-gradient mx-auto" style="">
    <div class="container py-5 mx-auto" style="max-width: 420px; margin-top: 200px;">
        <div class="mx-auto" style="">
            <div class="card shadow-lg border-0 rounded-4 p-4" style="background: linear-gradient(135deg, #f5f7fa, #d9dce0ff); padding: 20px; border-radius: 15px;">
                <img src="/asset/images/new_logo2.jpg" alt="Agent Logo" style="width: 80px; height: 80px; border-radius: 20px; display: block; margin: 0 auto 20px auto;">

                <h4 class="text-center mb-4 fw-semibold text-primary">Agent Login</h4>

                <?php if ($error): ?>
                    <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
                <?php elseif ($success): ?>
                    <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
                <?php endif; ?>

                <form method="post" autocomplete="off" style="display: flex; flex-direction: column; gap: 20px;">
                    <div class="mb-3">
                        <input type="text" class="form-control rounded-3 shadow-sm" id="user_id" name="user_id" placeholder="Enter your Agent ID" required>
                    </div>
                    <div class="mb-3">
                        <input type="password" class="form-control rounded-3 shadow-sm" id="pwd" name="pwd" placeholder="Enter your password" required>
                    </div>
                    <button type="submit" class="btn btn-md btn-primary w-100">Login</button>
                    <!-- <div class="w-100"></div> -->
                </form>

                <div class="text-center mt-3">
                    <!-- Optional: Forgot password link -->
                </div>
            </div>
        </div>
    </div>
</div>
