<?php  include_once ("config/db.php");  include_once("config/check.php");

$COMPANY = $conn->query(" select name from adcompany where id='$companyID'   ")->fetch();

 ?>

<section class="sidebar">

  <!-- Sidebar user panel -->
      <!-- <div class="user-panel">
        <div class="pull-left image">
          <img src="dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
        </div>
        <div class="pull-left info">
          <p> <?php echo "$company_name"; ?></p>
          
        </div>
      </div> -->
  
<!-- Sidebar Menu -->
<ul class="sidebar-menu" data-widget="tree">
<li class="header" style="font-size:15px; color:white; padding-left:20px;"> <i class="fa fa-building" style="color:white;"></i> <?php echo  $COMPANY['name'];  ?> </li>

 <?php if( $_SESSION['guard']==3 ) {
       $menus  = $conn->query(" SELECT module ,a.id ,a.name,a.icon,a.displayno,a.active from (
        Select b.role,b.page ,c.name,c.module
       from sys_rights b join sys_page c on b.page=c.id where b.role='$role' ) d 
       
join sys_module a on module=a.id  where active=1  and guard=1 group by module order by displayno asc     ")->fetchAll();

  foreach($menus as $menu)
    {?>
      <li class="treeview">
          <a href="#">  
              <?php echo $menu["icon"]?> <span> <?php echo $menu["name"]; ?> </span> 
              <i class="fa fa-angle-left pull-right"></i>
          </a>

          <ul class="treeview-menu">
          <?php                  
              $stmt1 = $conn->query("  SELECT a.id,a.name,a.icon,a.displayno,a.active, b.id bid,c.page,c.role   from sys_sub_module a 
                                       JOIN sys_page b ON a.id=b.submodule  JOIN sys_rights c ON b.id=c.page 
                                       WHERE a.module= '".$menu["id"]."' and  a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC
                                                                ");
              foreach($stmt1  as $P)
                      {  
                         // submenu
                          ?>
                            <li class="treeview">
                                <a href="#">  
                                <?php echo $P["icon"]?>  <span> <?php echo $P["name"]; ?> </span> 
                                    <i class="fa fa-angle-left pull-right"></i> </a>

                                <ul class="treeview-menu">
                                <?php                  
                                    $stmt2 = $conn->query(" SELECT * FROM  sys_submodule_child   a where a.submodule= '".$P["id"]."'  and active=1 order by displayno asc  ");
                                    foreach($stmt2  as $CH)
                                            {  
                                                
                                                ?>
                                                  <li class="treeview">
                                                        <a href="<?php echo $CH["url"]; ?>">  
                                                            <?php echo $CH["icon"]?> <span> <?php echo $CH["name"]; ?> </span> 
                                                            <i class="fa fa-angle-left pull-right"></i> </a>
                                                        <ul class="treeview-menu">
                                                        <?php                  
                                                            $stmt3 = $conn->query(" SELECT a.*,b.role  FROM  sys_page a join sys_rights b  on a.id= b.page
                                                                                    where a.submodulechild= '".$CH["id"]."'   and a.active=1 and b.role= '$role' GROUP BY a.id   order by displayno asc  ");
                                                            foreach($stmt3  as $PAGE)
                                                                    {  ?>
                                                                      
                                                                     <li class=""><a href='<?php echo $PAGE["link"].''. $PAGE["url"];?>'><?php echo $PAGE["icon"]; ?> <?php echo $PAGE["name"]; ?></a></li>  
                                                            <?php } ?>        
                                                        </ul>
                                                    </li>
                                    <?php } ?>        
                                </ul>
                            </li>

                        <!-- Submenu --->

              <?php   } ?>        
          </ul>
      </li>
      
  <?php }}?>


 <!-- Guard 2 -->



   

</section>
