<?php
include('session.php');
?>
<!doctype html>
<html lang="en">

<head>
<title>LaktusDev | Gallery</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="description" content="">
<meta name="author" content="LaktusDev, design by: laktusdev.com">

<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- VENDOR CSS -->
<link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="assets/vendor/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="assets/vendor/animate-css/vivify.min.css">
<link rel="stylesheet" href="assets/vendor/light-gallery/css/lightgallery.css">
<link rel="stylesheet" href="assets/vendor/dropify/css/dropify.min.css">

<!-- MAIN CSS -->
<link rel="stylesheet" href="assets/css/mooli.min.css">

</head>
<body>
    
<div id="body" class="theme-green">

<?php require 'includes/page-loader.php'; ?>

<?php require 'includes/themesetting.php'; ?>

    <!-- Overlay For Sidebars -->
    <div class="overlay"></div>

    <div id="wrapper">

<?php require 'includes/topbar.php'; ?>
<?php require 'includes/leftbar.php'; ?>  
        <!-- Main body part  -->
        <div id="main-content">

            <div class="container-fluid">
                <!-- Page header section  -->
                <div class="block-header">
                    <div class="row clearfix">
                        <div class="col-lg-4 col-md-12 col-sm-12">
                            <h1>Hi,  <?php echo $row_korisnik['ime']; ?>!</h1>
                            <span>Gallery</span>
                        </div>
                        <div class="col-lg-8 col-md-12 col-sm-12 text-lg-right">
                            <div class="d-flex align-items-center justify-content-lg-end mt-4 mt-lg-0 flex-wrap vivify pullUp delay-550">
                                <div class="border-right pr-4 mr-4 mb-2 mb-xl-0">
                                    <p class="text-muted mb-1"></p>
                                    <h5 class="mb-0"></h5>
                                </div>
                                <div class="border-right pr-4 mr-4 mb-2 mb-xl-0">
                                    <p class="text-muted mb-1"></p>
                                    <h5 class="mb-0"></h5>
                                </div>
                                <div class="mb-3 mb-xl-0">
                                    <p class="text-muted mb-1"><?php echo date('l'); ?></p>
                                    <h5 class="mb-0"><?php echo date('d').'. '.date('m').'. '.date('Y'); ?></h5>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
<?php
$gallery_images_dir = "../../uploads/gallery/";
?>
                <div class="row">
                    <div class="col-12">
                        <nav class="navbar navbar-expand-lg navbar-light bg-light page_menu">
                            <a class="navbar-brand" href="gallery.php">Gallery</a>
                                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
                                <i class="fa fa-bars text-muted"></i>
                            </button>
                            <div class="collapse navbar-collapse" id="navbarNavDropdown">
                                
                                <div class="ml-auto">
                                    <button type="button" class="btn btn-success" data-toggle="modal" data-target=".photo_add"><i class="fa fa-plus-square"></i> Add gallery</button>
                                    
                                </div>
                            </div>
                        </nav>
                    </div>
<?php
if (isset($_GET['page_no']) && $_GET['page_no']!="") {
$page_no = $_GET['page_no'];
} else {
	$page_no = 1;
	}

