<?php
session_start(); // Start the session
include '../includes/connection.php';

// Protect page — redirect to login if not logged in
if (!isset($_SESSION['authenticated_user']) || $_SESSION['authenticated_user'] !== true) {
    header("Location: /LoanApprovalPredction/login.php");
    exit;
}

$mpid = $_SESSION['mpid'];
$name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo htmlspecialchars($name); ?> | Notifications</title>
    <link rel="icon" href="../images/Icon.png">
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        /* Custom styles for the popup */
        .popup-overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.7);
            z-index: 50;
            justify-content: center;
            align-items: center;
        }
        .popup-image {
            max-width: 90%;
            max-height: 90%;
            object-fit: contain;
            border: 4px solid white;
            border-radius: 8px;
        }
    </style>
</head>
<body class="bg-gray-100 text-gray-800">

<!-- Navbar -->
<header class="sticky top-0 z-10">
    <?php include '../includes/navbar.php'; ?>
</header>

<!-- Heading Section -->
<section class="text-center py-6">
    <h1 class="text-2xl font-bold text-green-700">
        <?php if ($mpid == 1 || $mpid == 3): ?>
            In this page you will get all types of notifications
        <?php else: ?>
            In this page you will get your specified notifications only
        <?php endif; ?>
    </h1>
</section>

<!-- Notifications Section -->
<div class="max-w-6xl mx-auto px-4">
    <?php
    if ($mpid == 1 || $mpid == 3) {
        $query = "SELECT nid, notification, MPid, date, img FROM notification";
    } elseif ($mpid == 2) {
        $query = "SELECT nid, notification, MPid, date, img FROM notification WHERE MPid = 2";
    } else {
        $query = "SELECT nid, notification, MPid, date, img FROM notification WHERE MPid = 4";
    }

    $result = mysqli_query($conn, $query);

    if (mysqli_num_rows($result) > 0):
    ?>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <?php while ($row = mysqli_fetch_assoc($result)): ?>
                <div class="bg-white shadow-md rounded-lg p-6 flex items-center gap-6 hover:shadow-xl transition-all duration-300">
                    <img src="../Admin/notificationimg/<?php echo $row['img']; ?>"
                         alt="Notification Image"
                         class="w-20 h-20 object-cover rounded-full border-2 border-green-600 cursor-pointer"
                         onclick="showPopup('../Admin/notificationimg/<?php echo $row['img']; ?>')">
                    <div>
                        <p class="text-lg font-semibold text-gray-900"><?php echo $row['notification']; ?></p>
                        <p class="text-sm text-gray-500 mt-1">Date: <?php echo $row['date']; ?></p>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    <?php else: ?>
        <p class="text-center text-gray-500 mt-10 text-lg">No notifications found.</p>
    <?php endif; ?>
</div>

<!-- Popup Overlay -->
<div id="popupOverlay" class="popup-overlay">
    <img id="popupImage" class="popup-image" src="" alt="Popup Image">
</div>

<script>
    function showPopup(imageSrc) {
        const popupOverlay = document.getElementById('popupOverlay');
        const popupImage = document.getElementById('popupImage');
        popupImage.src = imageSrc;
        popupOverlay.style.display = 'flex';
    }

    // Close popup when clicking on the overlay
    document.getElementById('popupOverlay').addEventListener('click', function() {
        this.style.display = 'none';
    });
</script>

</body>
</html>