<?php 
include'../database.php';
$obj = new Database();
?>
								<div class="table-responsive col-12 s-right mb-75">
									<table class="table table-striped">
										<thead>
											<tr>
												<th>Visitor Name</th>
												<th>Father's Name</th>
												<th>Address</th>
												<th>Phone No.</th>
												<th>Reason To Visit</th>
												<th>Date</th>
												<th>Time In</th>
												<th>Time Out</th>
												<th>Description</th>
												<th>Action</th>
											</tr>
										</thead>
										<tbody>

<?php
$cid=$_POST['cid'];
$val=$_POST['search'];

  $limit_per_page = 10;

  $page = "";
  if(isset($_POST["page_no"])){
    $page = $_POST["page_no"];
  }else{
    $page = 1;
  }

  $offset = ($page - 1) * $limit_per_page;


$obj->sql("SELECT * FROM visitors WHERE cid='$cid' AND (id LIKE '%$val%' || visitor LIKE '%$val%' || fname LIKE '%$val%' || village LIKE '%$val%' || phone LIKE '%$val%' ||  reason LIKE '%$val%' || date LIKE '%$val%' || timein LIKE '%$val%' || timeout LIKE '%$val%' || detail LIKE '%$val%') ORDER BY id DESC LIMIT {$offset},{$limit_per_page}");
$result = $obj->getResult();
	foreach ($result as list("id"=>$id,"visitor"=>$visitor,"fname"=>$fname,"village"=>$village,"phone"=>$phone,"reason"=>$reason,"date"=>$date,"timein"=>$timein,"timeout"=>$timeout,"detail"=>$detail)) { ?>
											<tr>
												<td><?php echo $visitor; ?></td>
												<td><?php echo $fname; ?></td>
												<td><?php echo $village; ?></td>
												<td><?php echo $phone; ?></td>
												<td><?php echo $reason; ?></td>
												<td><?php echo $date; ?></td>
												<td><?php echo $timein; ?></td>
												<td><?php echo $timeout; ?></td>
												<td><?php echo $detail; ?></td>
												<td>
<?php 
if($timeout==''){ ?>
<div class="modal-info d-inline-block">
<button type="button" title="Edit" data-toggle="modal" data-target="#edit<?php echo $id; ?>" class="btn btn-success mr-1 mb-1">Time-Out</button>
<?php } ?>
	<div class="modal fade text-left" id="edit<?php echo $id; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel110" style="display: none;" aria-hidden="true">
		<div class="modal-dialog modal-lg modal-dialog-centered modal-dialog-scrollable" role="document">
			<div class="modal-content">
				<div class="modal-header bg-info">
					<h5 class="modal-title white" id="myModalLabel110">Edit</h5>
					<button type="button" class="close" data-dismiss="modal" aria-label="Close">
						<i class="bx bx-x"></i>
					</button>
				</div>
				<div class="modal-body">									
										<form class="form" action="" method="POST">
										    <input type="hidden" name="id" value="<?php echo $id; ?>">
											<div class="form-body">
												<div class="row">
													<div class="col-md-4">
													    <labe>Time Out</labe>
														<div class="form-label-group position-relative has-icon-left">
															<input type="time" class="form-control" name="timeout" placeholder="Select Time Out" required>
															<div class="form-control-position">
																<i class='bx bx-history'></i>
															</div>
														</div>
													</div>
													<div class="col-md-12 d-flex justify-content-end">
														<button type="submit" class="btn btn-primary mr-1 mb-1" name="edit_visitor">Submit</button>
														<button type="reset" class="btn btn-light-secondary mr-1 mb-1">Reset</button>
													</div>
												</div>
											</div>
										</form>
</div>
				<div class="modal-footer">
					NOTE: <i>Fill Details Carefully.</i>
				</div>
			</div>
		</div>
	</div>
</div>	
										        </td>
											</tr>
<?php } ?>
</tbody>
									</table>
								</div>

<?php
$output="";
$obj->count_rows("SELECT * FROM visitors WHERE cid='$cid' AND (id LIKE '%$val%' || visitor LIKE '%$val%' || fname LIKE '%$val%' || village LIKE '%$val%' || phone LIKE '%$val%' ||  reason LIKE '%$val%' || date LIKE '%$val%' || timein LIKE '%$val%' || timeout LIKE '%$val%' || detail LIKE '%$val%') ORDER BY id DESC");
$total_record= $obj->getResult();
    $total_pages = ceil($total_record/$limit_per_page);
    $output .='<div class="row px-2">
    <div class="col-md-8">Total '.$total_record.' record found</div>
    <div class="col-md-4"><nav aria-label="Page navigation"><ul class="pagination justify-content-end" id="pagination">';
//----------------
if($page <= 5){
    $x="";
    if($total_pages>6){$x=6;}else{$x=$total_pages;}
    for($i=1; $i <= $x; $i++){
      if($i == $page){
        $class_name = "page-item active";
      }else{
        $class_name = "page-item";
      }

      $output .= "<li class='{$class_name}'><a id='{$i}' class='page-link'>{$i}</a></li>";
    }
          if($total_pages>6){$output .= "<li class='{$class_name}'><a id='{$total_pages}' class='page-link'>{$total_pages}</a></li>";}

}
if(($page >5) && ($page<=($total_pages-5))){
$x=$page-2;
$y=$page+2;

      $output .= "<li class='page-item'><a id='1' class='page-link'>1</a></li>";
    for($i=$x; $i <= $y; $i++){
      if($i == $page){
        $class_name = "page-item active";
      }else{
        $class_name = "page-item";
      }

      $output .= "<li class='{$class_name}'><a id='{$i}' class='page-link'>{$i}</a></li>";
    }
      $output .= "<li class='{$class_name}'><a id='{$total_pages}' class='page-link'>{$total_pages}</a></li>";
}
if($page > ($total_pages-5) && $page > 5){
    $x=$total_pages-5;
      $output .= "<li class='page-item'><a id='1' class='page-link'>1</a></li>";

    for($i=$x; $i <= $total_pages; $i++){
      if($i == $page){
        $class_name = "page-item active";
      }else{
        $class_name = "page-item";
      }

      $output .= "<li class='{$class_name}'><a id='{$i}' class='page-link'>{$i}</a></li>";
    }
}

//---------------
    $output .='</ul></nav></div></div>';

    echo $output;
?>