<?php
session_start();

error_reporting(0);

// Assuming a file with a database connection variable named $conn
include("../config/theconfig.php");

if (!isset($_SESSION["adminid"])) {
    header("Location: login.php");
    exit();
}
      
$mailreq = mysqli_query($conn, "SELECT * FROM mail WHERE reciverid='" . $_SESSION['customerid'] . "' AND status='New'");
   
if (isset($_GET["mailid"])) {
    // This block is empty in the original code. No changes needed.
}

if (isset($_SESSION['adminid'])) {
    $result = mysqli_query($conn, "SELECT * FROM mail where reciverid='" . $_SESSION['adminid'] . "' AND status='New'");
} else if (isset($_SESSION['customerid'])) {
    $result = mysqli_query($conn, "SELECT * FROM mail where reciverid='" . $_SESSION['customerid'] . "' AND status='New'");
}
?>
<?php
$query = "SELECT * from system_settings";
$result_settings = mysqli_query($conn, $query);

while ($row = mysqli_fetch_array($result_settings)) {
    $cur = $row['currency'];
}
?>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <meta name="description" content="A digitalized banking software">
      <meta name="keywords" content="Ibank, piggybank">
      <script src="../assets/js/custom/sweetalert.min.js"></script>
      <title>Admin Dashboard</title>
      <link rel="shortcut icon" href="../assets/images/favicon.ico" type="image/x-icon">
      <link rel="stylesheet" href="../assets/node_modules/flag-icon-css/css/flag-icon.min.css">
      <link rel="stylesheet" href="../assets/vendors/gaxon-icon/styles.css">
      <link rel="stylesheet" href="../assets/css/light-style-navyblue.min.css">
      <link rel="stylesheet" href="../assets/node_modules/perfect-scrollbar/css/perfect-scrollbar.css">
      <link rel="stylesheet" href="../assets/css/style.min.css">
      <style>
         .dt-side-nav__header:before{
         background-color: #ffffff;
         }
         .dt-side-nav__text{
         text-transform: none;
         }
         .dt-side-nav__link{
         color:#fff !important;
         }
         .dt-brand, .dt-header {
         background: -webkit-linear-gradient(top, #2b85bb 0%, #0950a0 100%);
         background: linear-gradient(top, #2b85bb 0%, #0950a0 100%);
         }
         .dt-sidebar{
         background: -webkit-linear-gradient(bottom, #2b85bb 0%, #0950a0 100%);
         background: linear-gradient(bottom, #2b85bb 0%, #0950a0 100%);
         }
         .btn-danger {
         color: #fff;
         background-color: #00BCD4;
         border-color: #00BCD4;
         }
         .dt-side-nav__header{
         color: #e6e6e6;
         }
         .dt-side-nav__header:not(:first-child):before{
         background-color: #ffffff4d;
         }
         body{
         color:#666; 
         }
      </style>
   </head>
   <body class="dt-sidebar--fixed dt-header--fixed">
      
      <div class="dt-root">
      <div class="dt-root__inner">
      <header class="dt-header">
         <div class="dt-header__container">
            <div class="dt-brand">
               <div class="dt-brand__tool" data-toggle="main-sidebar">
                  <i style="color:white;" class="icon icon-apps icon-sm icon-fw"></i>
               </div>
               <span class="dt-brand__logo">
               <a class="dt-brand__logo-link" href="login.php">
               <img class="" width="50" height="40" src="../assets/images/logoo.png" alt="ibank">
               </a>
               </span>
               </div>
            <div class="dt-header__toolbar">
               <form class="search-box d-none d-lg-block" action="searchresult.php" method="get">
                  <div class="input-group" >
                     <input class="form-control" name="query" placeholder="Enter Account Number or Date" value="" type="search">
                     <span class="search-icon"><i class="icon icon-revenue icon-lg"></i></span>
                     <div class="input-group-append">
                        <button class="btn btn-danger" type="submit"><i class="icon icon-search icon-lg"></i>Search
                        </button>
                     </div>
                  </div>
               </form>
               <div class="dt-nav-wrapper">
                  <ul class="dt-nav d-lg-none">
                     <li class="dt-nav__item dt-notification-search dropdown">
                        <a href="#" class="dt-nav__link dropdown-toggle no-arrow" data-toggle="dropdown"
                           aria-haspopup="true" aria-expanded="false"> <i style="color:white;" class="icon icon-search icon-fw icon-lg"></i> </a>
                        <div class="dropdown-menu">
                           <form class="search-box right-side-icon">
                              <input class="form-control  form-control-lg" name="query" type="search" placeholder="Enter Account Number or Date">
                              <button type="submit" class="search-icon"><i class="icon icon-search icon-lg"></i></button>
                           </form>
                           </div>
                        </li>
                  </ul>
                  <ul class="dt-nav">
                     <li class="dt-nav__item dt-notification dropdown">
                        <a href="#" class="dt-nav__link dropdown-toggle no-arrow" data-toggle="dropdown"
                           aria-haspopup="true" aria-expanded="false"> <i style="color:white;" class="icon icon-open-mail icon-fw"></i> </a>
                        <div class="dropdown-menu dropdown-menu-right dropdown-menu-media">
                           <div class="dropdown-menu-header">
                              <h4 class="title"> New Messages (<?php echo mysqli_num_rows($mailreq); ?>)</h4>
                           </div>
                           <div class="dropdown-menu-body ps-custom-scrollbar">
                              <div class="h-auto">
                                 <?php
                                 while ($row = mysqli_fetch_array($result)) {
                                     $rid = $row['senderid'];
                                     if (!($rid == 'admin')) {
                                         $rres = mysqli_query($conn, "SELECT * FROM customers WHERE customerid='" . $rid . "'");
                                         $rresarr = mysqli_fetch_array($rres);
                                         $recid = $rresarr['firstname'] . " " . $rresarr['lastname'];
                                     } else {
                                         $recid = $rid;
                                     }
                                     
                                     echo "
                                     
                                         <a href='readmessage.php?mailid=" . htmlspecialchars($row['mailid']) . "' class='media'>
                                     
                                     
                                     
                                             <img class='dt-avatar mr-3' src='../assets/images/user-avatar/mathew.jpg' alt='User'>
                                     
                                             <span class='media-body text-truncate'>
                                     
                                             <span class='user-name mb-1'>" . htmlspecialchars($recid) . "</span>
                                     
                                             <span class='message text-light-gray text-truncate'> " . htmlspecialchars($row['subject']) . "</span>
                                     
                                             </span>
                                     
                                             <span class='action-area h-100 min-w-80 text-right'>
                                     
                                             <span class='meta-date mb-1'>" . htmlspecialchars($row['mdatetime']) . "</span>
                                     
                                                 <span class='toggle-button' data-toggle='tooltip' data-placement='left' title='Mark as read'>
                                     
                                                     <span class='show'><i class='icon icon-dot-o icon-fw f-10 text-light-gray'></i></span>
                                     
                                                     <span class='hide'><i class='icon icon-dot icon-fw f-10 text-light-gray'></i></span>
                                     
                                                 </span>
                                     
                                                 </span> </a>
                                     
                                             ";
                                 }
                                 ?>
                              </div>
                           </div>
                           <div class="dropdown-menu-footer">
                              <a href="inbox.php" class="card-link"> See All <i class="icon icon-arrow-right icon-fw"></i>
                              </a>
                           </div>
                           </div>
                        </li>
                  </ul>
                  <ul class="dt-nav">
                     <li class="dt-nav__item dropdown">
                        <a href="#" class="dt-nav__link dropdown-toggle no-arrow dt-avatar-wrapper"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <img class="" width="50" height="40" src="../assets/images/logoo.png" alt="ibank">
                        <span class="dt-avatar-info d-none d-sm-block">
                        <span style="color:white;" class="dt-avatar-name">Administrator</span>
                        </span> </a>
                        <div class="dropdown-menu dropdown-menu-right">
                           <div class="dt-avatar-wrapper flex-nowrap p-6 mt--5 bg-gradient-purple text-white rounded-top">
                              <img class="" width="50" height="40" src="../assets/images/logoo.png" alt="ibank">
                              <span class="dt-avatar-info">
                              <span class="dt-avatar-name">Administrator</span>
                              <span class="f-12">Admin</span>
                              </span>
                           </div>
                           </a> <a class="dropdown-item" href="systemsettings.php">
                           <i class="icon icon-settings icon-fw mr-2 mr-sm-1"></i>Setting </a>
                           <a class="dropdown-item" href="logout.php"> <i class="icon icon-logout icon-fw mr-2 mr-sm-1"></i>Logout
                           </a>
                        </div>
                        </li>
                  </ul>
                  </div>
               </div>
            </div>
         </header>
      <main class="dt-main">
      <aside id="main-sidebar" class="dt-sidebar">
         <div class="dt-sidebar__container">
            <ul class="dt-side-nav">
            <li class="dt-side-nav__item dt-side-nav__header">
               <span class="dt-side-nav__text">Admin Account</span>
            </li>
            <li class="dt-side-nav__item">
               <a href="dashboard.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-menu icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Dashboard</span> </a>
            </li>
            <li class="dt-side-nav__item dt-side-nav__header">
               <span class="dt-side-nav__text">Customers</span>
            </li>
            <li class="dt-side-nav__item">
               <a href="newaccount.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">New Customer</span> </a>
            </li> 
            
            <li class="dt-side-nav__item">
               <a href="managecustomers.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Manage Customers</span> </a>
            </li>
            
            <li class="dt-side-nav__item">
               <a href="pendingtransfer.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Pending Transfer</span> </a>
            </li>
            
            <li class="dt-side-nav__item">
               <a href="viewalltransfers.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">View All Transfer</span> </a>
            </li>
            
            <li class="dt-side-nav__item">
               <a href="creditcutomer.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Credit</span> </a>
            </li>
            
           <li class="dt-side-nav__item">
               <a href="debitcutomer.php" class="dt-side-nav__link" title="Widgets"> <i class="icon icon-user-account icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Debit</span> </a>
            </li> 
            
            
            
            <li class="dt-side-nav__item dt-side-nav__header">
               <span class="dt-side-nav__text">Settings</span>
            </li>
            <li class="dt-side-nav__item">
               <a href="transfer_codes.php" class="dt-side-nav__link" title="Drag & Drop"> <i class="icon icon-send icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Transfer Codes / Pin</span> </a>
            </li>
            
            <li class="dt-side-nav__item">
               <a href="systemsettings.php" class="dt-side-nav__link" title="Drag & Drop"> <i class="icon icon-send icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Settings</span> </a>
            </li>
            
            
            
            <li class="dt-side-nav__item">
               <a href="adminpass.php" class="dt-side-nav__link" title="Drag & Drop"> <i class="icon icon-send icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Admin Password</span> </a>
            </li>
            
            <li class="dt-side-nav__item dt-side-nav__header">
               <span class="dt-side-nav__text">Logout</span>
            </li>
            <li class="dt-side-nav__item">
               <a href="logout.php" class="dt-side-nav__link" title="Drag & Drop"> <i class="icon icon-logout icon-fw icon-lg"></i>
               <span class="dt-side-nav__text">Logout</span> </a>
            </li>
            </div>
      </aside>
     