<?php if ($_GET['page'] == 'productList'){  ?>
<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['GLOBAL_FORM_TABLE_TITLE']; ?></th>
          <th><?php echo $langArray['TABLE_DATE']; ?></th>
          <th><?php echo $langArray['TABLE_STATUS']; ?></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_products',
            'language'  => $getLangCheck['language_id']
          ));
          while ($allPostCheck=$allPostAsk->fetch(PDO::FETCH_ASSOC)){
           // All project List
           $productDetailAsk=$db->prepare("SELECT * FROM products WHERE product_post_id=:product_post_id");
           $productDetailAsk->execute(array(
             'product_post_id' => $allPostCheck['post_id']
           ));
           $productDetailCheck=$productDetailAsk->fetch(PDO::FETCH_ASSOC);
         ?>
          <tr>
            <td><?php echo $allPostCheck['order_sort']; ?></td>
            <td><img src="../<?php echo $allPostCheck['banner']; ?>" width="60"></td>
            <td>
              <?php
                $allCategoriesIdAsk->execute(array(
                  'categories_id'   => $allPostCheck["categories"],
                  'categories_type' => "product_categories",
                  'language'        => $getLangCheck['language_id']
                ));
                while ($allCategoriesCheck=$allCategoriesIdAsk->fetch(PDO::FETCH_ASSOC)){
              ?>
                <a target="_blank" href="<?php echo $generalSettingCheck['site_url'] . $allCategoriesCheck['seo_url']; ?>"><?php echo $allCategoriesCheck['title']; ?></a>
              <?php } ?>
            </td>
            <td>
              <a target="_blank" href="<?php echo $generalSettingCheck['site_url'] . $allPostCheck['seo_url']; ?>"><?php echo $allPostCheck['title']; ?></a>
            </td>
            <td><?php echo addAgo($allPostCheck['dates']); ?></td>
            <td>
              <?php if ($allPostCheck['status']== 1){ ?>
                <i class="fas fa-eye text-success"></i>
              <?php }else{ ?>
                <i class="fas fa-eye-slash text-danger"></i>
              <?php } ?>
            </td>
            <td align="center">
              <?php if ($allPostCheck['home_status']==0) {?>
               <a href="system/operation.php?post_id=<?php echo $allPostCheck['post_id'] ?>&status=1&productHome=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&productHome=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=product_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=productEdit&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']; ?>&productDelete=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['GLOBAL_FORM_TABLE_TITLE']; ?></th>
          <th><?php echo $langArray['TABLE_DATE']; ?></th>
          <th><?php echo $langArray['TABLE_STATUS']; ?></th>
          <th><?php echo $langArray['HOMEPAGE_STATUS']; ?></th>
          <th><?php echo $langArray['TABLE_OPERATION']; ?></th>
        </tr>
      </tfoot>
    </table>
  </div>
</div>
<?php }elseif($_GET['page'] == 'productAdd'){  ?>
<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_PRODUCT']?>">
      <div class="row">
        <div class="col-md-12">
          <div class="card card-secondary">
            <div class="card-body row">
              <div class="col-md-6">
                <!-- #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 class="col-md-6">
                <!-- #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='product_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">
                   ';
                    foreach($mainCategories as $category){
                      echo '
                       <option value="'.$category['categories_id'].'">'.str_repeat('- ',0).' '.$category['title'].'</option>
                      ';
                      findSubCategories($category['categories_id'],$allCategories);
                    }
                   echo '</select>';
                  ?>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #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>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="vat"><?php echo $langArray['GLOBAL_FORM_TABLE_VAT']; ?></label>
                  <select class="form-control select2" name="vat">
                    <option value="0"><?php echo $langArray['GLOBAL_FORM_TABLE_VAT_NO']; ?></option>
                    <option value="1">%1</option>
                    <option value="8">%8</option>
                    <option value="18">%18</option>
                  </select>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="price"><?php echo $langArray['GLOBAL_FORM_TABLE_PRICE']; ?></label>
                  <div class="input-group">
                    <input type="text" name="price" placeholder="1500" class="form-control">
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="discount_price"><?php echo $langArray['GLOBAL_FORM_TABLE_DISCOUNT_PRICE']; ?></label>
                  <input type="number" name="discount_price" class="form-control">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="product_code"><?php echo $langArray['GLOBAL_FORM_TABLE_PRODUCT_CODE']; ?></label>
                  <div class="input-group">
                    <input type="text" name="product_code" class="form-control" value="<?php echo mb_substr($code, 0, 9); ?>">
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="stock_number"><?php echo $langArray['GLOBAL_FORM_TABLE_STOCK_NUMBER']; ?></label>
                  <input type="text" name="stock_number" class="form-control">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="currency"><?php echo $langArray['GLOBAL_FORM_TABLE_CURRENCY']; ?></label>
                  <input type="text" name="currency" class="form-control">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="order_sales"><?php echo $langArray['GLOBAL_FORM_TABLE_ORDER_SALES']; ?></label>
                  <select class="form-control select2" name="order_sales">
                    <option value="adress"><?php echo $langArray['GLOBAL_FORM_TABLE_ADRESS']; ?></option>
                    <option value="order"><?php echo $langArray['GLOBAL_FORM_TABLE_ORDER']; ?></option>
                  </select>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="other_link"><?php echo $langArray['GLOBAL_FORM_TABLE_OTHER_LINK']; ?></label>
                  <input type="text" name="other_link" class="form-control">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #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-2">
                <!-- #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 $productsCount+1; ?>">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="seo_url"><?php echo $langArray['GLOBAL_FORM_TABLE_SEOURL']; ?></label>
                  <input type="text" name="seo_url" class="form-control">
                </div>
              </div>
              <div class="col-md-6">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="video"><?php echo $langArray['PROJECT_TABLE_VIDEO']; ?></label>
                  <textarea name="video" class="form-control"></textarea>
                </div>
              </div>
              <div class="col-md-6">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="maps"><?php echo $langArray['PROJECT_TABLE_MAPS']; ?></label>
                  <textarea name="maps" class="form-control"></textarea>
                </div>
              </div>
              <div class="col-md-6">
                <!-- #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>
              </div>
              <div class="col-md-6">
                <!-- #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>
              </div>
              <div class="col-md-12">
                <!-- #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>
        </div>
        <div class="col-md-12">
          <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="col-12">
          <a href="productList" 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="productAdd">
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'productEdit'){  ?>
<?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);
  $foo = $checkData["categories"];

  $productDetailAsk=$db->prepare("SELECT * FROM products where product_post_id=:product_post_id");
  $productDetailAsk->execute(array(
    'product_post_id' => $_GET["post_id"]
    ));
  $productDetailCheck=$productDetailAsk->fetch(PDO::FETCH_ASSOC);


  // $_GET null or nothing
  if ($_GET['post_id'] == null) {
    Header("Location:productList");
  }elseif($_GET['post_id'] != $checkData['post_id']){
    Header("Location:productList");
  }
?>
<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_PRODUCT']?>">
      <div class="row">
        <div class="col-md-12">
          <div class="card card-secondary">
            <div class="card-body row">
              <div class="col-md-6">
                <!-- #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 class="col-md-6">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="image"><?php echo $langArray['TABLE_IMAGE']; ?></label>
                  <div class="input-group">
                    <a href="../<?php echo $checkData["banner"]; ?>" data-toggle="lightbox" data-gallery="gallery">
                      <?php echo $langArray['TABLE_IMAGE_LINK']; ?>
                    </a>
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #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='product_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">
                   ';
                    foreach($mainCategories as $category){
                      echo '
                       <option '. ($category['categories_id']== $checkData['categories'] ? ' selected="selected"' : '').' value="'.$category['categories_id'].'">'.str_repeat('- ',0).' '.$category['title'].'</option>
                      ';
                      findSubCategories($category['categories_id'],$allCategories);
                    }
                   echo '</select>';
                  ?>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #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>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="vat"><?php echo $langArray['GLOBAL_FORM_TABLE_VAT']; ?></label>
                  <select class="form-control select2" name="vat">
                    <option <?php echo $productDetailCheck['vat'] == "0" ? 'selected=""' : ''; ?> value="0"><?php echo $langArray['GLOBAL_FORM_TABLE_VAT_NO']; ?></option>
                    <option <?php echo $productDetailCheck['vat'] == "1" ? 'selected=""' : ''; ?> value="1">%1</option>
                    <option <?php echo $productDetailCheck['vat'] == "8" ? 'selected=""' : ''; ?> value="8">%8</option>
                    <option <?php echo $productDetailCheck['vat'] == "18" ? 'selected=""' : ''; ?> value="18">%18</option>
                  </select>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="price"><?php echo $langArray['GLOBAL_FORM_TABLE_PRICE']; ?></label>
                  <div class="input-group">
                    <input type="text" name="price" placeholder="1500" class="form-control" value="<?php echo $productDetailCheck["price"]; ?>">
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="discount_price"><?php echo $langArray['GLOBAL_FORM_TABLE_DISCOUNT_PRICE']; ?></label>
                  <input type="number" name="discount_price" class="form-control" value="<?php echo $productDetailCheck["discount_price"]; ?>">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="product_code"><?php echo $langArray['GLOBAL_FORM_TABLE_PRODUCT_CODE']; ?></label>
                  <div class="input-group">
                    <input type="text" name="product_code" class="form-control" value="<?php echo $productDetailCheck["product_code"]; ?>">
                  </div>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="stock_number"><?php echo $langArray['GLOBAL_FORM_TABLE_STOCK_NUMBER']; ?></label>
                  <input type="text" name="stock_number" class="form-control" value="<?php echo $productDetailCheck["stock_number"]; ?>">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="currency"><?php echo $langArray['GLOBAL_FORM_TABLE_CURRENCY']; ?></label>
                  <input type="text" name="currency" class="form-control" value="<?php echo $productDetailCheck["currency"]; ?>">
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="order_sales"><?php echo $langArray['GLOBAL_FORM_TABLE_ORDER_SALES']; ?></label>
                  <select class="form-control select2" name="order_sales">
                    <option <?php echo $productDetailCheck['order_sales'] == "adress" ? 'selected=""' : ''; ?> value="adress"><?php echo $langArray['GLOBAL_FORM_TABLE_ADRESS']; ?></option>
                    <option <?php echo $productDetailCheck['order_sales'] == "order" ? 'selected=""' : ''; ?>  value="order"><?php echo $langArray['GLOBAL_FORM_TABLE_ORDER']; ?></option>
                  </select>
                </div>
              </div>
              <div class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="other_link"><?php echo $langArray['GLOBAL_FORM_TABLE_OTHER_LINK']; ?></label>
                  <input type="text" name="other_link" class="form-control" value="<?php echo $productDetailCheck["other_link"]; ?>">
                </div>
              </div>
              <div class="col-md-1">
                <!-- #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-1">
                <!-- #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 class="col-md-2">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="seo_url"><?php echo $langArray['FORM_SEO_URL']; ?></label>
                  <?php $prefixDivide = explode($langArray['PREFIX_PRODUCT'],$checkData['seo_url']); ?>
                  <div class="row">
                    <div class="col-md-6">
                      <input type="text" disabled class="form-control" value="<?php echo $langArray['PREFIX_PRODUCT']; ?>">
                    </div>
                    <div class="col-md-6">
                      <input type="text" name="seo_url" class="form-control" value="<?php echo $prefixDivide[1]; ?>">
                    </div>
                    <div class="clearfix"></div>
                  </div>
                </div>
              </div>
              <div class="col-md-6">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="video"><?php echo $langArray['PROJECT_TABLE_VIDEO']; ?></label>
                  <textarea name="video" class="form-control"><?php echo $productDetailCheck["video"]; ?></textarea>
                </div>
              </div>
              <div class="col-md-6">
                <!-- #form-group -->
                <div class="form-group">
                  <label for="maps"><?php echo $langArray['PROJECT_TABLE_MAPS']; ?></label>
                  <textarea name="maps" class="form-control"><?php echo $productDetailCheck["maps"]; ?></textarea>
                </div>
              </div>
              <div class="col-md-6">
                <!-- #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>
              </div>
              <div class="col-md-6">
                <!-- #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>
              </div>
              <div class="col-md-12">
                <!-- #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>
        </div>
        <div class="col-md-12">
          <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="col-12">
          <a href="productList" 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="productUpdate">
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php }elseif($_GET['page'] == 'productCategoriesList'){  ?>
<div class="card">
  <div class="card-body">
    <?php
    // Dizi oluşturuyoruz.
     $allCategories = array();
     $allCatCheck=$db->query("SELECT * FROM categories  where language={$getLangCheck['language_id']} and categories_type='product_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']]['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'];
       }
     }
     // Ana kategorileri almak için bir fonksiyon yazıyoruz.
     function findCategories($categories) {
        $row = array();

        foreach($categories as $category)
        {
            if($category['categories_top_id'] == 0) $row[] = $category;
        }
        return $row;
     }

     // Herhangi bir kategorinin alt kategorilerini bulmak için bir fonksiyon yazıyoruz.
     function findSubCategories($topCat,$categories,$again = 2) {
      // Bir dizi oluşturuyoruz.
      $row = array();

      // Tüm kategorileri döngüye sokuyoruz.
      foreach($categories as $category) {
          // Belirtilen kategorilerin alt kategorilerini alıyoruz ve diziye ekliyoruz.
          if($category['categories_top_id'] == $topCat) {
              $row[] = $category;
          }
      }

      // Dizide eleman var mı onu kontrol ediyoruz.
      if(count($row) > 0) {
          // Dizideki alt kategorileri döngüye sokuyoruz.
          foreach($row as $cat) {
             // Durum kontrol
              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();
              // Alt kategorileri yazdırıyoruz.
              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=productCategoriesEdit&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'].'&productCategoriesDelete=success" class="btn btn-outline-light">'.$langArray['TABLE_DELETE'].'</a>
                           </div>
                         </div>
                       </div>
                     </div>
                   ';
                 }
                 echo '
                 </td>
              </tr>
              ';
              // Yazdırılan kategorinin alt kategorilerini bulduruyoruz. Yani döngü içinde döngü oluşturarak verilerin dallanmasını sağlıyoruz.
              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=productCategoriesEdit&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'].'&productCategoriesDelete=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'] == 'productCategoriesAdd'){  ?>
<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='product_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_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 $productCategoriesCount+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="productCategoriesList" 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="productCategoriesAdd">
          </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'] == 'productCategoriesEdit'){  ?>
<?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:categoriesList");
  }elseif($_GET['categories_id'] != $checkData['categories_id']){
    Header("Location:categoriesList");
  }
?>
<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='product_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="productCategoriesList" 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="productCategoriesUpdate">
          </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="" width="100%">
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="clearfix"></div>
      </div>
    </form>
  </div>
</div>
<?php } ?>
