<?php
include('session.php');

$count=1;
$colname_content_cat = $_GET['content_cat'];
$colname_content_sub = $_GET['content_sub'];

if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
	$page_no = 1;
	}

$total_records_per_page = 20;
$offset = ($page_no-1) * $total_records_per_page;
$previous_page = $page_no - 1;
$next_page = $page_no + 1;
$adjacents = "2"; 

$result_count = mysqli_query($leld,"SELECT COUNT(*) As total_records FROM le_menu_content WHERE content_cat = '".$colname_content_cat."' and content_sub = '".$colname_content_sub."'");
$total_records = mysqli_fetch_array($result_count);
$total_records = $total_records['total_records'];
$total_no_of_pages = ceil($total_records / $total_records_per_page);
$second_last = $total_no_of_pages - 1; // total page minus 1

$result_prikaz = mysqli_query($leld,"SELECT * FROM le_menu_content WHERE content_cat = '".$colname_content_cat."' and content_sub = '".$colname_content_sub."' LIMIT $offset, $total_records_per_page");

?>
<?php if ($total_records > '0') { ?>
											<h5 class="card-title"><?php echo $total_records; ?></h5>
<?php while($row_prikaz = mysqli_fetch_assoc($result_prikaz)) { ?>
										
											<div class="card">
												<div class="card-header">
													 
												</div>
												<div class="card-body">
													<div class="circle">
<?php if ($row_prikaz['content_active'] == 'y') { ?>
													<span class="sub_n_category bg-green"><?php echo $row_prikaz['content_price']; ?>&nbsp;€</span>
<?php } else { ?>
													<span class="sub_n_category bg-red"><?php echo $row_prikaz['content_price']; ?>&nbsp;€</span>
<?php } ?>
														
													</div>
													<h6 class="mt-3 mb-0">&nbsp;</h6>
													<span><?php echo $row_prikaz['content']; ?></span>
													<div class="align-right">
<?php
$me = $row_prikaz['content_id'];
$query_events_edit = "SELECT * FROM le_menu_content WHERE content_id = '".$me."'";
$events_edit = mysqli_query($leld,$query_events_edit);
$row_events_edit = mysqli_fetch_assoc($events_edit);
$totalRows_events_edit = mysqli_num_rows($events_edit);
?>
													<button type="button" class="btn btn-success btn-circle btn-sm edit_button" data-toggle="modal" data-target=".edit<?php echo $row_events_edit['content_id']; ?>"><i class="fa fa-pencil" data-placement="top" data-toggle="tooltip" title="Izmeniti"></i></button>
													<button type="button" class="btn btn-danger btn-circle btn-sm delete_button" data-toggle="modal" data-target="#delete<?php echo $row_events_edit['content_id']; ?>"><i class="fa fa-trash-o" data-placement="top" data-toggle="tooltip" title="Izbrisati"></i></button>
													</div>
													<div class="modal fade edit<?php echo $row_events_edit['content_id']; ?>" role="dialog" data-backdrop="static" aria-labelledby="myLargeModalLabel" aria-hidden="true">
														<div class="modal-dialog modal-lg">
															<div class="modal-content">
																<div class="modal-header">                                            
																	<h4 class="modal-title"><i class="fa fa-edit"></i>&nbsp;</h4>
																	<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
																</div>
																<form action="menu_content_edit.php" method="post" name="form<?php echo $row_events_edit['content_id']; ?>" id="form<?php echo $row_events_edit['content_id']; ?>">
																<div class="modal-body">
																	<div class="form-group c_form_group">
																		<label>Sadržaj menija</label>
																		<div class="input-group">
																			<input type="text" name="content" value="<?php echo $row_events_edit['content']; ?>" class="form-control" required data-parsley-maxlength="255">
																		</div>
																	</div>
																	<div class="form-group c_form_group">
																		<label>Cena</label>
																		<div class="input-group">
																			<input type="numeric" name="content_price" value="<?php echo $row_events_edit['content_price']; ?>" class="form-control" data-parsley-maxlength="11">
																		</div>
																	</div>
																	<div class="form-group c_form_group">
																		<label>Status</label>
																		<select name="content_active" class="custom-select" id="inputGroupSelect01">
																			<option value="n"<?php if (!(strcmp('n', htmlentities($row_events_edit['content_active'])))) {echo "selected=\"selected\"";} ?>>Nije aktivna</option>
																			<option value="y"<?php if (!(strcmp('y', htmlentities($row_events_edit['content_active'])))) {echo "selected=\"selected\"";} ?>>Aktivna</option>
																		</select>
																	</div>
																</div>
																<div class="modal-footer">
																	<button type="submit" class="btn btn-success">Sačuvaj</button>
																	<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
																</div>
																<input name="content_id" type="hidden" value="<?php echo $row_events_edit['content_id'];?>" />
																<input name="content_cat" type="hidden" value="<?php echo $row_events_edit['content_cat'];?>" />
																<input name="content_sub" type="hidden" value="<?php echo $row_events_edit['content_sub'];?>" />
																<input type="hidden" name="edit" value="1" />
																</form>
															</div>
														</div>
													</div>
													<div class="modal fade" id="delete<?php echo $row_events_edit['content_id']; ?>" data-backdrop="static" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
														<div class="modal-dialog modal-dialog-centered" role="document">
															<div class="modal-content">
																<div class="modal-header">
																	<h5 class="modal-title" id="exampleModalCenterTitle">Upozorenje!</h5>
																	<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
																</div>
																<div class="modal-body">
																	<p>Da li ste sigurni da želite da obrišete podatke?</p>
																</div>
																<div class="modal-footer">
																	<a href="menu_content_delete.php?content_id=<?php echo $row_events_edit['content_id']; ?>" class="btn btn-primary theme-bg gradient">Da</a>
																	<button type="button" class="btn btn-default" data-dismiss="modal">Ne</button>
																</div>
															</div>
														</div>
													</div>
												</div>
												
											</div>
										
<?php } ?>
<?php } else { ?>
					<div class="col-12">
						<div class="alert alert-danger alert-dismissible" role="alert">
							<i class="fa fa-times-circle"></i> Sadržaj menija nije dodat!
						</div>
					</div>
<?php } ?>
<nav>									
<ul class="pagination pagination-split">
<?php if($page_no > 1){ echo "<li class='page-item'><a href='?page_no=1' class='page-link'>&laquo;&laquo;</a></li>"; } ?>

