<?php  
session_start(); 
if(!isset($_SESSION['tulsilogin']))
	{
		header('Location: index.php');
		return;
	}

	$tt="";
	if(isset($_SESSION['inqcountid']))
	{
			$id=$_SESSION['inqcountid'];

		$tt=" imstatus='Registered' and ";
		$tt1=" imstatus='Registered' ";
		
	}
	unset($_SESSION['inqcountid']);
	include("connect.php");	
	header("Access-Control-Allow-Origin: *");
	
	
	/* $aa="";
	if(isset($_SESSION['daterange']))
	{
		if($_SESSION['daterange']=="")
		{
		
		}
		else
		{
			
			$aa=" and idate between ".$_SESSION['daterange'];
			$aa1="idate between ".$_SESSION['daterange'];
			
		}

	} */
	
	$aa="";
	if(isset($_SESSION['trandatelower']))
	{
		$lower = $_SESSION['trandatelower'];
		$upper = $_SESSION['trandateupper'];
	
		if($_SESSION['trandatelower']=="")
		{
		
		}
		else
		{
			
			$aa=" and idate between '$lower' and '$upper'";
			$aa1="idate between '$lower' and '$upper'";
			
		}

	}
	
	unset($_SESSION['daterange']);
	
	$bb="";
	if( isset($_SESSION['imstatus']))
	{
		if($_SESSION['imstatus']=="all")
		{
		
		}
		else
		{
				$imstatus=$_SESSION['imstatus'];
			$bb=" and imstatus='$imstatus'";
		}
	}
	unset($_SESSION['imstatus']);
	
	
	if($_SESSION['utype']=='branchmanager')
	{
		 $eve = "select * from res_inquiry_master where $tt entrybyuserid=".$_SESSION['umid']."$aa $bb order by imid desc";
		$re = mysqli_query($conn, $eve);
		}
	else if($_SESSION['utype']=='receptionist')
	{
		$eve = "select * from res_inquiry_master where $tt entrybyuserid=".$_SESSION['umid']."$aa $bb order by imid desc";
		$re = mysqli_query($conn, $eve);
	}
	else if($_SESSION['utype']=='admin')
	{
		 $eve = "select * from res_inquiry_master where $tt 1 $aa $bb order by imid desc";
		$re = mysqli_query($conn, $eve);
	}
	else if($_SESSION['utype']=='counseller')
	{
		$eve = "select * from res_inquiry_master where $tt inqbranch=".$_SESSION['branchid']."$aa $bb and (imid in (select imid from res_counsellor_detail where counsellorid=".$_SESSION['umid'].") or entrybyuserid in (select umid from res_user_master where umid=".$_SESSION['umid']."))  order by imid desc";
		$re = mysqli_query($conn, $eve);
	}
	else if($_SESSION['utype']=='agent')
	{
		$eve = "select * from res_inquiry_master  where $tt inqbranch=".$_SESSION['branchid']." and entrybyuserid=".$_SESSION['umid']."$aa $bb order by imid desc";
		$re = mysqli_query($conn, $eve);
	}
	else
	{
		return;
	
	}

	
	
	
	
	$response = array();
	$posts = array();
	
	$result="0";
	
	if(mysqli_num_rows($re) > 0)
	{
		$i=1;
		 while($rt = mysqli_fetch_assoc($re))
		 {
			 	
				$imid=$rt['imid'];
				
				$pidedit="<a href='inquiryedit.php?id=".$rt['imid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg' >  </a>";
				
				$piddelete="<a href='inquirydelete.php?id=".$rt['imid']."'  onclick='return confirm(&#39;Are you sure you want to delete ?&#39;);' data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-trash-alt fa-lg'> </a>";	
				
				
				$imstatus="<a href='inquirydetail.php?id=".$rt['imid']."'   data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-eye fa-lg'> </a>";	
				
				

                $op= $imstatus."&nbsp;&nbsp;&nbsp; ".$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;
								
				$imstatus=str_replace ("'","'",$rt['imstatus']);
				$imstatus=str_replace ('"','\"',$imstatus);
				
					if($imstatus == 'New')	
					{						
				$imstatus2="<div class='badge badge-light-info'>$imstatus</div>";
					}else if($imstatus == 'Inquiry Details')
					{
						$imstatus2="<div class='badge badge-light-success'>$imstatus</div>";
					}else{
						$imstatus2="<div class='badge badge-light-danger'>$imstatus</div>";
					}						
				
				$visatmid=$rt['visatmid'];
				$iintakemonth=$rt['iintakemonth'];
				$iintakeyear=$rt['iintakeyear'];
				$istudentcategory=$rt['istudentcategory'];
				
			
				$iaddedtype=$rt['iaddedtype'];
				$entrybyuserid=str_replace ("'","'",$rt['entrybyuserid']);
				$entrybyuserid=str_replace ('"','\"',$entrybyuserid);
				$eveag = "select ufullname,utype from res_user_master where umid=$entrybyuserid";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$entrybyuserid=$rtag['ufullname'];
						$utype=$rtag['utype'];
					}
				}
				else
				{
					$entrybyuserid="";
					$utype="";
				}
				if($iaddedtype=='')
				{
					$iaddedtype=$utype;
				}	
					
					$entrybyuserid1 = $entrybyuserid." (".$iaddedtype.")";
				$eve2 = "select * from res_visatype_master where visatmid='$visatmid'";
				$re2 = mysqli_query($conn, $eve2);
				$visatype="";
				
					while($rt2 = mysqli_fetch_assoc($re2))
					{
						$visatype=$rt2['visatype'];
					}
				
				$eve3 = "select * from res_user_master  where umid in (select inqbranch from res_inquiry_master where imid=$imid)";
				$re3 = mysqli_query($conn, $eve3);
				if(mysqli_num_rows($re3)>0)
				{
					while($rt3 = mysqli_fetch_assoc($re3))
				{
					
					$ufullname1=$rt3['ufullname'];
				}
				}
				else
				{
					$ufullname1="";
				}
				
					
					
				
				$idate=implode('-', array_reverse(explode('-', $rt['idate'])));
				
				$ipname=str_replace ("'","'",$rt['ipname']);
				$ipname=str_replace ('"','\"',$ipname);
				
				$ipsurname=str_replace ("'","'",$rt['ipsurname']);
				$ipsurname=str_replace ('"','\"',$ipsurname);
				
				$ipname1 = $ipname." ".$ipsurname;
				
				$ipaddress=str_replace ("'","'",$rt['ipaddress']);
				$ipaddress=str_replace ('"','\"',$ipaddress);
				
				$ipemail=str_replace ("'","'",$rt['ipemail']);
				$ipemail=str_replace ('"','\"',$ipemail);
				
				$ipmobile=str_replace ("'","'",$rt['ipmobile']);
				$ipmobile=str_replace ('"','\"',$ipmobile);
				
				
				$ipgender=str_replace ("'","'",$rt['ipgender']);
				$ipgender=str_replace ('"','\"',$ipgender);
				
				$ipdob=str_replace ("'","'",$rt['ipdob']);
				$ipdob=str_replace ('"','\"',$ipdob);
				
				$ipbirthcountry=str_replace ("'","'",$rt['ipbirthcountry']);
				$ipbirthcountry=str_replace ('"','\"',$ipbirthcountry);
				
				$ipbirthcity=str_replace ("'","'",$rt['ipbirthcity']);
				$ipbirthcity=str_replace ('"','\"',$ipbirthcity);
				
				$inqbranch=$rt['inqbranch'];
				
				$interestedcountryvisatype =str_replace ("'","'",$rt['interestedcountryvisatype']);
				$interestedcountryvisatype =str_replace ('"','\"',$interestedcountryvisatype );
				
				
				$servicesrequired =str_replace ("'","'",$rt['servicesrequired']);
				$servicesrequired =str_replace ('"','\"',$servicesrequired );
				
				$sourceofinq =str_replace ("'","'",$rt['sourceofinq']);
				$sourceofinq =str_replace ('"','\"',$sourceofinq );
				
				$attendedby =str_replace ("'","'",$rt['attendedby']);
				$attendedby =str_replace ('"','\"',$attendedby );
				
				$imremarks =str_replace ("'","'",$rt['imremarks']);
				$imremarks =str_replace ('"','\"',$imremarks );
				
				$imdetails =str_replace ("'","'",$rt['imdetails']);
				$imdetails =str_replace ('"','\"',$imdetails );
				
				
				//if($imstatus=="Followup")
				//{
					
					$imstatus="<a href='inquirydetail.php?id=$imid' ><span class='label label-sm label-info'>Click Here</span></a>";
				//}
				//else if($istatus=="Close")
				//{
					
					//$imstatus="<a href='inquirystatus.php?id=$imid' ><span class='label label-sm label-danger'>Close</span></a>";
				//}
				$coun="";
				$tt1="";
				
				$eve1 = "select * from res_user_master  where umid in(select counsellorid from res_counsellor_detail where imid=$imid)";
				$re1 = mysqli_query($conn, $eve1);
				while($rt1 = mysqli_fetch_assoc($re1))
				{
					$tt1=$rt1['ufullname'];										
				}
							
						
				$posts[] = array('srno'=> $i++,'op'=> $op, 'imstatus'=> $imstatus,'imstatus2'=> $imstatus2,'entrybyuserid'=> $entrybyuserid1,'idate'=> $idate,'ipname'=> $ipname1,'ipaddress'=> $ipaddress,'ipemail'=> $ipemail,'ipmobile'=> $ipmobile,'ipgender'=> $ipgender,'ipdob'=> $ipdob,'ipbirthcountry'=> $ipbirthcountry,'ipbirthcity'=> $ipbirthcity,'interestedcountryvisatype'=> $interestedcountryvisatype,'servicesrequired'=> $servicesrequired,'sourceofinq'=> $sourceofinq,'attendedby'=> $attendedby,'imremarks'=> $imremarks,'imdetails'=> $imdetails,'visatype'=> $visatype,'tt1'=> $tt1,'ufullname1'=> $ufullname1,'iaddedtype'=> $iaddedtype,'iintakemonth'=> $iintakemonth,'iintakeyear'=> $iintakeyear,'istudentcategory'=> $istudentcategory);
		
		
		}
		 
		$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);
	
	
?>