<?php
$where 					= "where 1=1";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}

$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : "";
if($search) {
    $where	.= " AND ( title LIKE '%$search%' ) ";
    $next_page_condition.="&search=$search";
}
$category = isset($_REQUEST['category']) ? $_REQUEST['category'] : "";
if($category) {
    $where	.= " AND ( category = '$category') ";
    $next_page_condition.="&category=$category";
}
$event 	= isset($_REQUEST['event']) ? $_REQUEST['event'] : "";
$booking 	= isset($_REQUEST['booking']) ? $_REQUEST['booking'] : "";
$from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($event == 1){
	if($from_date && $to_date){
		$where .= " AND event_duration_from BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59'  ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
	}else if($from_date){
		$where.= " AND event_duration_from >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND event_duration_from <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
}else if($booking == 1){
	if($from_date && $to_date){
		$where .= " AND booking_duration_from BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59'  ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
	}else if($from_date){
		$where.= " AND booking_duration_from >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND booking_duration_from <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
}else {
	if($from_date && $to_date){
		$where .= " AND created_on BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
	}else if($from_date){
		$where.= " AND created_on >= '$from_date' ";
		$next_page_condition.="&from_date=".$_REQUEST['from_date'];
	}else if($to_date){
		$where.= " AND created_on <= '$to_date' ";
		$next_page_condition.="&to_date=".$_REQUEST['to_date'];
	}
} 
###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "ess_image_template";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "ESS Image Template";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name	".$where." LIMIT $eu, $limit";
$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);
$total_no_of_template = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	$id = $_REQUEST['id'];
	$sql=mysqli_query($conn,"DELETE FROM ess_image_template WHERE id ='$id' ");
	 $_SESSION['info_message'] = "Record deleted sucessfully";
	header("location: $file_name?page=view_ess_template".$next_page_condition.$paging);
   
    exit();	
}
#################################  END DELETE    #########################################
?>
<section class="content-header">
  <h1>
	<?=$page_header;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<div class="box">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message'];?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
				<div class="box-body">
				<!-- search form -->
				<?php include_once('ess_template_search_option.inc.php');?>
				<!-- /.search form -->
				Total No. of Templates - <?=$total_no_of_template;?>
					<form action="index?page=add_ess_template" method="post" name="form1" id="form1">
					<div class="box-footer">
					   <button type="submit" name="add_ess_template" class="btn btn-primary">Add ESS Template</button>
					</div>
					<? if($result>0){?>
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<th>Title</th>
							<th>Image 1</th>
							<th>Image 2</th>
                            <th>Image 3</th>
                            <th>Image 4</th>
							<th>Edit</th>
							<th>Delete</th>
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
							?>
						  <tr>                   
							<td><?php echo $row['title']; ?></td>
							<td><?		
								$dir = "../ess_images/";
								 if (is_file($dir.$row['ess_image_1'])) {						 
										$image_path_1 ='<a href="../'.$dir.$row['ess_image_1'].'" target="_blank"> <img style="width:100px;border:1px solid #777;" src="../'.$dir.$row['ess_image_1'].'"></a>';
									}else{
										$image_path_1 ='<img style="width:100px;border:1px solid #777;" src="../'.$dir.'blank_imag.png">';	
									}
									echo $image_path_1;
								?>
							</td>
							<td><?		
								$dir = "../ess_images/";
								 if (is_file($dir.$row['ess_image_2'])) {						 
										$image_path_2 ='<a href="../'.$dir.$row['ess_image_2'].'" target="_blank"> <img style="width:100px;border:1px solid #777;" src="../'.$dir.$row['ess_image_2'].'"></a>';
									}else{
										$image_path_2 ='<img style="width:100px;border:1px solid #777;" src="../'.$dir.'blank_imag.png">';	
									}
									echo $image_path_2;
								?>
							</td>
                            <td><?		
								$dir = "../ess_images/";
								 if (is_file($dir.$row['ess_image_3'])) {						 
										$image_path_3 ='<a href="../'.$dir.$row['ess_image_3'].'" target="_blank"> <img style="width:100px;border:1px solid #777;" src="../'.$dir.$row['ess_image_3'].'"></a>';
									}else{
										$image_path_3 ='<img style="width:100px;border:1px solid #777;" src="../'.$dir.'blank_imag.png">';	
									}
									echo $image_path_3;
								?>
							</td>
                            <td><?		
								$dir = "../ess_images/";
								 if (is_file($dir.$row['ess_image_4'])) {						 
										$image_path_4 ='<a href="../'.$dir.$row['ess_image_4'].'" target="_blank"> <img style="width:100px;border:1px solid #777;" src="../'.$dir.$row['ess_image_4'].'"></a>';
									}else{
										$image_path_4 ='<img style="width:100px;border:1px solid #777;" src="../'.$dir.'blank_imag.png">';	
									}
									echo $image_path_4;
								?>
							</td>
                            <td><a href="index?page=add_ess_template&action=edit&id=<?=$row['id'].$next_page_condition.$paging;?>"> <img src="../images/edit.gif" border="0" /></a></td>
							<td><a href="index?page=view_ess_template&action=delete&id=<?=$row['id'].$next_page_condition.$paging;?>"onclick="javascript:return confirm('Are you sure! You want to delete');"><img src="../images/del.gif" border="0" /> </a></td>
						  </tr>
							<? } ?>
							<!-- Paging starts here  -->
							<? 
								$next_page_condition .="&page=view_ess_template";
								include("paging/paging_row.inc.php") ?>
							<!-- Paging ends starts here  -->
						</tbody>
					 </table>
					 <? }else{ ?>
						    No	Record found...
					 <? } ?>
				</div>
				<div class="box-footer">
                   <button type="submit" name="add" class="btn btn-primary">Add ESS Template</button>
                </div>
				</form>
			</div>
		</div>
	</div>
</section>
<script type="text/javascript">

</script>