<?php if ($_GET['page'] == 'postList'){  ?>
<div class="card">
  <div class="card-body table-responsive">
    <table id="dataTable" class="table table-bordered table-striped">
      <thead>
        <tr>
          <th><?php echo $langArray['TABLE_ORDER_SORT']; ?></th>
          <th><?php echo $langArray['TABLE_IMAGE']; ?></th>
          <th><?php echo $langArray['GLOBAL_FORM_TABLE_CATEGORIES']; ?></th>
          <th><?php echo $langArray['TABLE_DATE']; ?></th>
          <th><?php echo $langArray['GLOBAL_FORM_TABLE_TITLE']; ?></th>
          <th><?php echo $langArray['HOMEPAGE_STATUS']; ?></th>
          <th><?php echo $langArray['TABLE_OPERATION']; ?></th>
        </tr>
      </thead>
      <tbody>
          <?php
            $allPostAsk->execute(array(
              'post_type' => 'post_post',
              'language'  => $getLangCheck['language_id']
            ));
            while ($allPostCheck=$allPostAsk->fetch(PDO::FETCH_ASSOC)){
          ?>
          <tr>
            <td><?php echo $allPostCheck['order_sort']; ?></td>
            <td><img src="../<?php echo $allPostCheck['banner']; ?>" width="100"></td>
            <td>
              <?php
                $categories = explode(',', $allPostCheck['categories']);
                $x = count($categories);
                  for ($i=0; $i < $x; $i++) {
                    $idd=$categories[$i];
                    $allCategoriesIdAsk->execute(array(
                      'categories_id'   => $idd,
                      'categories_type' => "post_categories",
                      'language'        => $getLangCheck['language_id']
                    ));
                    $allCategoriesCheck=$allCategoriesIdAsk->fetch(PDO::FETCH_ASSOC);
                    echo '<a target="_blank" href="'.$generalSettingCheck['site_url'] . $allCategoriesCheck['seo_url'].'">'.$allCategoriesCheck['title'].'</a>' .'<br>';
                  }
               ?>
            </td>
            <td><?php echo addAgo($allPostCheck['dates']); ?></td>
            <td><a target="_blank" href="<?php echo $generalSettingCheck['site_url'] . $allPostCheck['seo_url']; ?>"><?php echo $allPostCheck['title']; ?></a></td>
            <td align="center">
              <?php if ($allPostCheck['home_status']==0) {?>
               <a href="system/operation.php?post_id=<?php echo $allPostCheck['post_id'] ?>&status=1&postHome=ok">
                 <button class="btn btn-danger btn-sm"><i class="fa fa-eye-slash"></i></button>
               </a>
             <?php } elseif ($allPostCheck['home_status']==1) {?>
               <a href="system/operation.php?post_id=<?php echo $allPostCheck['post_id'] ?>&status=0&postHome=ok">
                 <button class="btn btn-success  btn-sm"><i class="fa fa-eye"></i></button>
               </a>
             <?php } ?>
            </td>
            <td>
               <a class="btn btn-light" href="index.php?page=mediaUpload&gallery_type=post_gallery&gallery_id=<?php echo $allPostCheck['post_id'] ?>&gallery_title=<?php echo $allPostCheck['title'] ?>"><i class="fas fa-photo"></i> <?php echo $langArray['TABLE_GALLERY']; ?></a>
               <a class="btn btn-light" href="index.php?page=postEdit&post_id=<?php echo $allPostCheck['post_id'] ?>"><?php echo $langArray['TABLE_EDIT']; ?></a>
               <a class="btn btn-danger" data-toggle="modal" data-target="#dataDelete-<?php echo $allPostCheck['post_id']?>"><?php echo $langArray['TABLE_DELETE']; ?></a>
               <div class="modal fade" id="dataDelete-<?php echo $allPostCheck['post_id'] ?>">
                 <div class="modal-dialog customDialog">
                   <div class="modal-content bg-danger">
                     <div class="modal-header">
                       <h4 class="modal-title"><?php echo $langArray['ARE_YOU_SURE']; ?></h4>
                     </div>
                     <div class="modal-footer justify-content-between">
                       <button type="button" class="btn btn-outline-light" data-dismiss="modal"><?php echo $langArray['CANCEL_FORM']; ?></button>
                       <a href="system/operation.php?post_id=<?php echo $allPostCheck['post_id']; ?>&banner=<?php echo $allPostCheck['banner']; ?>&postDelete=success" class="btn btn-outline-light"><?php echo $langArray['TABLE_DELETE']; ?></a>
                     </div>
                   </div>
                 </div>
               </div>
            </td>
          </tr>
        <?php } ?>
      </tbody>
      <tfoot>
        <tr>
          <th><?php echo $langArray['TABLE_ORDER_SORT']; ?></th>
          <th><?php echo $langArray['TABLE_IMAGE']; ?></th>
          <th><?php echo $langArray['GLOBAL_FORM_TABLE_CATEGORIES']; ?></th>
          <th><?php echo $langArray['TABLE_DATE']; ?></th>
          <th><?php echo $langArray['GLOBAL_FORM_TABLE_TITLE']; ?></th>
          <th><?php echo $langArray['HOMEPAGE_STATUS']; ?></th>
          <th><?php echo $langArray['TABLE_OPERATION']; ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php }elseif($_GET['page'] == 'postAdd'){  ?>
<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"] ?>">
      <input type="hidden" name="dates" value="<?=$dates;?>">
      <input type="hidden" name="prefix" value="<?=$langArray['PREFIX_POST']?>">
      <div class="row">
        <div class="col-md-5">
          <div class="card card-secondary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="categories"><?php echo $langArray['GLOBAL_FORM_TABLE_CATEGORIES']; ?></label>
                <?php
                 $allCategories = array();
                 $allCatCheck=$db->query("SELECT * FROM categories where language={$getLangCheck['language_id']} and categories_type='post_categories'", PDO::FETCH_ASSOC);
                 if ( $allCatCheck->rowCount() ){
                   foreach( $allCatCheck as $line ){
                      $allCategories[$line['categories_id']]['categories_id'] = $line['categories_id'];
                      $allCategories[$line['categories_id']]['title'] = $line['title'];
                      $allCategories[$line['categories_id']]['categories_top_id'] = $line['categories_top_id'];
                   }
                 }
                 function findCategories($categories){
                    $row = array();
                    foreach($categories as $category){
                      if($category['categories_top_id'] == 0) $row[] = $category;
                    }
                    return $row;
                 }
                 function findSubCategories($topCat,$categories,$again = 2){
                    $row = array();
                    foreach($categories as $category){
                      if($category['categories_top_id'] == $topCat){
                        $row[] = $category;
                      }
                    }
                    if(count($row) > 0){
                      foreach($row as $cat){
                        echo '<option value="'.$cat['categories_id'].'">'.str_repeat('- ',$again).' '.$cat['title'].'</option>';
                        findSubCategories($cat['categories_id'],$categories,$again+1);
                      }
                    }
                 }
                 $mainCategories = findCategories($allCategories);
                 echo '
                   <select class="select2 form-control" name="categories[]" multiple="multiple">
                 ';
                  foreach($mainCategories as $category){
                    echo '
                     <option value="'.$category['categories_id'].'">'.str_repeat('- ',0).' '.$category['title'].'</option>
                    ';
                    findSubCategories($category['categories_id'],$allCategories);
                  }
                 echo '</select>';
                ?>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <div class="custom-file">
                    <input type="file" name="banner" class="custom-file-input">
                    <label class="custom-file-label"><?php echo $langArray['TABLE_CHOOSE_FILE']; ?></label>
                  </div>
                </div>
              </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="seo_url"><?php echo $langArray['FORM_SEO_URL']; ?></label>
                <input type="text" name="seo_url" class="form-control">
              </div>
              <div class="row">
                <div class="col-md-6">
                  <!-- #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-6">
                  <!-- #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 $postCount+1; ?>">
                  </div>
                </div>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_title"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_TITLE']; ?></label>
                <input type="text" name="seo_title" class="form-control">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_description"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_DESCRIPTION']; ?></label>
                <input type="text" name="seo_description" class="form-control">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_keywords"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_KEYWORDS']; ?></label>
                <input type="text" placeholder="<?php echo $langArray['WRITE_ENTER_PRESS']; ?>" name="seo_keywords" class="form-control"  data-role="tagsinput">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="pagesList" 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="postAdd">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-7">
          <div class="card card-primary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="content"><?php echo $langArray['GLOBAL_FORM_TABLE_CONTENT']; ?></label>
                <textarea class="textarea" name="content"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'postEdit'){  ?>
<?php
  $askData=$db->prepare("SELECT * FROM posts where post_id=:post_id");
  $askData->execute(array(
    'post_id' => $_GET["post_id"]
    ));
  $checkData=$askData->fetch(PDO::FETCH_ASSOC);

  // $_GET null or nothing
  if ($_GET['post_id'] == null) {
    Header("Location:postList");
  }elseif($_GET['post_id'] != $checkData['post_id']){
    Header("Location:postList");
  }
?>
<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="post_id" value="<?=$checkData["post_id"] ?>">
      <input type="hidden" name="isset_banner" value="<?=$checkData["banner"] ?>">
      <input type="hidden" name="prefix" value="<?=$langArray['PREFIX_POST']?>">
      <div class="row">
        <div class="col-md-5">
          <div class="card card-secondary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="categories"><?php echo $langArray['GLOBAL_FORM_TABLE_CATEGORIES']; ?></label>
                <select class="form-control select2" name="categories[]" multiple="multiple">
                  <?php
                    $allCategoriesAsk->execute(array(
                      'categories_type' => "post_categories",
                      'language'        => $getLangCheck['language_id']
                    ));
                    while ($allCategoriesCheck=$allCategoriesAsk->fetch(PDO::FETCH_ASSOC)){
                      $tags = explode(",",$checkData['categories']);
                  ?>
                    <option <?php if(in_array($allCategoriesCheck['categories_id'], $tags)){ echo ' selected=""'; } ?> value="<?php echo $allCategoriesCheck['categories_id']; ?>"><?php echo $allCategoriesCheck['title']; ?></option>
                  <?php } ?>
                </select>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <div class="custom-file">
                    <input type="file" name="banner" class="custom-file-input"  id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label"><?php echo $langArray['TABLE_CHOOSE_FILE']; ?></label>
                  </div>
                </div>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <img src="../<?php echo $checkData['banner']; ?>" alt="" width="100%">
                </div>
              </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>
              <div class="form-group">
                <label for="seo_url"><?php echo $langArray['FORM_SEO_URL']; ?></label>
                <?php $prefixDivide = explode($langArray['PREFIX_POST'],$checkData['seo_url']); ?>
                <div class="row">
                  <div class="col-md-3">
                    <input type="text" disabled class="form-control" value="<?php echo $langArray['PREFIX_POST']; ?>">
                  </div>
                  <div class="col-md-9">
                    <input type="text" name="seo_url" class="form-control" value="<?php echo $prefixDivide[1]; ?>">
                  </div>
                  <div class="clearfix"></div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-6">
                  <!-- #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-6">
                  <!-- #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>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_title"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_TITLE']; ?></label>
                <input type="text" name="seo_title" class="form-control" value="<?php echo $checkData['seo_title']; ?>">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_description"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_DESCRIPTION']; ?></label>
                <input type="text" name="seo_description" class="form-control" value="<?php echo $checkData['seo_description']; ?>">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_keywords"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_KEYWORDS']; ?></label>
                <input type="text" value="<?php echo $checkData['seo_keywords']; ?>" placeholder="<?php echo $langArray['WRITE_ENTER_PRESS']; ?>" name="seo_keywords" class="form-control"  data-role="tagsinput">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="postList" 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="postUpdate">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-7">
          <div class="card card-primary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="content"><?php echo $langArray['GLOBAL_FORM_TABLE_CONTENT']; ?></label>
                <textarea class="textarea" name="content"><?php echo $checkData['content']; ?></textarea>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'postCategoriesList'){  ?>
<div class="card">
  <div class="card-body">
    <?php
     $allCategories = array();
     $allCatCheck=$db->query("SELECT * FROM categories  where language={$getLangCheck['language_id']} and categories_type='post_categories' order by categories_id", PDO::FETCH_ASSOC);
     if ( $allCatCheck->rowCount() ){
       foreach( $allCatCheck as $line ){
          $allCategories[$line['categories_id']]['categories_id'] = $line['categories_id'];
          $allCategories[$line['categories_id']]['title'] = $line['title'];
          $allCategories[$line['categories_id']]['order_sort'] = $line['order_sort'];
          $allCategories[$line['categories_id']]['categories_top_id'] = $line['categories_top_id'];
          $allCategories[$line['categories_id']]['seo_url'] = $line['seo_url'];
          $allCategories[$line['categories_id']]['status'] = $line['status'];
          $allCategories[$line['categories_id']]['banner'] = $line['banner'];
       }
     }
     function findCategories($categories) {
        $row = array();

        foreach($categories as $category)
        {
            if($category['categories_top_id'] == 0) $row[] = $category;
        }
        return $row;
     }
     function findSubCategories($topCat,$categories,$again = 2) {
      $row = array();
      foreach($categories as $category) {
          if($category['categories_top_id'] == $topCat) {
              $row[] = $category;
          }
      }
      if(count($row) > 0) {
          foreach($row as $cat) {
              if ($cat['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;
              global $generalSettingCheck;
              $categoriesCountAsk2=$db->prepare("SELECT * FROM categories where categories_top_id=:categories_top_id");
              $categoriesCountAsk2->execute(array(
                'categories_top_id' => $cat['categories_id']
              ));
              $categoriesCount2=$categoriesCountAsk2->rowCount();
              echo '
              <tr>
                 <td> '.$cat['order_sort'].' </td> 
                 <td><img src="../'.$cat['banner'].'" width="100"></td>
                 <td><i class="fas fa-level-up-alt fa-rotate-90"></i><a target="_blank" href="'.$generalSettingCheck['site_url'] . $cat['seo_url'].'">'.str_repeat('- ',$again).' '.$cat['title'].'</a></td>
                 <td>' .$status.'</td>
                 <td>
                   <a class="btn btn-light" href="index.php?page=postCategoriesEdit&categories_id='.$cat['categories_id'].'">'.$langArray['TABLE_EDIT'].'</a>
                 ';
                 if ($categoriesCount2 > 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-'.$cat['categories_id'].'"> '.$langArray['TABLE_DELETE'].'</a>
                   <div class="modal fade" id="dataDelete-'.$cat['categories_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?categories_id='.$cat['categories_id'].'&banner='.$cat['banner'].'&postCategoriesDelete=success" class="btn btn-outline-light"> '.$langArray['TABLE_DELETE'].'</a>
                         </div>
                       </div>
                     </div>
                   </div>
                   ';
                 }
                 echo '
                 </td>
              </tr>
              ';
               findSubCategories($cat['categories_id'],$categories,$again+1);
          }
      }
     }

     // Ana kategorileri alıyoruz.
     $mainCategories = findCategories($allCategories);

     echo '
     <table id="dataTable" class="table table-bordered table-striped">
       <thead>
         <tr>
           <th>'. $langArray['TABLE_ORDER_SORT'] .'</th>
           <th>'. $langArray['TABLE_IMAGE'] .'</th>
           <th>'. $langArray['GLOBAL_FORM_TABLE_TITLE'] .'</th>
           <th>'. $langArray['TABLE_STATUS'] .'</th>
           <th>'. $langArray['TABLE_OPERATION'] .'</th>
         </tr>
       </thead>
       <tbody>
     ';
      // Ana kategorileri liste şeklinde alıyoruz.
      foreach($mainCategories as $category) {
        // Durum kontrol
         if ($category['status'] == 1) {
           $status ='<i class="fas fa-eye text-success"></i>';
         }else{
           $status='<i class="fas fa-eye-slash text-danger"></i>';
         }
         global $db;
         $categoriesCountAsk22=$db->prepare("SELECT * FROM categories where categories_top_id=:categories_top_id");
         $categoriesCountAsk22->execute(array(
           'categories_top_id' => $category['categories_id']
         ));
         $categoriesCount22=$categoriesCountAsk22->rowCount();
          // Ana kategorileri ekrana yazdırıyoruz.
          echo '
            <tr>
             <td>' .str_repeat('-',0).' '.$category['order_sort'].'</td>
             <td><img src="../'.$category['banner'].'" width="100"></td>
             <td><a target="_blank" href="'. $generalSettingCheck['site_url'] . $category['seo_url'].'">' .str_repeat('-',0).' '.$category['title'].'</a></td>
             <td>' .str_repeat('-',0).' '.$status.'</td>
             <td>
               <a class="btn btn-light" href="index.php?page=postCategoriesEdit&categories_id='.$category['categories_id'].'">'.$langArray['TABLE_EDIT'].'</a>
               ';
               if ($categoriesCount22 > 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-'.$category['categories_id'].'">'.$langArray['TABLE_DELETE'].'</a>
                 <div class="modal fade" id="dataDelete-'.$category['categories_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?categories_id='.$category['categories_id'].'&banner='.$category['banner'].'&postCategoriesDelete=success" class="btn btn-outline-light"> '.$langArray['TABLE_DELETE'].'</a>
                       </div>
                     </div>
                   </div>
                 </div>

                 ';
               }
               echo '

             </td>
            </tr>
          ';
          // Alt Kategorilerini dallandırarak yazdırıyoruz.
          findSubCategories($category['categories_id'],$allCategories);
      }
     echo '
         </tr>
       </tbody>
       <tfoot>
         <tr>
           <th>'. $langArray['TABLE_ORDER_SORT'] .'</th>
           <th>'. $langArray['TABLE_IMAGE'] .'</th>
           <th>'. $langArray['GLOBAL_FORM_TABLE_TITLE'] .'</th>
           <th>'. $langArray['TABLE_STATUS'] .'</th>
           <th>'. $langArray['TABLE_OPERATION'] .'</th>
         </tr>
       </tfoot>
     </table>
     ';
    ?>
  </div>
</div>
<?php }elseif($_GET['page'] == 'postCategoriesAdd'){  ?>
<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"] ?>">
      <input type="hidden" name="prefix" value="<?=$langArray['PREFIX_CATEGORY']?>">
      <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="categories_top_id"><?php echo $langArray['GLOBAL_FORM_CATEGORIES_TOP']; ?></label>
                <?php
                 $allCategories = array();
                 $allCatCheck=$db->query("SELECT * FROM categories where language={$getLangCheck['language_id']} and categories_type='post_categories'  order by categories_id", PDO::FETCH_ASSOC);
                 if ( $allCatCheck->rowCount() ){
                   foreach( $allCatCheck as $line ){
                      $allCategories[$line['categories_id']]['categories_id'] = $line['categories_id'];
                      $allCategories[$line['categories_id']]['title'] = $line['title'];
                      $allCategories[$line['categories_id']]['categories_top_id'] = $line['categories_top_id'];
                   }
                 }
                 function findCategories($categories){
                    $row = array();
                    foreach($categories as $category){
                      if($category['categories_top_id'] == 0) $row[] = $category;
                    }
                    return $row;
                 }
                 function findSubCategories($topCat,$categories,$again = 2){
                    $row = array();
                    foreach($categories as $category){
                      if($category['categories_top_id'] == $topCat){
                        $row[] = $category;
                      }
                    }
                    if(count($row) > 0){
                      foreach($row as $cat){
                        echo '<option value="'.$cat['categories_id'].'">'.str_repeat('- ',$again).' '.$cat['title'].'</option>';
                        findSubCategories($cat['categories_id'],$categories,$again+1);
                      }
                    }
                 }
                 $mainCategories = findCategories($allCategories);
                 echo '
                   <select class="select2" name="categories_top_id">
                     <option value="0">'.$langArray['GLOBAL_FORM_CATEGORIES_TOP'].'</option>
                 ';
                  foreach($mainCategories as $category){
                    echo '
                     <option value="'.$category['categories_id'].'">'.str_repeat('- ',0).' '.$category['title'].'</option>
                    ';
                    findSubCategories($category['categories_id'],$allCategories);
                  }
                 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 reference_close">
                <label for="seo_url"><?php echo $langArray['FORM_SEO_URL']; ?></label>
                <input type="text" name="seo_url" class="form-control">
              </div>
              <div class="row">
                <div class="col-md-6">
                  <!-- #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-6">
                  <!-- #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 $postCategoriesCount+1 ?>">
                  </div>
                </div>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_title"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_TITLE']; ?></label>
                <input type="text" name="seo_title" class="form-control">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_description"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_DESCRIPTION']; ?></label>
                <input type="text" name="seo_description" class="form-control">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_keywords"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_KEYWORDS']; ?></label>
                <input type="text" placeholder="<?php echo $langArray['WRITE_ENTER_PRESS']; ?>" name="seo_keywords" class="form-control"  data-role="tagsinput">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="postCategoriesList" 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="postCategoriesAdd">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-8">
          <div class="card card-primary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <div class="custom-file">
                    <input type="file" name="banner" class="custom-file-input"  id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label"><?php echo $langArray['TABLE_CHOOSE_FILE']; ?></label>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'postCategoriesEdit'){  ?>
<?php
  $askData=$db->prepare("SELECT * FROM categories where categories_id=:categories_id");
  $askData->execute(array(
    'categories_id' => $_GET["categories_id"]
    ));
  $checkData=$askData->fetch(PDO::FETCH_ASSOC);
  $foo = $checkData["categories_top_id"];
  // $_GET null or nothing
  if ($_GET['categories_id'] == null) {
    Header("Location:postCategoriesList");
  }elseif($_GET['categories_id'] != $checkData['categories_id']){
    Header("Location:postCategoriesList");
  }
?>
<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="categories_id" value="<?=$checkData["categories_id"] ?>">
      <input type="hidden" name="isset_banner" value="<?=$checkData["banner"] ?>">
      <input type="hidden" name="prefix" value="<?=$langArray['PREFIX_CATEGORY']?>">
      <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="categories_top_id"><?php echo $langArray['GLOBAL_FORM_CATEGORIES_TOP']; ?></label>
                 <?php
                  $allCategories = array();
                  $allCatCheck=$db->query("SELECT * FROM categories where language={$getLangCheck['language_id']} and categories_type='post_categories'", PDO::FETCH_ASSOC);
                  if ( $allCatCheck->rowCount() ){
                    foreach( $allCatCheck as $line ){
                       $allCategories[$line['categories_id']]['categories_id'] = $line['categories_id'];
                       $allCategories[$line['categories_id']]['title'] = $line['title'];
                       $allCategories[$line['categories_id']]['categories_top_id'] = $line['categories_top_id'];
                    }
                  }
                  function findCategories($categories){
                   $row = array();

                   foreach($categories as $category) {
                       if($category['categories_top_id'] == 0) $row[] = $category;
                   }
                   return $row;
                  }
                  function findSubCategories($topCat,$categories,$again = 2) {
                   $row = array();
                   foreach($categories as $category) {
                     if($category['categories_top_id'] == $topCat){
                       $row[] = $category;
                     }
                   }
                   if(count($row) > 0) {
                     foreach($row as $cat){
                       echo '<option ';
                       echo $cat['categories_id'] == $GLOBALS["foo"] ? ' selected="selected"' : '';
                       echo ' value="'.$cat['categories_id'].'">'.str_repeat('- ',$again).' '.$cat['title'].'</option>';
                       findSubCategories($cat['categories_id'],$categories,$again+1);
                     }
                   }
                  }
                  $mainCategories = findCategories($allCategories);
                  echo '
                    <select class="select2 form-control" name="categories_top_id">
                      <option '. ($checkData['categories_top_id'] == 0 ? ' selected="selected"' : '').' value="0">'.$langArray['GLOBAL_FORM_CATEGORIES_TOP'].'</option>
                  ';
                   foreach($mainCategories as $category){
                     echo '
                      <option '. ($category['categories_id']== $checkData['categories_top_id'] ? ' selected="selected"' : '').' value="'.$category['categories_id'].'">'.str_repeat('- ',0).' '.$category['title'].'</option>
                     ';
                     findSubCategories($category['categories_id'],$allCategories);
                   }
                  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="seo_url"><?php echo $langArray['FORM_SEO_URL']; ?></label>
                <?php $prefixDivide = explode($langArray['PREFIX_CATEGORY'],$checkData['seo_url']); ?>
                <div class="row">
                  <div class="col-md-3">
                    <input type="text" disabled class="form-control" value="<?php echo $langArray['PREFIX_CATEGORY']; ?>">
                  </div>
                  <div class="col-md-9">
                    <input type="text" name="seo_url" class="form-control" value="<?php echo $prefixDivide[1]; ?>">
                  </div>
                  <div class="clearfix"></div>
                </div>
              </div>
              <div class="row">
                <div class="col-md-6">
                  <!-- #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-6">
                  <!-- #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>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_title"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_TITLE']; ?></label>
                <input type="text" name="seo_title" class="form-control" value="<?php echo $checkData['seo_title'] ?>">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_description"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_DESCRIPTION']; ?></label>
                <input type="text" name="seo_description" class="form-control" value="<?php echo $checkData['seo_description'] ?>">
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="seo_keywords"><?php echo $langArray['GLOBAL_FORM_TABLE_SEO_KEYWORDS']; ?></label>
                <input type="text" placeholder="<?php echo $langArray['WRITE_ENTER_PRESS']; ?>" value="<?php echo $checkData['seo_keywords'] ?>" name="seo_keywords" class="form-control"  data-role="tagsinput">
              </div>
            </div>
          </div>
          <div class="col-12">
            <a href="postCategoriesList" 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="postCategoriesUpdate">
          </div>
          <div class="clearfix"></div>
        </div>
        <div class="col-md-8">
          <div class="card card-primary">
            <div class="card-body">
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <div class="custom-file">
                    <input type="file" name="banner" class="custom-file-input"  id="inputGroupFile01" aria-describedby="inputGroupFileAddon01">
                    <label class="custom-file-label"><?php echo $langArray['TABLE_CHOOSE_FILE']; ?></label>
                  </div>
                </div>
              </div>
              <!-- #form-group -->
              <div class="form-group">
                <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                <div class="input-group">
                  <img src="<?php echo $checkData['banner'] ?>" alt="" class="img-responsive">
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php } ?>
