<ul class="navbar-nav bg-gradient-primary sidebar sidebar-dark accordion" id="accordionSidebar">

  <!-- Sidebar - Brand -->
  <a class="sidebar-brand d-flex align-items-center justify-content-center" href="<?php echo base_url(); ?>">
    <div class="sidebar-brand-icon rotate-n-15">
      <img src="<?php echo base_url(); ?>img/swlogo_new.png" width="48px;">
    </div>
    <div class="sidebar-brand-text mx-3"><?php echo @$var->txt_shortname; ?></div>
  </a>

  <!-- Divider -->
  <hr class="sidebar-divider my-0">

  <!-- Nav Item - Dashboard -->
  <li class="nav-item">
    <a class="nav-link" href="<?php echo base_url(); ?>">
      <i class="fas fa-fw fa-tachometer-alt"></i>
      <span>Dashboard</span></a>
  </li>
  <!-- Divider -->
  <hr class="sidebar-divider my-0">

  <?php
  // $sql = "SELECT * FROM sys_module WHERE module LIKE '#%' AND record_status ='N' ORDER BY sort ASC";
  $sql = "SELECT * FROM sys_module 
          WHERE module IN (SELECT DISTINCT sys_module.parent_module FROM sys_module 
          INNER JOIN sys_permission ON sys_module.module = sys_permission.module 
          WHERE sys_permission.group_id = '" . $this->session->userdata('user_profile')->group_id . "' 
          AND sys_module.record_status ='N' AND sys_module.is_active ='1' ) 
          AND record_status ='N'
          AND sys_module.is_active ='1' 
          ORDER BY sort ASC";
  foreach ($this->db->query($sql)->result() as $item) {
    $nSubMenu = $this->db->query("SELECT * FROM sys_module WHERE parent_module = '" . $item->module . "' AND record_status ='N' AND is_active ='1' ")->num_rows();
  ?>
    <li class="nav-item">
      <a class="nav-link collapsed" href="<?php echo ($nSubMenu === 0 ? base_url($item->module) : '#'); ?>" data-toggle="collapse" data-target="#collapse<?php echo md5($item->module); ?>" aria-expanded="true" aria-controls="collapse<?php echo $item->module; ?>">
        <i class="fas <?php echo $item->module_icon; ?>"></i>
        <span> <?php echo $item->module_name; ?></span>
      </a>
      <?php if ($nSubMenu > 0) {
      ?>
        <div id="collapse<?php echo md5($item->module); ?>" class="collapse" aria-labelledby="heading<?php echo md5($item->module); ?>" data-parent="#accordionSidebar">
          <div class="bg-white py-2 collapse-inner rounded">
            <?php
            $sql = "SELECT sys_module.* FROM sys_module
            INNER JOIN sys_permission ON sys_module.module = sys_permission.module
            AND sys_permission.group_id = '" . $this->session->userdata('user_profile')->group_id . "'
            WHERE sys_module.parent_module = '" . $item->module . "'
            AND sys_module.record_status ='N'
            AND sys_module.is_active ='1'
            AND sys_module.module_description NOT IN('[hide]')
            ORDER BY sys_module.sort ASC";
            foreach ($this->db->query($sql)->result() as $item2) {
            ?>
              <a href="<?php echo base_url($item2->module); ?>" class="collapse-item">
                <i class="fas <?php echo $item2->module_icon; ?>"></i>
                <?php echo $item2->module_name; ?>
              </a>
            <?php
            }
            ?>
          </div>
        </div>
      <?php
      }
      ?>
    </li>
  <?php
  }
  ?>
  <!-- Divider -->
  <hr class="sidebar-divider">
  <li class="nav-item">
    <a class="nav-link" href="#" data-toggle="modal" data-target="#logoutModal">
      <i class="nav-icon fas fa-power-off"></i>
      Logout
    </a>
  </li>
</ul>