<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

require_once "../config/theconfig.php";


if (!isset($_SESSION["adminid"])) {
    header("Location: login.php");
    exit;
}

// Fetch system settings and admin email
$cur = '$';
$brand_logo = 'assets/images/brand/logo_1758475458.png';;
$favicon = 'assets/images/brand/favicon_1758475458.ico';;
$admin_email = 'support@defaultbank.com';
$brand_name = 'Default Bank';

$query = "SELECT currency, email, brand_logo, bank_name, favicon FROM system_settings LIMIT 1";
$res = $con->query($query);
if ($res && $res->num_rows > 0) {
    $row = $res->fetch_assoc();
    $cur = $row['currency'];
    $admin_email = $row['email'];
	$brand_name = $row['bank_name'];
    $brand_logo = $row['brand_logo'];
    $favicon = $row['favicon'];
}

// Fetch pending transfers (where status = 0)
$pending_transfers = [];
$stmt = $con->prepare("SELECT name FROM transfers WHERE status = 0");
if ($stmt) {
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $pending_transfers[] = $row;
    }
    $stmt->close();
}

// Fetch new sign-ups (where accstatus = 'verifying')
$new_signups = [];
$stmt = $con->prepare("SELECT firstname, lastname FROM customers WHERE accstatus = 'verifying'");
if ($stmt) {
    $stmt->execute();
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        $new_signups[] = $row;
    }
    $stmt->close();
}

// Calculate total new notifications
$total_notifications = count($pending_transfers) + count($new_signups);

?>

<nav class="navbar top-bar navbar-light py-0 py-xl-3">
    <div class="container-fluid p-0">
        <div class="d-flex align-items-center w-100">

            <div class="d-flex align-items-center d-xl-none">
                <?php if (!empty($brand_logo)): ?>
                    <a class="navbar-brand" href="dashboard.php">
                        <img class="navbar-brand-item h-20px" src="../<?= htmlspecialchars($brand_logo); ?>" alt="">
                    </a>
                <?php else: ?>
                    <a class="navbar-brand" href="index-2.html">
                        <img class="navbar-brand-item h-20px" src="assets/images/logo-icon.svg" alt="">
                    </a>
                <?php endif; ?>
            </div>
            <div class="navbar-expand-xl sidebar-offcanvas-menu">
                <button class="navbar-toggler me-auto p-2" type="button" data-bs-toggle="offcanvas" data-bs-target="#offcanvasSidebar" aria-controls="offcanvasSidebar" aria-expanded="false" aria-label="Toggle navigation" data-bs-auto-close="outside">
                    <i class="bi bi-list text-primary fa-fw" data-bs-target="#offcanvasMenu"></i>
                </button>
            </div>
            <div class="navbar-expand-lg ms-auto ms-xl-0">
                <div class="collapse navbar-collapse w-100 z-index-1" id="navbarTopContent">
                    <a href="systemsettings.php" class="btn btn-primary-soft mb-0"><i class="bi bi-plus-lg fa-fw"></i> Manage Settings</a>
                    </div>
                </div>
            <ul class="nav flex-row align-items-center list-unstyled ms-xl-auto">
                <li class="nav-item dropdown ms-3">
							<a class="nav-notification btn btn-light p-0 mb-0" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">
								<i class="bi bi-bell fa-fw"></i>
							</a>
							<?php if ($total_notifications > 0): ?>
								<span class="notif-badge animation-blink"></span>
							<?php endif; ?>
		
							<div class="dropdown-menu dropdown-animation dropdown-menu-end dropdown-menu-size-md shadow-lg p-0">
								<div class="card bg-transparent">
									<div class="card-header bg-transparent d-flex justify-content-between align-items-center border-bottom">
										<h6 class="m-0">Notifications <span class="badge bg-danger bg-opacity-10 text-danger ms-2"><?= $total_notifications; ?> new</span></h6>
										
									</div>
		
									<div class="card-body p-0" style="max-height: 300px; overflow-y: auto;">
										<ul class="list-group list-group-flush list-unstyled p-2">
											<?php if ($total_notifications > 0): ?>
												<?php foreach ($pending_transfers as $transfer): ?>
													<li>
														<a href="pendingtransfer.php" class="list-group-item list-group-item-action rounded notif-unread border-0 mb-1 p-3">
															<h6 class="mb-2">New Pending Transfer!</h6>
															<p class="mb-0 small">A new transfer request has been made by **<?= htmlspecialchars($transfer['name']); ?>**.</p>
															<span>Today</span>
														</a>
													</li>
												<?php endforeach; ?>
		
												<?php foreach ($new_signups as $signup): ?>
													<li>
														<a href="managecustomers.php" class="list-group-item list-group-item-action rounded notif-unread border-0 mb-1 p-3">
															<h6 class="mb-2">New Sign-up!</h6>
															<p class="mb-0 small">A new customer, **<?= htmlspecialchars($signup['firstname'] . ' ' . $signup['lastname']); ?>**, has registered.</p>
															<span>Today</span>
														</a>
													</li>
												<?php endforeach; ?>
											<?php else: ?>
												<li class="p-3 text-center text-muted">No new notifications.</li>
											<?php endif; ?>
											</ul>
									</div>
									<div class="card-footer bg-transparent text-center border-top">
										<a href="pendingtransfer.php" class="btn btn-sm btn-link mb-0 p-0">See all incoming activity</a>
									</div>
								</div>
							</div>
							</li>
                <li class="nav-item ms-3 dropdown">
                    <a class="avatar avatar-sm p-0" href="#" id="profileDropdown" role="button" data-bs-auto-close="outside" data-bs-display="static" data-bs-toggle="dropdown" aria-expanded="false">
                        <img class="avatar-img rounded-2" src="assets/images/avatar/001.png" alt="avatar">
                    </a>

                    <ul class="dropdown-menu dropdown-animation dropdown-menu-end shadow pt-3" aria-labelledby="profileDropdown">
                        <li class="px-3 mb-3">
                            <div class="d-flex align-items-center">
                                <div class="avatar me-3">
                                    <img class="avatar-img rounded-circle shadow" src="assets/images/avatar/001.png" alt="avatar">
                                </div>
                                <div>
                                    <a class="h6 mt-2 mt-sm-0" href="#"><?= htmlspecialchars($brand_name); ?></a>
                                    <p class="small m-0"><?= htmlspecialchars($admin_email); ?></p>
                                </div>
                            </div>
                        </li>

                        <li> <hr class="dropdown-divider"></li>
                        <li><a class="dropdown-item" href="adminpass.php"><i class="bi bi-bookmark-check fa-fw me-2"></i>Change Password</a></li>
                        <li><a class="dropdown-item" href="systemsettings.php"><i class="bi bi-gear fa-fw me-2"></i>Settings</a></li>
                        <li><a class="dropdown-item bg-danger-soft-hover" href="logout.php"><i class="bi bi-power fa-fw me-2"></i>Sign Out</a></li>
                    </ul>
                </li>
                </ul>
            </div>
    </div>
</nav>