<?php

$i = 0;
$j = 0;

$arrayMenu = array( 
  array(
    'title-menu' => 'Dashboard',
    'icon' => 'fa fa-home',
    'href' => base_url('/dashboard')
  )
);

foreach ($data as $key){ 
  if($key->submenu == 0){ 
    $i++;
    // menu utama
    $arrayMenu[$i] = array(
      'title-menu' => $key->judul,
      'icon' => $key->icon,
      'href' => base_url($key->url)
    ); 
    $j = 0;
  } else {
    // menu utama dengan submenu 
    $arrayMenu[$i]['child'][$j] = array(
      'title-menu' => $key->judul,
      'icon' => $key->icon,
      'href' => base_url($key->url)
    );

    $j++;
  }
}  

?>

<!-- Main Sidebar Container -->
<aside class="main-sidebar elevation-4 sidebar-light-maroon">
  <!-- Brand Logo -->
  <center>
    <a href="./" class="brand-link navbar-white">
      <img src="./<?php echo $this->session->userdata('url_logo_kecil_'.$this->session->userdata('prefix')) ?>" alt="AdminLTE Logo" class="brand-image" >
      <span class="brand-text"><strong><?php echo strtoupper($this->session->userdata('singkatan_'.$this->session->userdata('prefix')))  ?></strong></span>
    </a>
  </center>

  <!-- Sidebar -->
  <div class="sidebar">
    <div class="user-panel mt-1 pb-1 mb-1">

      <table style="width: 100%">
        <tr>
          <td width="30" style="vertical-align: middle;">
            <div class="image">
              <img width="30px" src="<?php echo base_url('_asset/img/avatar.png'); ?>" class="img-circle elevation-2"  alt="User Image">
            </div>
          </td>
          <td>
            <div style="white-space: normal; padding: 5px 0px 10px 10px"> 
              <?php if($this->session->userdata('class_utama_'.$this->session->userdata('prefix')) == 'admin'){ ?>
                <a href="#" class="brand-text" data-toggle="modal" data-target="#modal-data" onclick="edit_profil(<?php echo $this->session->userdata('user_id_'.$this->session->userdata('prefix')) ?>)"><u><?php echo $this->session->userdata('nama_'.$this->session->userdata('prefix')) ?></u></a>
                <br>
                <select class="form-control form-control-sm" name="level_user" required onchange="ganti_user()"> 
                  <option value="" selected hidden disabled></option>
                  <?php foreach ($select_level as $key){ ?>
                    <option value="<?php echo $key->id ?>" <?php if($this->session->userdata('level_id_'.$this->session->userdata('prefix')) == $key->id){ echo 'selected'; } ?>><?php echo $key->value ?></option>
                  <?php } ?>                     
                </select>    
              <?php } else { ?>
                <a class="brand-text"><u><?php echo $this->session->userdata('nama_'.$this->session->userdata('prefix')) ?></u></a>
                <br>
                <a class="brand-text"><?php echo $this->session->userdata('level_value_'.$this->session->userdata('prefix')) ?></a>
                <a href="#" data-toggle="modal" data-target="#modal-data" onclick="edit_profil(<?php echo $this->session->userdata('user_id_'.$this->session->userdata('prefix')) ?>)"> ( <i class="far fa-edit"></i> )</a>
              <?php } ?>              
            </div>
          </td>
        </tr>
      </table>
    </div>

    <script type="text/javascript">

      function edit_profil(id)
      {
        var data = {id: id};

        $.ajax({
          url: './user/edit',
          type: 'POST',
          data: data,
          success: function(response){    
            $('#modal-view').html(response);
            $('#modal-title').html("Edit Data");
          },
          error: function (xhr, ajaxOptions, thrownError) { 
           console.log(xhr.responseText);
         }
       });
      }

      function ganti_user()
      {
        var data = {level_id: $('select[name=level_user]').val()};

        $.ajax({
          url: './user/ganti',
          type: 'POST',
          data: data,
          dataType:'json',
          success: function(response){            
            if(response.eror == 'success') window.location.href = '<?php echo base_url("login") ?>';
          },
          error: function (xhr, ajaxOptions, thrownError) { 
           console.log(xhr.responseText);
         }
       });
      }

    </script>

    <!-- Sidebar Menu -->
    <nav class="mt-2">
      <ul class="nav nav-pills nav-sidebar flex-column text-sm nav-compact" data-widget="treeview" role="menu" data-accordion="false">

        <?php foreach ($arrayMenu as $key)
        {
          if(isset($key['child']))
          {    
            ?>

            <li class="nav-item has-treeview">
              <a class="nav-link">
                <i class="nav-icon <?php echo $key['icon'] ?>"></i>
                <p style="font-size: 14px">
                  <?php echo $key['title-menu']; ?>
                  <i class="right fas fa-angle-left"></i>
                </p>
              </a>

              <ul class="nav nav-treeview">
                <?php foreach ($key['child'] as $key2) { ?>

                  <li class="nav-item">
                    <a href="<?php echo $key2['href'] ?>" 
                      class="nav-link <?php if(current_url() == $key2['href']){ echo 'active'; $title_menu =  '<i class="'.$key['icon'].'"></i> | '.$key['title-menu'].' - '.$key2['title-menu']; } ?>"
                      >
                      <table>
                        <tr>
                          <td valign="middle">                            
                            <i class="nav-icon fas fa-genderless"></i>
                          </td>
                          <td <?php if(strlen($key2['title-menu']) > 25) echo 'style="white-space: normal"'; ?>>
                            <?php echo $key2['title-menu'] ?>
                          </td>
                        </tr>
                      </table>
                    </a>
                  </li>

                <?php } ?>

              </ul>
            </li>

          <?php } else { ?>

            <li class="nav-item">
              <a href="<?php echo $key['href'] ?>" class="nav-link <?php if(current_url() == $key['href']){ echo 'active'; $title_menu = '<i class="'.$key['icon'].'"></i> | '.$key['title-menu']; } ?>">
                <i class="nav-icon <?php echo $key['icon'] ?>"></i>
                <p style="font-size: 14px"><?php echo $key['title-menu'] ?></p>
              </a>
            </li>

          <?php }
        } ?>

        <li class="nav-header"></li>
        <li class="nav-item">
          <a href="<?php echo base_url('logout') ?>" class="nav-link ">
            <i class="nav-icon fas fa-sign-out-alt"></i>
            <p style="font-size: 14px">Logout</p>
          </a>
        </li>

      </ul>
    </nav>
    <!-- /.sidebar-menu -->
  </div>
  <!-- /.sidebar -->
</aside>

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <div class="content-header">
    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-12">
          <!-- <h1 class="m-0 text-dark" id='home-title'><?php echo $title_menu ?></h1> -->
          <table width="100%">
            <tr>
              <td><h1 class="m-0 text-dark" id='home-title'><?php if(isset($title_menu)) echo $title_menu ?></h1> </td>
              <td style="text-align: right;"><a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a></td>
            </tr>
          </table>  
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.container-fluid -->
  </div>
  <!-- /.content-header -->