<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
$user_level = $_SESSION['user_level'];
$xyy = $_SESSION['xyy'];
?>

<script>
    document.addEventListener("DOMContentLoaded", function() {

      function sendSearchPositionToParent() {
        const input = document.getElementById("menuSearch");
        if (!input) return;

        const rect = input.getBoundingClientRect();

        // Send position and size to parent
        window.parent.postMessage({
          type: 'searchPosition',
          left: rect.left,
          top: rect.bottom,
          width: rect.width
        }, "*");
      }

      document.getElementById("menuSearch").addEventListener("focus", sendSearchPositionToParent);
      document.getElementById("menuSearch").addEventListener("keyup", sendSearchPositionToParent);


      const menuSearch = document.getElementById("menuSearch");
      const resultsDiv = document.getElementById("searchResults");

      if (!menuSearch || !resultsDiv) return;

      let allLinks = [];

      fetch('menuLinks.php')
        .then(response => response.text())
        .then(html => {
          const parser = new DOMParser();
          const doc = parser.parseFromString(html, "text/html");
          allLinks = Array.from(doc.querySelectorAll("a"))
            .filter(a => a.getAttribute("href") && a.getAttribute("href") !== "#");
        })
        .catch(error => {
          console.error("Failed to load menu links:", error);
        });

      // 2. Handle search input
      menuSearch.addEventListener("keyup", function() {
        let input = this.value.toLowerCase();
        let results = [];
        let seen = new Set();

        if (input.trim() === "") {
          resultsDiv.innerHTML = "";
          resultsDiv.style.display = "none";
          return;
        }

        allLinks.forEach(link => {
          const text = link.textContent.trim().toLowerCase();
          const href = link.getAttribute("href");

          if (!seen.has(href) && text.includes(input)) {
            seen.add(href);
            results.push(`<a href="${href}" target="main">${link.textContent.trim()}</a>`);
          }
        });

        const html = results.length ?
          results.join("") :
          "<p>No matches found</p>";


        if (parent && parent.showGlobalSearchResults) {
          parent.showGlobalSearchResults(html);
        }

      });
    });
  </script>
  <script>
    document.addEventListener("click", function() {
      if (window.parent) {
        window.parent.postMessage("hideSearchDropdown", "*");
      }
    });
  </script>
<script type="text/javascript">
   function timedMsg() {
      //alert('Change your password Immediately\n - Admin');
      var t = setTimeout("timedMsg()", 500)
   }
</script>
<?
include("opendb.php");
$br = mysqli_query($db, "select * from branch where brcode='$brcode'");
$brrow = mysqli_fetch_row($br);

?>

