<?php
session_start();
if(!isset($_SESSION['user'])) {
	header("location: ../index.php");
	exit;
}

if(isset($_POST['page'])){
    //Include pagination class file
    include('pagination.php');
    
    //Include database configuration file
    include('../dbconnect.php');
    
    $start = !empty($_POST['page'])?$_POST['page']:0;
    $limit = 10;
    
    //set conditions for search
    $whereSQL = '';
    $keywords = $_POST['keywords'];
    if(!empty($keywords)){
        $whereSQL = " AND goh.`GuestPhone` LIKE '%$keywords%' OR goh.`FirstName` LIKE '%$keywords%' OR goh.`LastName` LIKE '%$keywords%'";
        // $whereSQL1 = " AND gd.`GuestPhone` LIKE '%$keywords%' OR gd.`FirstName` LIKE '%$keywords%' OR gd.`LastName` LIKE '%$keywords%'";
    }

    //get number of rows
    $queryNum = mysql_query("SELECT DISTINCT(goh.`GuestPhone`) as postNum FROM `guest_order_history` AS goh WHERE (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled')".$whereSQL);
    // $resultNum = $queryNum->fetch_assoc();
    $rowCount = mysql_num_rows($queryNum);

    //initialize pagination class
    $pagConfig = array(
        'currentPage' => $start,
        'totalRows' => $rowCount,
        'perPage' => $limit,
        'link_func' => 'searchFilter'
    );
    $pagination =  new Pagination($pagConfig);
    
    //get rows
    $query = mysql_query("SELECT goh.`GuestId`, goh.`GuestPhone`, goh.`FirstName`, goh.`LastName`, MAX(goh.`OrderDate`) AS OrderDate, goh.`OrderTime`, count(goh.`OrderDate`) AS ord, goh.`PointsRedeemed` FROM `guest_order_history` AS goh WHERE (goh.`OrderStatus` != 'Rejected' AND goh.`OrderStatus` != 'Logged' AND goh.`OrderStatus` != 'Manual' AND goh.`OrderStatus` != 'Cancelled') $whereSQL GROUP BY goh.`GuestPhone` LIMIT $start,$limit");
    ?>
    <table class="table table-hover customers-table">
        <thead>
           	<tr>
              	<th class="fw400">Image</th>
              	<th class="fw400">Name</th>
              	<th class="fw400">Phone No</th>
              	<th class="fw400">Last Visited</th>
              	<th class="fw400">Visits</th>
              	<!-- <th class="fw400">Points</th> -->
           	</tr>
        </thead>
        <tbody class="js-users_body">
		    <?php    
		    while($row = mysql_fetch_array($query)){ 
		        ?>
		    	<tr>
					<td>
						<div class="media">
							<img src="../assets/img/avatars/user3.png" alt="Image" class="img-responsive img-circle">
						</div>
					</td>
					<td style="overflow-wrap: break-word;max-width: 157px;">
						<a href="customers_information.php?p=<?php echo $row['GuestPhone']; ?>" class="js-name">
							<?php echo $row['FirstName'].' '.$row['LastName']; ?>
						</a>
					</td>
					<td class="hidden-xs hidden-sm">
						<?php echo $row['GuestPhone']; ?>
					</td>
					<td class="hidden-xs hidden-sm"><?php echo date('d-m-Y', strtotime($row['OrderDate'])).', '.$row['OrderTime']; ?></td>
					<td><?php echo $row['ord']; ?></td>
					<!-- <td><?php //echo $row['PointsRedeemed']; ?></td> -->
				</tr>
				<?php
			}
			?>
		</tbody>
	</table>
	<?php
	echo $pagination->createLinks();
} ?>