$cid = stripslashes($_GET['legcid']);
$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_gallery_photos` where `legcid` = $cid");
$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_gallery = mysqli_query($leld,"SELECT * FROM le_gallery_photos where legcid = $cid LIMIT $offset, $total_records_per_page");

?>
								<div class="card">
									<div class="card-header">
                                         <span class="badge badge-success"><?php echo $total_records ?></span>
                                    </div>
									<div class="card-body">
										<div class="row clearfix">
										<div class="col-lg-3 col-md-6">
										<div class="card n_category">
											<div class="modal fade photo_add" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
												<div class="modal-dialog" role="document">
													<div class="modal-content">
														<div class="modal-header">
															<h5 class="modal-title" id="exampleModalLabel"><i class="fa fa-picture-o"></i> </h5>
															<button type="button" class="close" data-dismiss="modal" aria-label="Close">
																<span aria-hidden="true">&times;</span>
															</button>
														</div>
														<div class="modal-body">
<?php

// initialization
$photo_upload_fields = "";
$counter = 1;

// default number of fields
$number_of_fields = 3; 



// Lets build the Photo Uploading fields
while( $counter <= $number_of_fields )
{
$photo_upload_fields .=<<<__HTML_END
<div class="form-group c_form_group">
<label for="opis">Naslov {$counter}<span class="text-danger">*</span>:</label>
<input type="text" class="form-control" maxlength="150" name="opis[]" id="placement" />
</div>
<div class="form-group">
<label for="slika">Slika {$counter}<span class="text-danger">*</span>:</label>
<input type="file" name='slika[]' class="dropify">
</div>

__HTML_END;
$counter++;
}

// Final Output
echo <<<__HTML_END

<form enctype='multipart/form-data' action='upload_gallery_photo.php?category_id=$cid' method='post' name='upload_form' class='needs-validation' novalidate>

<!-Insert the photo fields here -->
$photo_upload_fields

<div class="clearfix text-right mt-3">
	<button type="submit" class="btn btn-success"> <i class="fa fa-send-o mr-1"></i> Submit</button>
</div>

</form>
 

__HTML_END;
?>
										
														</div>
														<div class="modal-footer">
															
														</div>
													</div>
												</div>
											</div>
											
										</div>
</div>
										
									</div>
								</div>
                    <div class="col-12">
                        <div class="card">
                            <div class="body">
                                <div id="lightgallery" class="row clearfix lightGallery">
<?php while($row_gallery = mysqli_fetch_assoc($result_gallery)) { ?>
                                    <div class="col-lg-3 col-md-6 m-b-15">
									<a class="light-link" href="<?php echo $gallery_images_dir; ?><?php echo $row_gallery['slika']; ?>">
									<img class="img-fluid" src="<?php echo $gallery_images_dir; ?><?php echo $row_gallery['slika']; ?>" alt="">
									</a>
									<a href="gallery_photo_edit.php?photo_id=<?php echo $row_gallery['photo_id']; ?>" class="btn btn-blue btn-circle btn-sm edit_button"><i class="fa fa-pencil" data-placement="top" data-toggle="tooltip" title="Izmeniti"></i></a>
									</div>
<?php } ?>
                                </div>
                            </div>
                        </div>
<nav>									
<ul class="pagination pagination-split">
<?php if($page_no > 1){ echo "<li class='page-item'><a href='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&page_no=$second_last' class='page-link'>$second_last</a></li>";
	echo "<li class='page-item'><a href='?legcid=$cid&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='?legcid=$cid&page_no=1' class='page-link'>1</a></li>";
	echo "<li class='page-item'><a href='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&page_no=$second_last' class='page-link'>$second_last</a></li>";
   echo "<li class='page-item'><a href='?legcid=$cid&page_no=$total_no_of_pages' class='page-link'>$total_no_of_pages</a></li>";      
		}
	
	else {
	echo "<li class='page-item'><a href='?legcid=$cid&page_no=1' class='page-link'>1</a></li>";
	echo "<li class='page-item'><a href='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&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='?legcid=$cid&page_no=$total_no_of_pages' class='page-link'>&raquo;&raquo;</a></li>";
	} ?>
</ul>
</nav>
                    </div>
                </div>
            </div>
        </div>

    </div>

</div>

<!-- Javascript -->
<script src="assets/bundles/libscripts.bundle.js"></script>    
<script src="assets/bundles/vendorscripts.bundle.js"></script>

<!-- Vedor js file and create bundle with grunt  --> 
<script src="assets/vendor/light-gallery/js/lightgallery-all.min.js"></script><!-- Light Gallery Plugin Js --> 
<script src="assets/vendor/light-gallery/js/mousewheel.min.js"></script><!-- Light Gallery Plugin Js -->
<script src="assets/vendor/dropify/js/dropify.js"></script>

<!-- Project core js file minify with grunt --> 
<script src="assets/bundles/mainscripts.bundle.js"></script>
<script src="../js/pages/medias/image-gallery.js"></script>
<script src="../js/pages/forms/dropify.js"></script>
</body>
</html>