<?php  
session_start(); 

	include("connect.php");	
	header("Access-Control-Allow-Origin: *");
	if(!isset($_SESSION['linkarisecrm']))
	{
		header('Location: index.php');
		return;
	}
		$brmid=$_SESSION['linkarisebranchid'];
		$cmpmid=$_SESSION['linkarisecmpmid'];
		
		date_default_timezone_set("Asia/Singapore");
	$mdate= date('Y-m-d H:i:s');
	$mdate1= date('Y-m-d');
	
	 if(isset($_SESSION['spotmid']))
	 {
		 $spotmid = $_SESSION['spotmid'];
		if($spotmid == 'All')
		{
			$bb = "";
		}else{
			$bb = "and spotmid='$spotmid'";
		}			
	 }else
	 {
		$bb = ""; 
	 }
	 
	 if(isset($_SESSION['lowerdate']))
	 {
		 $lowerdate = $_SESSION['lowerdate'];
		 $upperdate = $_SESSION['upperdate'];
		$aa = "and Date(availabledate) between '$lowerdate' and '$upperdate'";		
	 }else
	 {
		$aa = ""; 
	 }
	 
	  if(isset($_SESSION['slotmid']))
	 {
		 $slotmid = $_SESSION['slotmid'];
		if($slotmid == 'All')
		{
			$cc = "";
		}else{
			$cc = "and slotmid='$slotmid'";
		}			
	 }else
	 {
		$cc = ""; 
	 }
	 
	  if(isset($_SESSION['status']))
	 {
		 $status = $_SESSION['status'];
		if($status == 'All')
		{
			$dd = "";
		}else{
			$dd = "and availablestatus='$status'";
		}			
	 }else
	 {
		$dd = ""; 
	 }
	
	 $eve = "select * from res_availibility_detail where cmpmid=$cmpmid and availabledate>='$mdate1' $aa $bb $cc $dd";
	
	$re = mysqli_query($conn, $eve);
	
	$response = array();
	$posts = array();
	
	$result="0";
	
	if(mysqli_num_rows($re) > 0)
	{
		$i=1;
		while($rt = mysqli_fetch_assoc($re))
		{
			 	$spotmid=$rt['spotmid'];
			 	$slotmid=$rt['slotmid'];
			 	$availabledate=date("d-m-Y", strtotime($rt['availabledate']));  
			 	$availablestatus=$rt['availablestatus'];
			 	$starttime=$rt['starttime'];
			 	$endtime=$rt['endtime'];
		$spotname="";
			$spotaddress="";
			$slotname="";
				$eve1 = "select * from res_spot_master where spotmid=$spotmid and cmpmid=$cmpmid";
				$re1 = mysqli_query($conn, $eve1);
				if(mysqli_num_rows($re1) > 0)
		while($rt1 = mysqli_fetch_assoc($re1))
		{
			$spotname=$rt1['spotname'];
			$spotaddress=$rt1['spotaddress'];
		}
			
			$eve12 = "select * from res_slots_master where slotmid=$slotmid and cmpmid=$cmpmid";
				$re12 = mysqli_query($conn, $eve12);
				if(mysqli_num_rows($re12) > 0)
		while($rt12 = mysqli_fetch_assoc($re12))
		{
			$slotname=$rt12['slotname'];
			
		}	
				
			
				
				
				$posts[] = array('spotmid'=> $i, 'spotname'=> $spotname, 'spotaddress'=> $spotaddress,'availabledate'=> $availabledate,'slotname'=> $slotname,'starttime'=> $starttime,'endtime'=>$endtime,'status'=>$availablestatus);
	
			
			$i++;
			
		}
		 
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('data' => $posts)));
		mysqli_close($conn);
		return;
	}
	
   else
	{
		$result="0";
		$msg="Invalid Error";
		
		$posts[] = array('p1'=> $result,'p2'=> $msg);
	
		$response['posts'] = $posts;
		echo stripslashes(json_encode( array('item' => $posts)));
	}
   
	mysqli_close($conn);	
?>