<body bgcolor="red" leftmargin=0 onLoad=timedMsg()>
   <link rel="stylesheet" href="css/bootstrap.css" rel="stylesheet">
   <!-- Template CSS -->
   <link rel="stylesheet" href="css/nexus.css" rel="stylesheet">
   <style>
      body {
         background-image: url("images/backg.jpeg");
      }

      td {
         color: #FF0000;
         font-size: 15px;
         font-weight: bold;
      }

      #hornav ul#hornavmenu {
         display: inline-block;
         margin: 0 0 -6px;
         padding: 0px;
         width: 1000px;
      }

      #hornav .container {
         background-color: #1d39a8;
         box-shadow: 0 -2px 0 #33747a inset, 0 0 20px rgba(0, 0, 0, 0.1);
         min-height: 60px;
         display: block;
      }
      #menuSearch {
         padding: 6px 10px;
         border-radius: 8px;
         border: none;
         width: 200px;
      }
      #searchResults {
         position: absolute;
         top: 100%;
         left: -65;
         width: 300px;
         background: #525252;
         border: 1px solid #ccc;
         border-radius: 6px;
         box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
         margin-top: 4px;
         max-height: 300px;
         overflow-y: auto;
         overflow-x: hidden;
         box-sizing: border-box;
         display: none;
         z-index: 9999;
      }

      #searchResults a {
         display: block;
         padding: 5px 6px;
         color: #fff !important;
         text-decoration: none;
         font-size: 14px;
         width: 100%;
      }

      #searchResults a:hover {
         background-color: #0082e7;
      }
   </style>
   <base target=contents>
   <div id="hornav">
      <div class="container no-padding border-bottom">
         <div class="col-md-8 no-padding">
            <ul id="hornavmenu" class="nav navbar-nav" style="display: flex ; justify-content: space-between; align-items: center;">
               <li><img src="img/psn.jpeg" alt="PSN Telcon" align='right' height="50" width="100"></img></li>
               
               <?
               if($user != 'tom'){
               ?>
               <li>
                  <a href='vehmnu.php'>Vehicle</a>
               </li>
               <?
               }
               ?>
               <?
               $us = mysqli_query($db, "select level from bruser where username='$user'") or die(mysqli_error());
               $r = mysqli_fetch_row($us);
               $user_level = $r[0];

               if ($user_level == 0 || $user_level == 12 || $user_level == 11 || $user_level == 10) {
                  print "<script>document.location='exetop.php'</script>";
                  return;
               }
               if ($user_level > 5) {
               ?>
                  <li>
                     <a href='dms/dmsmnu.php'>DMS</a>
                  </li>
               <?
               }
               if ($user_level >= '2' && $user_level <= 6 || $user == 'admin') {
               ?>
                  <li>
                     <a href='acmnu.php'>ACCOUNTS</a>
                  </li>
                  
                  <?
                  if($user != 'tom'){
                  ?>
                  <li>
                     <a href='utilmnu.html'>UTILITY</a>
                  </li>
                  <?
                  }
                  ?>
               <?
               }
               if ($user_level > '2' && $user_level <= 5 || $user == 'admin') {
               ?>
                  <li>
                     <a href='amastmnu.php'>ASSET MASTER</a>
                  </li>
                  <?
                  if ($brcode == 'PSN841' || $brcode == 'PSN705' || $brcode == 'PSN638' || $brcode == 'PSN880') {
                  ?>
                     <!-- <li>
                                        <a href='sparemnu.php'>SPARES</a>
                                    </li> -->
                     <li>
                        <a href='servmnu.php'>SERVICE</a>
                     </li>

                     <!-- <li>
                                       <a href='mastermnu.php'>GEN MASTER</a>
                                    </li> -->
                  <?
                  }
               }
               if ($user_level > 5 || $user == 'admin') {
                  ?> <li>
                     <a href='setupmnu.php'>SETUP</a>
                  </li>
               <?
               }
               ?>
               <li>
                  <a href='abt.html'>ABOUT</a>
               </li>
               <li>
                  <a href='sft.html'>SOFTWARE CELL</a>
               </li>
               <li>
                  <a href='logout.php' onMouseOver="window.status='Logout';return true" ; onMouseOut="window.status='';return true" ;>Logout</a>
               </li>
               <div class="nav-right" style="position: relative; display: flex; align-items: center; gap: 15px; color:#2f4050;">
                  <input type="text" id="menuSearch" placeholder="🔍 Search..." autocomplete="off">
                  <div id="searchResults"></div>
               </div>
               <?
               if ($xyy <= 5) {
                  print "<script>alert('Change your password Immediately');</script>";
               }
               ?>

            </ul>
         </div>

      </div>
      <TABLE WIDTH=100% border=0>
         <tr>
            <td align=left>
               PSN AUTO-Globally Accessible Administration Software</TD>
            <td align=left bgcolor='green' style="background: #97f597; border-radius: 5px; padding: 3px; ">Branch: <b><? print $branch . "[2025-2026]"; ?></b></td>
            <td align=left> User: <b><? print $user ?><b></td>
            <td><? print "Password expires in " . $xyy . " days"; ?></td>
         </tr>
      </TABLE>
   </div>

