<?php if ($_GET['page'] == 'menuList'){  ?>
<div class="card">
  <div class="card-body table-responsive">
    <?php
     $allMenus = array();
     $allMenuCheck=$db->query("SELECT * FROM menu  where language={$getLangCheck['language_id']} order by order_sort", PDO::FETCH_ASSOC);
     if ( $allMenuCheck->rowCount() ){
       foreach( $allMenuCheck as $line ){
          $allMenus[$line['menu_id']]['menu_id'] = $line['menu_id'];
          $allMenus[$line['menu_id']]['title'] = $line['title'];
          $allMenus[$line['menu_id']]['menu_top_id'] = $line['menu_top_id'];
          $allMenus[$line['menu_id']]['status'] = $line['status'];
          $allMenus[$line['menu_id']]['order_sort'] = $line['order_sort'];
       }
     }
     function findMenus($menus) {
        $row = array();

        foreach($menus as $menu)
        {
            if($menu['menu_top_id'] == 0) $row[] = $menu;
        }
        return $row;
     }
     function findSubMenus($topMenu,$menus,$again = 2) {
      $row = array();
      foreach($menus as $menu) {
          if($menu['menu_top_id'] == $topMenu) {
              $row[] = $menu;
          }
      }
      if(count($row) > 0) {
          foreach($row as $men) {
              if ($men['status'] == 1) {
                $status ='<i class="fas fa-eye text-success"></i>';
              }else{
                $status='<i class="fas fa-eye-slash text-danger"></i>';
              }
              global $langArray;
              global $db;
              $menusCountAsk2=$db->prepare("SELECT * FROM menu where menu_top_id=:menu_top_id");
              $menusCountAsk2->execute(array(
                'menu_top_id' => $men['menu_id']
              ));
              $menusCount2=$menusCountAsk2->rowCount();
              echo '
              <tr>
                 <td>'.$men['order_sort'].'</td>
                 <td><i class="fas fa-level-up-alt fa-rotate-90"></i>'.str_repeat('- ',$again).' '.$men['title'].'</td>
                 <td>' .$status.'</td>
                 <td>
                   <a class="btn btn-light" href="index.php?page=menuEdit&menu_id='.$men['menu_id'].'">'.$langArray['TABLE_EDIT'].'</a>
                 ';
                 if ($menusCount2 > 0 ) {
                   echo '<span class="btn btn-warning btn-sm"><i class="fas fa-exclamation-triangle"></i></span>';
                 }else{
                   echo '
                   <a class="btn btn-danger" data-toggle="modal" data-target="#dataDelete-'.$men['menu_id'].'">'.$langArray['TABLE_DELETE'].'</a>
                   <div class="modal fade" id="dataDelete-'.$men['menu_id'].'">
                     <div class="modal-dialog customDialog">
                       <div class="modal-content bg-danger">
                         <div class="modal-header">
                           <h4 class="modal-title">'.$langArray['ARE_YOU_SURE'].'</h4>
                         </div>
                         <div class="modal-footer justify-content-between">
                           <button type="button" class="btn btn-outline-light" data-dismiss="modal">'.$langArray['CANCEL_FORM'].'</button>
                           <a href="system/operation.php?menu_id='.$men['menu_id'].'&menuDelete=success" class="btn btn-outline-light"> '.$langArray['TABLE_DELETE'].'</a>
                         </div>
                       </div>
                     </div>
                   </div>
                   ';
                 }
                 echo '
                 </td>
              </tr>
              ';
              findSubMenus($men['menu_id'],$menus,$again+2);
          }
      }
     }
     $mainMenus = findMenus($allMenus);

     echo '
     <table id="" class="table table-bordered table-striped">
       <thead>
         <tr>
           <th>'. $langArray['TABLE_ORDER_SORT'] .'</th>
           <th>'. $langArray['MENU_TITLE'] .'</th>
           <th>'. $langArray['TABLE_STATUS'] .'</th>
           <th>'. $langArray['TABLE_OPERATION'] .'</th>
         </tr>
       </thead>
       <tbody>
     ';
      foreach($mainMenus as $menu) {
         if ($menu['status'] == 1) {
           $status ='<i class="fas fa-eye text-success"></i>';
         }else{
           $status='<i class="fas fa-eye-slash text-danger"></i>';
         }
         global $langArray;
         global $db;
         $menusCountAsk22=$db->prepare("SELECT * FROM menu where menu_top_id=:menu_top_id");
         $menusCountAsk22->execute(array(
           'menu_top_id' => $menu['menu_id']
         ));
         $menusCount22=$menusCountAsk22->rowCount();
          echo '
            <tr>
             <td>' .str_repeat('-',0).' '.$menu['order_sort'].'</td>
             <td><strong>' .str_repeat('-',0).' '.$menu['title'].'</strong></td>
             <td>' .str_repeat('-',0).' '.$status.'</td>
             <td>
               <a class="btn btn-light" href="index.php?page=menuEdit&menu_id='.$menu['menu_id'].'">'.$langArray['TABLE_EDIT'].'</a>
               ';
               if ($menusCount22 > 0 ) {
                 echo '<span class="btn btn-warning btn-sm"><i class="fas fa-exclamation-triangle"></i></span>';
               }else{
                 echo '
                 <a class="btn btn-danger" data-toggle="modal" data-target="#dataDelete-'.$menu['menu_id'].'">'.$langArray['TABLE_DELETE'].'</a>
                 <div class="modal fade" id="dataDelete-'.$menu['menu_id'].'">
                   <div class="modal-dialog customDialog">
                     <div class="modal-content bg-danger">
                       <div class="modal-header">
                         <h4 class="modal-title">'.$langArray['ARE_YOU_SURE'].'</h4>
                       </div>
                       <div class="modal-footer justify-content-between">
                         <button type="button" class="btn btn-outline-light" data-dismiss="modal">'.$langArray['CANCEL_FORM'].'</button>
                         <a href="system/operation.php?menu_id='.$menu['menu_id'].'&menuDelete=success" class="btn btn-outline-light"> '.$langArray['TABLE_DELETE'].'</a>
                       </div>
                     </div>
                   </div>
                 </div>
                 ';
               }
               echo '

             </td>
            </tr>
          ';
          findSubMenus($menu['menu_id'],$allMenus);
      }
     echo '
         </tr>
       </tbody>
       <tfoot>
         <tr>
           <th>'. $langArray['TABLE_ORDER_SORT'] .'</th>
           <th>'. $langArray['MENU_TITLE'] .'</th>
           <th>'. $langArray['TABLE_STATUS'] .'</th>
           <th>'. $langArray['TABLE_OPERATION'] .'</th>
         </tr>
       </tfoot>
     </table>
     ';
    ?>
  </div>
</div>
<?php }elseif($_GET['page'] == 'menuAdd'){  ?>
<div class="card">
  <div class="card-body">
    <form action="system/operation.php" method="post" enctype="multipart/form-data" data-parsley-validate>
      <input type="hidden" name="language_id" value="<?=$getLangCheck["language_id"] ?>">
      <div class="row">
        <div class="col-md-4">
          <div class="card card-secondary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="menu_top_id"><?php echo $langArray['GLOBAL_FORM_MENU_TOP']; ?></label>
                   <?php
                    $allMenus = array();
                    $allMenuCheck=$db->query("SELECT * FROM menu where language={$getLangCheck['language_id']} order by menu_id asc", PDO::FETCH_ASSOC);
                    if ( $allMenuCheck->rowCount()){
                      foreach( $allMenuCheck as $line ){
                         $allMenus[$line['menu_id']]['menu_id'] = $line['menu_id'];
                         $allMenus[$line['menu_id']]['title'] = $line['title'];
                         $allMenus[$line['menu_id']]['menu_top_id'] = $line['menu_top_id'];
                      }
                    }
                    function findMenus($menus){
                       $row = array();
                       foreach($menus as $menu){
                         if($menu['menu_top_id'] == 0) $row[] = $menu;
                       }
                       return $row;
                    }
                    function findSubMenus($topMenu,$menus,$again = 2){
                     $row = array();
                     foreach($menus as $menu){
                       if($menu['menu_top_id'] == $topMenu){
                         $row[] = $menu;
                       }
                     }
                     if(count($row) > 0){
                       foreach($row as $men){
                         echo '<option value="'.$men['menu_id'].'">'.str_repeat('- ',$again).' '.$men['title'].'</option>';
                         findSubMenus($men['menu_id'],$menus,$again+1);
                       }
                     }
                    }
                    $mainMenus = findMenus($allMenus);
                    echo '
                      <select class="select2 form-control" name="menu_top_id">
                        <option value="0">'.$langArray['GLOBAL_FORM_MENU_TOP'].'</option>
                    ';
                     foreach($mainMenus as $menu){
                       echo '
                        <option value="'.$menu['menu_id'].'">'.str_repeat('- ',0).' '.$menu['title'].'</option>
                       ';
                       findSubMenus($menu['menu_id'],$allMenus);
                     }
                    echo '</select>';
                   ?>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="title"><?php echo $langArray['GLOBAL_FORM_TABLE_TITLE']; ?></label>
                <input type="text" name="title" class="form-control">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="ready_link"><?php echo $langArray['GLOBAL_READY_LINK']; ?></label>
                  <select class="form-control select2" name="ready_link">
                    <option value="manualLink"><?php echo $langArray['GLOBAL_MANUAL_LINK']; ?></option>
                    <?php
                      $allPostAsk->execute(array(
                        'post_type' => 'post_page',
                        'language'  => $getLangCheck['language_id']
                      ));
                      while ($allPostCheck=$allPostAsk->fetch(PDO::FETCH_ASSOC)){
                    ?>
                      <option value="<?php echo $allPostCheck['post_id']; ?>"><?php echo $allPostCheck['title']; ?></option>
                    <?php } ?>
                  </select>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="link"><?php echo $langArray['GLOBAL_MANUAL_LINK']; ?></label>
                <input type="text" name="link" class="form-control">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="menuList" class="btn btn-secondary"><?php echo $langArray['CANCEL_FORM']; ?></a>
            <input type="submit" value="<?php echo $langArray['CREATE_FORM']; ?>" class="btn btn-success float-right" name="menuAdd">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-8">
          <div class="card card-primary">
            <div class="card-body">
              <div class="row">
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="status"><?php echo $langArray['TABLE_STATUS']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="status" name="status[]" checked data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="new_tab"><?php echo $langArray['TABLE_NEW_TAB']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="new_tab" name="new_tab[]" data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="footer_menu"><?php echo $langArray['TABLE_FOOTER_MENU']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="footer_menu" name="footer_menu[]" data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="order_sort"><?php echo $langArray['TABLE_ORDER_SORT']; ?></label>
                    <input type="number" name="order_sort" class="form-control" value="<?php echo $menuCount+1 ?>">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'menuEdit'){  ?>
<?php
  $askData=$db->prepare("SELECT * FROM menu where menu_id=:menu_id");
  $askData->execute(array(
    'menu_id' => $_GET["menu_id"]
    ));
  $checkData=$askData->fetch(PDO::FETCH_ASSOC);
  $foo = $checkData["menu_top_id"];
  // $_GET null or nothing
  if ($_GET['menu_id'] == null) {
    Header("Location:menuList");
  }elseif($_GET['menu_id'] != $checkData['menu_id']){
    Header("Location:menuList");
  }
?>
<div class="card">
  <div class="card-body">
    <form action="system/operation.php" method="post" enctype="multipart/form-data" data-parsley-validate>
      <input type="hidden" name="menu_id" value="<?=$checkData["menu_id"] ?>">
      <div class="row">
        <div class="col-md-4">
          <div class="card card-secondary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="menu_top_id"><?php echo $langArray['GLOBAL_FORM_MENU_TOP']; ?></label>
                <?php
                 $allMenus = array();
                 $allMenuCheck=$db->query("SELECT * FROM menu where language={$getLangCheck['language_id']}  order by menu_id asc", PDO::FETCH_ASSOC);
                 if ( $allMenuCheck->rowCount() ){
                   foreach( $allMenuCheck as $line ){
                      $allMenus[$line['menu_id']]['menu_id'] = $line['menu_id'];
                      $allMenus[$line['menu_id']]['title'] = $line['title'];
                      $allMenus[$line['menu_id']]['menu_top_id'] = $line['menu_top_id'];
                   }
                 }
                 function findMenus($menus) {
                    $row = array();
                    foreach($menus as $menu) {
                      if($menu['menu_top_id'] == 0) $row[] = $menu;
                    }
                    return $row;
                 }
                 function findSubMenus($topMenu,$menus,$again = 2){
                  $row = array();
                  foreach($menus as $menu) {
                    if($menu['menu_top_id'] == $topMenu){
                      $row[] = $menu;
                    }
                  }
                  if(count($row) > 0) {
                    foreach($row as $men){
                      echo '<option ';
                      echo $men['menu_id'] == $GLOBALS["foo"] ? ' selected="selected"' : '';
                      echo ' value="'.$men['menu_id'].'">'.str_repeat('- ',$again).' '.$men['title'].'</option>';
                      findSubMenus($men['menu_id'],$menus,$again+1);
                    }
                  }
                 }
                 $mainMenus = findMenus($allMenus);
                 echo '
                   <select class="select2 form-control" name="menu_top_id">
                     <option '. ($checkData['menu_top_id'] == 0 ? ' selected="selected"' : '').' value="0">'.$langArray['GLOBAL_FORM_MENU_TOP'].'</option>
                 ';
                  foreach($mainMenus as $menu){
                    echo '
                     <option '. ($menu['menu_id']== $checkData['menu_top_id'] ? ' selected="selected"' : '').' value="'.$menu['menu_id'].'">'.str_repeat('- ',0).' '.$menu['title'].'</option>
                    ';
                    findSubMenus($menu['menu_id'],$allMenus);
                  }
                 echo '</select>';
                ?>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="title"><?php echo $langArray['GLOBAL_FORM_TABLE_TITLE']; ?></label>
                <input type="text" name="title" class="form-control" value="<?php echo $checkData['title'] ?>">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="ready_link"><?php echo $langArray['GLOBAL_READY_LINK']; ?></label>
                  <select class="form-control select2" name="ready_link">
                    <option value="manualLink"><?php echo $langArray['GLOBAL_MANUAL_LINK']; ?></option>
                    <?php
                      $allPostAsk->execute(array(
                        'post_type' => 'post_page',
                        'language'  => $getLangCheck['language_id']
                      ));
                      while ($allPostCheck=$allPostAsk->fetch(PDO::FETCH_ASSOC)){
                    ?>
                      <option <?php echo $checkData['ready_link'] == $allPostCheck['post_id'] ? 'selected=""' : ''; ?> value="<?php echo $allPostCheck['post_id']; ?>"><?php echo $allPostCheck['title']; ?></option>
                    <?php } ?>
                  </select>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="link"><?php echo $langArray['GLOBAL_MANUAL_LINK']; ?></label>
                <input type="text" name="link" class="form-control" value="<?php echo $checkData['link'] ?>">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="menuList" class="btn btn-secondary"><?php echo $langArray['CANCEL_FORM']; ?></a>
            <input type="submit" value="<?php echo $langArray['UPDATE_FORM']; ?>" class="btn btn-warning float-right" name="menuUpdate">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-8">
          <div class="card card-primary">
            <div class="card-body">
              <div class="row">
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="status"><?php echo $langArray['TABLE_STATUS']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="status" name="status[]" <?php echo $checkData['status'] == "1" ? 'checked=""' : ''; ?> data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="new_tab"><?php echo $langArray['TABLE_NEW_TAB']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="new_tab" name="new_tab[]" <?php echo $checkData['new_tab'] == "1" ? 'checked=""' : ''; ?> data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="footer_menu"><?php echo $langArray['TABLE_FOOTER_MENU']; ?></label>
                    <div class="input-group">
                      <input type="checkbox" id="footer_menu" name="footer_menu[]" <?php echo $checkData['footer_menu'] == "1" ? 'checked=""' : ''; ?> data-bootstrap-switch data-off-color="danger" data-on-color="success">
                    </div>
                  </div>
                </div>
                <div class="col-md-3">
                  <!-- #form-group -->
                  <div class="form-group">
                    <label for="order_sort"><?php echo $langArray['TABLE_ORDER_SORT']; ?></label>
                    <input type="number" name="order_sort" class="form-control" value="<?php echo $checkData['order_sort'] ?>">
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php } ?>
