<?php
  $classes="";
  if(isset($_POST['view_blogs_display_guider'])){
    $classes="../classes/main_classes.php";
  }else{
    $classes="../classes/main_classes.php";
  }
 
  require_once($classes);

  $data_tableObj=new DATA_TABLE_RETRIEVE();
  $rowcountObj=new ROWCOUNT();
?>
<div id="deleteUserResponse"></div>
<table class="table borderless table-striped table-responsive-lg">
  
  <?php
      $view_by_category_var="";
      $orderBy="b_id";
      $orderByType="DESC";
      $getCount=5;
      $getPage=0;
        if(isset($_POST['view_blogs_display_guider'])){
          //set order by
          if(!empty($_POST['orderBy'])){
            $orderBy=$_POST['orderBy'];
          }
          //set order by type
          if(!empty($_POST['orderByType'])){
            $orderByType=$_POST['orderByType'];
          }
          //set Page
          if(!empty($_POST['page'])){
            $getPage=$_POST['page'];
          }
          //set Count
          if(!empty($_POST['count'])){
            $getCount=$_POST['count'];
          }
        }

        if(isset($_POST["b_category_id"])){
          $category_id=addslashes($_POST["b_category_id"]);
          $view_by_category_var=" WHERE blog.b_category_id='$category_id' ";
        }
      //get data
      $tableVariable="blog $view_by_category_var ";
      $getData=$data_tableObj->data_table_display($tableVariable,$orderBy,$orderByType,$getCount,$getPage,"data");
      $getTotalCount=$data_tableObj->data_table_display($tableVariable,$orderBy,$orderByType,$getCount,$getPage,"count");
      if($getData==1020){
        echo "<tr>
          <td colspan='100%' class='text-center alert alert-warning'>No post found.</td>
        </tr>";
      }else{
          $elementId="";
          
          foreach ($getData as $key) {
            $elementId=$elementId."I";
            
        ?>
            <tr id="<?php echo $elementId;?>">
              <td>
                <div class="col-md-12 col-sm-12 col-xs-12">
                  <div class="single-blog">
                    <div class="single-blog-img">
                        <a href="<?php echo 'img/blogs/'.$key->b_image.'?timestamp'.time() ?>" class="fancybox" rel="ligthbox">
                          <img src="img/blogs/<?php echo $key->b_image ?>" alt="">
                        </a>
                    </div>
                    <div class="blog-meta">
                      <span class="comments-type">
                          <i class="fa fa-comment-o"></i>
                          <a href="#"><?php echo $rowcountObj->rowcounter("blog_comments","*","b_id",$key->b_id); ?> comment/s</a>
                        </span>
                      <span class="date-type">
                          <i class="fa fa-calendar"></i>
                          <?php
                            echo date("M j, Y", strtotime($key->b_date_posted)) ;
                            echo " / ";
                            echo date("H:i A", strtotime($key->b_date_posted)) ;
                          ?>
                        </span>
                    </div>
                    <div class="blog-text">
                      <h4>
                          <a href="#"><?php echo $key->b_title ?></a>
                        </h4>
                      <div class="view-6-lines">
                        <?php echo $key->b_body ?>
                      </div>
                    </div>
                    <span>
                        <a href="#!" onclick="blog_details_view(<?php echo $key->b_id; ?>)" class="ready-btn">Read more</a>
                      </span>
                  </div>
                </div>

              </td>
            </tr>
        <?php
            # code...
          }
        ?>
  </table>
  <p class="text-left text-info">
    <?php echo "Total number of posts : "."<span class='bold' id='currentUserCount'> ".$getTotalCount."</span>"; ?>
  </p>
  <nav aria-label="">
    <ul class="pagination justify-content-center">

      <?php
        //PREVIOUS
        $prevProperty="";
        $netxProperty="";
        $prevPage=0;
        if($getPage==0 or $getPage==1){
          $prevProperty="disabled";
        }else{
          $prevProperty="";
        }
        //Set Prev page
        if($getPage>1){
          $prevPage=$getPage-1;
        }
      ?>
        <li <?php echo "class='page-item $prevProperty'"?>>
          <a class="page-link" href="#!" tabindex="-1" onclick="view_blogs_special(<?php echo $prevPage; ?>)">Previous</a>
        </li>
      <?php
       
      ?>

      <?php
        //PAGE NUMBERS
        $loopCounter=0;  //get number of loops for pages
        
        $nextPage=0;
        $currentPage=0;
        for ($i=1; $i <= $getTotalCount; $i+=$getCount) { 
          $loopCounter=$loopCounter+1;

          $activeProperty="";

          if($loopCounter==$getPage or $getPage==0){
            $activeProperty="active";
            $currentPage=$loopCounter;
          }
          if($getPage==0){
            $activeProperty="active";
            $getPage=$getPage+1;
          }

          //next page
          if($loopCounter==$currentPage+1){
            if($getPage==0){
              $nextPage=1+$loopCounter;
            }else{
              $nextPage=$loopCounter;
            }
          }

          //limit pages
          if($loopCounter>=$currentPage-5 and $loopCounter<=$currentPage+5){
      ?>
            <li <?php echo "class='page-item $activeProperty'"?>><a class="page-link" href="#!" onclick="view_blogs_special(<?php echo $loopCounter; ?>)"><?php echo $loopCounter; ?></a></li>
      <?php
          }
        }
        $lastLoop=$loopCounter;
      ?>

      <?php
        //NEXT
        if($loopCounter>1 and $loopCounter!=$getPage){
          $nextProperty="";
        }else{
          $nextProperty="disabled";
        }
      ?>
        <li class="page-item <?php echo $nextProperty?>">
          <a class="page-link" href="#!" onclick="view_blogs_special(<?php echo $nextPage; ?>)">Next</a>
        </li>
    </ul>
  </nav>
</div>
<?php
  }
?>


<script type="text/javascript">
   $(document).ready(function(){
        //fancy box
        $(".fancybox").fancybox({
              openEffect: "none",
              closeEffect: "none"
          });
          
          $(".zoom").hover(function(){
          
          $(this).addClass('transition');
        }, function(){
              
          $(this).removeClass('transition');
        });
 }) 
</script>
