<?php
session_start();
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'connect.php';

## Read value

$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
if($columnIndex == 1)
{
$columnName ='imid';
}else{
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
}
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and (imstatus like '%".$searchValue."%' or 
        ipname like '%".$searchValue."%' or 
        ipemail like'%".$searchValue."%' or 
        ipmobile like'%".$searchValue."%' or 
        ipsurname like'%".$searchValue."%' ) ";
}
 /* $columnName = 'iaddedfrom';
$columnSortOrder='asc';
$row=1;
$rowperpage=20;  */

if($_SESSION['utype']=='admin')
	{

## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master where iaddedfrom in ('AgentPanel','PlanStudies')");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
 $sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master WHERE iaddedfrom in ('AgentPanel','PlanStudies') ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_inquiry_master WHERE iaddedfrom in ('AgentPanel','PlanStudies') ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;

	}else if($_SESSION['utype']=='branchmanager' || $_SESSION['utype']=='receptionist')
	{
		
	$sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master where entrybyuserid=".$_SESSION['umid']."");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
 $sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master WHERE entrybyuserid=".$_SESSION['umid']."  ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_inquiry_master WHERE entrybyuserid=".$_SESSION['umid']." ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;

}
	else if($_SESSION['utype']=='counseller')
	{
		$sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master where entrybyuserid=".$_SESSION['umid']."");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
 $sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master WHERE entrybyuserid=".$_SESSION['umid']."  ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_inquiry_master WHERE entrybyuserid=".$_SESSION['umid']." 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'].")) ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
		
		}
	else if($_SESSION['utype']=='agent')
	{
		$sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master where entrybyuserid=".$_SESSION['umid']."");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
 $sel = mysqli_query($conn,"select count(*) as allcount from res_inquiry_master WHERE entrybyuserid=".$_SESSION['umid']."  ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_inquiry_master WHERE inqbranch=".$_SESSION['branchid']." and entrybyuserid=".$_SESSION['umid']." ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
	}
else
	{
		return;
	
	}
	
//$empQuery = "select * from res_inquiry_master WHERE 1 ";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$i=1;
while ($row = mysqli_fetch_assoc($empRecords)) {
	$imid=$row['imid'];
	$imstatus = $row['imstatus'];
	
	$ipmobile=str_replace ("'","'",$row['ipmobile']);
				$ipmobile=str_replace ('"','\"',$ipmobile);
				
	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 if($imstatus == 'Counselling Details')
					{
						$imstatus2="<div class='badge badge-light-primary'>$imstatus</div>";
					}else{
						$imstatus2="<div class='badge badge-light-danger'>$imstatus</div>";
					}	
					
					$ipname=str_replace ("'","'",$row['ipname']);
				$ipname=str_replace ('"','\"',$ipname);
				
				$ipsurname=str_replace ("'","'",$row['ipsurname']);
				$ipsurname=str_replace ('"','\"',$ipsurname);
				
				$ipname1 = $ipname." ".$ipsurname;
					
					$iaddedtype=$row['iaddedtype'];
				$agentid=str_replace ("'","'",$row['agentid']);
				$agentid=str_replace ('"','\"',$agentid);
				$eveag = "select ufullname,utype from res_user_master where umid=$agentid";
				$reag = mysqli_query($conn, $eveag);
				if(mysqli_num_rows($reag) > 0)
				{
					while($rtag = mysqli_fetch_assoc($reag))
					{
						$agentid=$rtag['ufullname'];
						$utype=$rtag['utype'];
					}
				}
				else
				{
					$agentid="";
					$utype="";
				}
				if($iaddedtype=='')
				{
					$iaddedtype=$utype;
				}	
					
					
					
					$idate=date("d-m-Y", strtotime($row['idate']));
					
					
					$eve3 = "select * from res_branch_master  where branchmid 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['branchname'];
				}
				}
				else
				{
					$ufullname1="";
				}
				
				$ipemail=str_replace ("'","'",$row['ipemail']);
				$ipemail=str_replace ('"','\"',$ipemail);
				
				$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'];										
				}
				
				$pidedit="<a href='inquiryedit.php?id=".$row['imid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg' >  </a>";
				
				$piddelete="<a href='inquirydelete.php?id=".$row['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=".$row['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;
				 
    $data[] = array(
    		"imid"=>$i++,
    		"idate"=>$idate,
    		"ufullname"=>$ufullname1,
    		"ipname"=>$ipname1,
    		"imstatus"=>$imstatus2,
    		"ipemail"=>$ipemail,
    		"ipmobile"=>$ipmobile,
    		"agentid"=>$agentid,
    		"counsellorid"=>$tt1,
    		"op"=>$op
    		
			

				
			
    	);
}

## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
