<?php 

   require_once 'Connect.php'; 
   $db = new Connect();
   $conn = $db->connect();
    $companyfk = 1;
$result =  $conn->prepare("select count(t.`ticketid`) as total FROM `ticket_sales_tb` t WHERE t.`companyfk`=? AND t.`reversed` = 0");
  $result->bind_param("s", $companyfk);
  $result->execute();
  $result = $result->get_result();
  $row = $result->fetch_assoc();

  $rows = $row["total"];
  $page_rows = 10;

  $last = ceil($rows / $page_rows);


  if($last < 1){
    $last = 1;
  }
 
  $pagenum = 1;

  if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
  }
 

  if ($pagenum < 1) { 
    $pagenum = 1; 
  } 

  else if ($pagenum > $last) { 
    $pagenum = $last; 
  }


  $limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
 
    $result =  $conn->prepare("SELECT t.`ticketid`, CONCAT(r.`origin`,' ' ,r.`destination`) routeName,
                                    s.name station , t.`posid`, t.`custname`, t.`custnumber`, t.`custnextofkin`,
                                    r.`fair`, CONCAT(u.`fname`,' ' ,u.`lname`) cashiername, t.`seatno`, t.`barcode`,t.`luggagefair`,t.`transactiondate`
                                    FROM `ticket_sales_tb` t JOIN station_tb s ON s.stationid = t.stationfk
                                    JOIN route_tb r ON r.routeid = t.routefk  JOIN user_tb u ON u.userid = t.cashierfk WHERE t.`companyfk`=? AND t.`reversed` = 0 ORDER BY t.`ticketid` DESC  $limit");

    $result->bind_param("s", $companyfk);
    $result->execute();
    $transactions["transactions"] = array();

    $result->bind_result($ticketid,$routeName, $station,$posid,$custname,$custnumber,$custnextofkin,$fair,$cashiername,$seatnumber,$barcode,$luggagefair,$transactiondate);
        while($row = $result->fetch()) 
        {
            $ticket = array();
            $ticket["ticketid"] = $ticketid;
            $ticket["routeName"] =  $routeName;  
            $ticket["station"] = $station;
            $ticket["posid"] = $posid;
            $ticket["custname"] = $custname; 
            $ticket["custnumber"] = $custnumber;  
            $ticket["custnextofkin"] = $custnextofkin;
            $ticket["fair"] = $fair; 
            $ticket["cashiername"] = $cashiername;
            $ticket["seatno"] = $seatnumber; 
            $ticket["barcode"] = $barcode;
            $ticket["luggagefair"] = $luggagefair;  
            $ticket["transactiondate"] = $transactiondate;  
            $transactions["transactions"][] =  $ticket;
     

        }


  $paginationCtrls = "";


	if($last != 1){
		
	if ($pagenum > 1) {
        $previous = $pagenum - 1;
		$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'type="button" class="btn btn-dark btn-outline m-b-10 m-l-5">Previous</a> &nbsp; &nbsp; ';
		
		for($i = $pagenum-4; $i < $pagenum; $i++){
			if($i > 0){
		        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'type="button" class="btn btn-dark btn-outline m-b-10 m-l-5">'.$i.'</a> &nbsp; ';
			}
	    }
    }
	$paginationCtrls .= ''.$pagenum.' &nbsp; ';
	
	for($i = $pagenum+1; $i <= $last; $i++){
		$paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'type="button" class="btn btn-dark btn-outline m-b-10 m-l-5">'.$i.'</a> &nbsp; ';
		if($i >= $pagenum+4){
			break;
		}
	}

    if ($pagenum != $last) {
        $next = $pagenum + 1;
        $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'type="button" class="btn btn-dark btn-outline m-b-10 m-l-5" >Next</a> ';
    }
	}



?>