<?php

require_once __DIR__ . '/../../library/super_admin_notification_helper.php';

/*
|--------------------------------------------------------------------------
| FETCH NOTIFICATIONS
|--------------------------------------------------------------------------
*/

$unreadCount = getUnreadNotificationCount($pdo);

$latestNotifications = getLatestNotifications($pdo, 5);

?>

<!-- =========================================================
NAVBAR NOTIFICATIONS
========================================================= -->

<li class="nav-item dropdown">

    <a
        class="nav-link"
        data-toggle="dropdown"
        href="#"
    >

        <i class="far fa-bell"></i>

        <?php if($unreadCount > 0): ?>

            <span class="badge badge-danger navbar-badge">

                <?= $unreadCount ?>

            </span>

        <?php endif; ?>

    </a>

    <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right shadow">

        <!-- HEADER -->

        <span class="dropdown-item dropdown-header">

            <?= $unreadCount ?>

            Notifications

        </span>

        <div class="dropdown-divider"></div>

        <!-- NOTIFICATIONS -->

        <?php if($latestNotifications): ?>

            <?php foreach($latestNotifications as $notification): ?>

                <a
                    href="<?php echo WEB_ROOT; ?>super_admin/notifications/view.php?id=<?= $notification['id'] ?>"
                    class="dropdown-item"
                >

                    <div class="d-flex justify-content-between align-items-start">

                        <div>

                            <!-- TITLE -->

                            <strong>

                                <?= htmlspecialchars($notification['title']) ?>

                            </strong>

                            <br>

                            <!-- MESSAGE -->

                            <small class="text-muted">

                                <?= htmlspecialchars(substr($notification['message'], 0, 50)) ?>...

                            </small>

                            <br>

                            <!-- DATE -->

                            <small class="text-muted">

                                <?= date('d M Y h:i A', strtotime($notification['created_at'])) ?>

                            </small>

                        </div>

                        <!-- TYPE -->

                        <div>

                            <?php if($notification['type'] == 'Success'): ?>

                                <span class="badge badge-success">

                                    S

                                </span>

                            <?php elseif($notification['type'] == 'Warning'): ?>

                                <span class="badge badge-warning">

                                    W

                                </span>

                            <?php elseif($notification['type'] == 'Danger'): ?>

                                <span class="badge badge-danger">

                                    D

                                </span>

                            <?php else: ?>

                                <span class="badge badge-info">

                                    I

                                </span>

                            <?php endif; ?>

                        </div>

                    </div>

                </a>

                <div class="dropdown-divider"></div>

            <?php endforeach; ?>

        <?php else: ?>

            <div class="dropdown-item text-center text-muted py-3">

                No new notifications

            </div>

            <div class="dropdown-divider"></div>

        <?php endif; ?>

        <!-- FOOTER -->

        <a
            href="<?php echo WEB_ROOT; ?>super_admin/notifications/index.php"
            class="dropdown-item dropdown-footer"
        >

            View All Notifications

        </a>

    </div>

</li>