<?php // include_once ("../config/db.php");  include_once("../config/check.php");
     
     $STORE = $conn->query(" select name from branches where id='$storeID'   ")->fetch();
     
 ?>

<section class="sidebar">

<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  $STORE['name'];  ?> </li> -->
<!--<li class="header">MAIN NAVIGATION</li>-->

<?php  if( $_SESSION['guard']==1)
        {
                $subs  = $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($subs as $sub)
                    {
             
                          if ( $mycurrentpage =='' )  { $myactivemenu =0 ; } else {
                                $activemenu = $conn->query("SELECT module,submodule,submodulechild FROM sys_page  where url='$mycurrentpage'    ")->fetch(); 
                                $activemodule  = $activemenu["module"];
                                $activesubmodule  = $activemenu["submodule"];
                                $activesubmodulechild  = $activemenu["submodulechild"];
                                
                              }

                          if( $sub["id"] == $activemodule )
                            {
                              ?>  
                              
                                  <li class="treeview active">
                                    <a href="#">  <?php echo $sub["icon"]; ?>     <span>  <?php echo $sub["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.module= '".$sub["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   
                            }  
                            // End  Active module end
                          else 
                            { ?>
                                <li class="treeview">
                                    <!-- Module listing -->
                                    <a href="">  <?php echo $sub["icon"]; ?>     <span>  <?php echo $sub["name"]; ?>  </span>   <i class="fa fa-angle-left pull-right"></i> </a>
                                    <!-- New sorting -->
                                     <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.module= '".$sub["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>
                                
                                    <!--end new sorting -->
                                </li>
                              
                             <?php
                            }     
                    }
        }
        // Second guard
        if( $_SESSION['guard']==0)
        {
                $subs  = $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>2   group by module order by displayno asc   ")->fetchAll();
                 foreach($subs as $sub)
                    {
             
                          if ( $mycurrentpage =='' )  { $myactivemenu =0 ; } else {
                                $activemenu = $conn->query("SELECT module,submodule,submodulechild FROM sys_page  where url='$mycurrentpage'    ")->fetch(); 
                                $activemodule  = $activemenu["module"];
                                $activesubmodule  = $activemenu["submodule"];
                                $activesubmodulechild  = $activemenu["submodulechild"];
                                
                              }

                          if( $sub["id"] == $activemodule )
                            {
                              ?>
                                  <li class="treeview active">
                                    <a href="#">  <?php echo $sub["icon"]; ?>     <span>  <?php echo $sub["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= '".$sub["id"]."' and  a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC      ");
                                                foreach($stmt1  as $P) // Listing all submodules 
                                                        { 
                                                          
                                                            // if ( $mycurrentpage =='' )    { $myactiveSBM =0 ; }
                                                            //  else {
                                                            //          $activeSBM = $conn->query("SELECT submodule FROM sys_page  where url='$mycurrentpage'   order by displayno asc   ")->fetch(); 
                                                            //          $myactiveSBM  = $activeSBM["submodule"]  ;
                                                            //       }
                                    
                                                                if( $P["id"] == $activesubmodule )
                                                                  {
                                                                    ?>
                                                                          <li class="treeview active">
                                                                              <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">
                                                                                <!-- sub menu child -->
                                                                                    <?php  $CHILD = $conn->query(" SELECT a.id,a.name,a.icon,a.displayno,a.active, b.id bid,c.page,c.role   from sys_submodule_child a 
                                                                                                                  JOIN sys_page b ON a.id=b.submodulechild 
                                                                                                                  JOIN sys_rights c ON b.id=c.page 
                                                                                                                  WHERE a.submodule= '".$P["id"]."' AND   a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC 
                                                                                                                    ");
                                                                                        foreach($CHILD  as $CH)
                                                                                                { 
                                                                                                  // if ( $mycurrentpage =='' )  
                                                                                                  //     { $myactiveSBMC =0 ; }
                                                                                                  //   else {
                                                                                                  //           $activeSBMC = $conn->query("SELECT submodulechild FROM sys_page  where url='$mycurrentpage'   order by displayno asc   ")->fetch(); 
                                                                                                  //           $myactiveSBMC  = $activeSBM["submodulechild"]  ;
                                                                                                  //         }
                                                                            
                                                                                                        if( $CH["id"] == $activesubmodulechild )
                                                                                                          {
                                                                                                            ?>
                                                                                                                  <li class="treeview active">
                                                                                                                      <a href="#">   <?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["url"];?>'><?php echo $PAGE["icon"]; ?> <?php echo $PAGE["name"]; ?></a></li>  
                                                                                                                            <?php } ?>        
                                                                                                                        </ul>
                                                                                                                      

                                                                                                                  </li>
                                                                                                            <?php
                                                                                                          }
                                                                                                        else 
                                                                                                          {  ?>
                                                                                                                  <li class="treeview">
                                                                                                                      <!-- Submodule listing -->
                                                                                                                      <a href="#">   <?php echo $CH["icon"]?> <span> <?php echo $CH["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                                      <!--page -->
                                                                                                                        
                                                                                                                      <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>
                                
                                                                                                                        <ul class="treeview-menu">
                                                                                                                            <!-- Submodule child  from a submodule here query -->
                                                                                                                                <?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="treeview">
                                                                                                                                                  <!-- Submodule listing -->
                                                                                                                                                  <a href="../<?php echo $PAGE["link"].''.$PAGE["url"]; ?>">   <?php echo $PAGE["icon"]?> <span> <?php echo $PAGE["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                                                                
                                                                                                                                          </li>
                                                                                                                                <?php } ?>        
                                                                                                                        </ul>
                                                                                                                      <!--page -->
                                                                                                                    
                                                                                                              </li>
                                                                                                            <?php
                                                                                                            
                                                                                                          }
                                                                                                    
                                                                                                } ?>   
                                                                                <!-- sub menu child -->
                                                                                      
                                                                              </ul>
                                                                          </li>
                                                                    <?php
                                                                  }
                                                                else 
                                                                  { ?> 
                                                                    <li class="treeview">
                                                                    <!-- Submodule listing -->
                                                                    <a href="#">   <?php echo $P["icon"]?> <span> <?php echo $P["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                    
                                                                    <!-- CHILD QUERY -->
                                                                        <ul class="treeview-menu">
                                                                                <!-- Submodule child  from a submodule here query -->
                                                                                <?php $stmt2 = $conn->query(" SELECT a.id,a.name,a.icon,a.displayno,a.active, b.id bid,c.page,c.role   from sys_submodule_child a 
                                                                                                                  JOIN sys_page b ON a.id=b.submodulechild 
                                                                                                                  JOIN sys_rights c ON b.id=c.page 
                                                                                                                  WHERE a.submodule= '".$P["id"]."' AND   a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC     ");
                                                                                    foreach($stmt2  as $CH)
                                                                                          {  ?>
                                                                                                <li class="treeview">
                                                                                                        <!-- Submodule listing -->
                                                                                                        <a href="#">   <?php echo $CH["icon"]?> <span> <?php echo $CH["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                        <!--page -->
                                                                                                          
                                                                                                        <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>
                  
                                                                                                          <ul class="treeview-menu">
                                                                                                              <!-- Submodule child  from a submodule here query -->
                                                                                                                  <?php $stmt3 = $conn->query(" SELECT * FROM  sys_page  a where a.submodulechild= '".$CH["id"]."' and active=1   order by displayno asc   ");
                                                                                                                    foreach($stmt3  as $PAGE)
                                                                                                                      {  ?>
                                                                                                                            <li class="treeview">
                                                                                                                                    <!-- Submodule listing -->
                                                                                                                                    <a href="../<?php echo $PAGE["link"].''.$PAGE["url"]; ?>">   <?php echo $PAGE["icon"]?> <span> <?php echo $PAGE["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                                                  
                                                                                                                            </li>
                                                                                                                  <?php } ?>        
                                                                                                          </ul>
                                                                                                        <!--page -->
                                                                                                      
                                                                                                </li>
                                                                                    <?php } ?>        
                                                                        </ul>
                                                                    <!-- Child Query -->
                                                                    
                                                                </li> 

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

                            
                            <?php   
                            }  
                            // End  Active module end
                          else 
                            { ?>
                                <li class="treeview">
                                    <!-- Module listing -->
                                    <a href="">  <?php echo $sub["icon"]; ?>     <span>  <?php echo $sub["name"]; ?>  </span>   <i class="fa fa-angle-left pull-right"></i> </a>
                                    <!-- New sorting -->
                                    <ul class="treeview-menu">
                                      <!-- Submodule from a module here query -->
                                      <?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= '".$sub["id"]."' and   a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC   ");
                                          foreach($stmt1  as $SBM)
                                                  {  ?>
                                                          <li class="treeview">
                                                              <!-- Submodule listing -->
                                                              <a href="#">   <?php echo $SBM["icon"]?> <span> <?php echo $SBM["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                              <!-- CHILD QUERY -->
                                                                  <ul class="treeview-menu">
                                                                          <!-- Submodule child  from a submodule here query -->
                                                                          <?php $stmt2 = $conn->query("SELECT a.id,a.name,a.icon,a.displayno,a.active, b.id bid,c.page,c.role   from sys_submodule_child a 
                                                                                                                  JOIN sys_page b ON a.id=b.submodulechild 
                                                                                                                  JOIN sys_rights c ON b.id=c.page 
                                                                                                                  WHERE a.submodule= '".$SBM["id"]."' AND   a.active =1 AND role='$role' GROUP BY a.id  order BY a.displayno ASC   ");
                                                                              foreach($stmt2  as $CH)
                                                                                    {  ?>
                                                                                          <li class="treeview">
                                                                                                  <!-- Submodule listing -->
                                                                                                  <a href="#">   <?php echo $CH["icon"]?> <span> <?php echo $CH["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                  <!--page -->
                                                                                                    
                                                                                                  <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>

                                                                                                    <ul class="treeview-menu">
                                                                                                        <!-- Submodule child  from a submodule here query -->
                                                                                                            <?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="treeview">
                                                                                                                              <!-- Submodule listing -->
                                                                                                                              <a href="../<?php echo $PAGE["link"].''.$PAGE["url"]; ?>">   <?php echo $PAGE["icon"]?> <span> <?php echo $PAGE["name"]; ?> </span> <i class="fa fa-angle-left pull-right"></i> </a>
                                                                                                                            
                                                                                                                      </li>
                                                                                                            <?php } ?>        
                                                                                                    </ul>
                                                                                                  <!--page -->
                                                                                                
                                                                                          </li>
                                                                              <?php } ?>        
                                                                  </ul>
                                                              <!-- Child Query -->
                                                              
                                                          </li>
                                          <?php }  ?>        
                                  </ul>
                                    <!--end new sorting -->
                                </li>
                              
                             <?php
                            }     
                    }
        }
        
            ?>
    </ul> 
 </section>