<li <?php if($page_no <= 1){ echo "class='disabled'"; } ?>>
<?php if($page_no > 1){ echo "<a href='?page_no=$previous_page' class='page-link'>&laquo;</a>"; } ?>
</li>
   
<?php 
if ($total_no_of_pages <= 10){  	 
	for ($counter = 1; $counter <= $total_no_of_pages; $counter++){
		if ($counter == $page_no) {
	   echo "<li class='page-item active'><span class='page-link'>$counter</span></li>";	
			}else{
	   echo "<li class='page-item'><a href='?page_no=$counter' class='page-link'>$counter</a></li>";
			}
	}
}
elseif($total_no_of_pages > 10){
	
if($page_no <= 4) {			
 for ($counter = 1; $counter < 8; $counter++){		 
		if ($counter == $page_no) {
	   echo "<li class='page-item active'><span class='page-link'>$counter</span></li>";	
			}else{
	   echo "<li class='page-item'><a href='?page_no=$counter' class='page-link'>$counter</a></li>";
			}
	}
	echo "<li class='page-item'><span class='page-link'>...</span></li>";
	echo "<li class='page-item'><a href='?page_no=$second_last' class='page-link'>$second_last</a></li>";
	echo "<li class='page-item'><a href='?page_no=$total_no_of_pages' class='page-link'>$total_no_of_pages</a></li>";
	}

 elseif($page_no > 4 && $page_no < $total_no_of_pages - 4) {		 
	echo "<li class='page-item'><a href='?page_no=1' class='page-link'>1</a></li>";
	echo "<li class='page-item'><a href='?page_no=2' class='page-link'>2</a></li>";
	echo "<li class='page-item'><span class='page-link'>...</span></li>";
	for ($counter = $page_no - $adjacents; $counter <= $page_no + $adjacents; $counter++) {			
	   if ($counter == $page_no) {
	   echo "<li class='page-item active'><span class='page-link'>$counter</span></li>";	
			}else{
	   echo "<li class='page-item'><a href='?page_no=$counter' class='page-link'>$counter</a></li>";
			}                  
   }
   echo "<li class='page-item'><span class='page-link'>...</span></li>";
   echo "<li class='page-item'><a href='?page_no=$second_last' class='page-link'>$second_last</a></li>";
   echo "<li class='page-item'><a href='?page_no=$total_no_of_pages' class='page-link'>$total_no_of_pages</a></li>";      
		}
	
	else {
	echo "<li class='page-item'><a href='?page_no=1' class='page-link'>1</a></li>";
	echo "<li class='page-item'><a href='?page_no=2' class='page-link'>2</a></li>";
	echo "<li class='page-item'><span class='page-link'>...</span></li>";

	for ($counter = $total_no_of_pages - 6; $counter <= $total_no_of_pages; $counter++) {
	  if ($counter == $page_no) {
	   echo "<li class='page-item active'><span class='page-link'>$counter</span></li>";	
			}else{
	   echo "<li class='page-item'><a href='?page_no=$counter' class='page-link'>$counter</a></li>";
			}                   
			}
		}
}
?>

<li <?php if($page_no >= $total_no_of_pages){ echo "class='disabled'"; } ?>>
<?php if($page_no < $total_no_of_pages) { echo "<a href='?page_no=$next_page' class='page-link'>&raquo;</a>"; } ?>
</li>
<?php if($page_no < $total_no_of_pages){
	echo "<li class='page-item'><a href='?page_no=$total_no_of_pages' class='page-link'>&raquo;&raquo;</a></li>";
	} ?>
</ul>
</nav>