<?php
//Created on 10-02-2023
//session_start();
include_once '../config/database.php';
include_once '../security/session_config.php';
include_once "../resources/offering.php";
include_once '../config/properties.php';
    
        $offering = new Offering();
        $offering->user_id = $_GET['user_id'];
        $offering->date = $date;
        
        $page = $_GET['page'];
        $user_id = $_GET['user_id'];
        $page_url = $_GET['page_url'];  
        
        $value= explode(".", $page_url);
        if($value[0] == "home"){
            $records_per_page = 8; // set records or rows of data per page
            $from_record_num = ($records_per_page * $page) - $records_per_page; // calculate for the query LIMIT clause

            $total_rows = $offering->getCourses_Home_count();
            if($total_rows == '0'){
                 $total_rows = $offering->completedcourses_count();
            } 
            $pass_value="";
        }else{
            $view = $_GET['view'];
            $mode = $_GET['mode'];
            $records_per_page = 10; // set records or rows of data per page
            $from_record_num = ($records_per_page * $page) - $records_per_page; // calculate for the query LIMIT clause

            if($mode == "active"){
                $total_rows = $offering->activecourses_count();
            }else if($mode == "complete"){
                $total_rows = $offering->completedcourses_count();
            }else if($mode == "incomplete"){
                $total_rows = $offering->incompletecourses_count();            
            }
            $pass_value="&mode=".$mode."&view=".$view;
        }
  
                   
        if($total_rows != "0"){
                    $limit = 0;
                    $total_pages = 0;
                    $total_pages = ceil($total_rows / $records_per_page);
                    $dotshow = true;
                    if ($page == 2 || $page == $total_pages - 1) {
                        $limit = 2;
                    } else if ($page >= 3 && $page != $total_pages) {
                        $limit = 1;
                    } else {
                        $limit = 3;
                    }
                    //previous btn
                    $previous_page = $page-1;
                    $previous = $page > 1 ? "{$page_url}page={$previous_page}{$pass_value}": "javascript:;" ; 
                    //next btn
                    $next_page = $page+1;
                    $next = $page < $total_pages ? "{$page_url}page={$next_page}{$pass_value}": "javascript:;" ; ?>
                        <div class="col-auto">
                            <div class="pagination -buttons">
                                <a href="<?php echo $previous ?>">
                                    <button class="pagination__button -prev" <?php if ($page <= 1) { ?> disabled="" <?php } ?>>
                                        <i class="fa fa-chevron-left"></i>
                                    </button>
                                </a>
                                <div class="pagination__count">
                                    <?php
                                    for ($i = 1; $i <= $total_pages; $i++) {
                                        if ($i == 1 || $i == $total_pages || ($i >= $page - $limit && $i <= $page + $limit)) {
                                            $dotshow = true;
                                            if ($i != $page) {
                                                echo "<a href='{$page_url}page=$i{$pass_value}'>$i</a>";
                                            } else {
                                                echo "<a class='-count-is-active' href='javascript:;'>$i</a>";
                                            }
                                        } else if ($dotshow) {
                                            $dotshow = false;
                                            echo "<span>...</span>";
                                        }
                                    }
                                    ?>
                                </div>
                                 <a href="<?php  echo $next ?>">
                                    <button class="pagination__button -next"  <?php if ($page >= $total_pages) { ?>disabled<?php } ?>>
                                       <i class="fa fa-chevron-right"></i>
                                    </button>
                                </a>
                            </div>
                        </div>
          
      <?php   } ?>           