<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'connect.php';
session_start();
## 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 = 'collegemid';

}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 (res_college_master.cmname like '%".$searchValue."%' or 
        res_contact_master.caddress1 like '%".$searchValue."%' or 
        res_contact_master.caddress2 like '%".$searchValue."%' or 
        res_contact_master.contactname like '%".$searchValue."%' or 
        res_contact_master.cmiddlename like '%".$searchValue."%' or 
        res_contact_master.csurname like '%".$searchValue."%'  or 
        res_contact_master.contactnumber like '%".$searchValue."%'  or 
        res_contact_master.contactemail like '%".$searchValue."%' ) ";
}


 /* $columnName = 'collegemid';
$columnSortOrder='asc';
$row=1;
$rowperpage=20;  */

## Total number of records without filtering
$sel = mysqli_query($conn,"select  count(*) as allcount from res_college_master,res_contact_master where res_contact_master.clinkid = res_college_master.collegemid and res_contact_master.clinktype = 'College'   ");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
 $sel = mysqli_query($conn,"select  count(*) as allcount from res_college_master,res_contact_master where res_contact_master.clinkid = res_college_master.collegemid and res_contact_master.clinktype = 'College'  ".$searchQuery." ");
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];

//echo $aa = "select * from res_college_master,res_contact_master where res_contact_master.clinkid = res_college_master.collegemid and res_contact_master.clinktype = 'College' ".$searchQuery." order by res_college_master.".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
## Fetch records
$empQuery = "select  * from res_college_master,res_contact_master where res_contact_master.clinkid = res_college_master.collegemid and res_contact_master.clinktype = 'College'  ".$searchQuery." order by res_college_master.".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
//$empQuery = "select * from res_inquiry_master WHERE 1 ";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$i=1;
while ($row = mysqli_fetch_assoc($empRecords)) {
	
	
	$collegemid=$row['collegemid'];
				
				$pidedit='';
				 $currentfile1 = basename($_SERVER['PHP_SELF']);
				  
				  $currentfile = str_replace('ajax','',$currentfile1);
				   $rolemid = $_SESSION['userrolemid'];
											 $eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and maedit=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				
				$pidedit="<a href='collegeedit.php?id=".$row['collegemid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg'>  </a>";
					}
					$piddetail='';
				$eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and maview=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				
				$piddetail="<a href='collegedetail.php?id=".$row['collegemid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-eye fa-lg'>  </a>";
					}
				$piddelete='';
				$eve23 = "select * from res_moduleallocation_detail where smodulemid in (select smodulemid from res_submodule_master where smfilename='$currentfile') and madelete=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
						$eve11 = "select collegemid from res_course_master where collegemid=$collegemid";
				$re11 = mysqli_query($conn, $eve11);
	
				if(mysqli_num_rows($re11)>0 )
					{
						$piddelete='';
					}else{
				
				$piddelete="<a href='collegedelete.php?id=".$row['collegemid']."' 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>";	
					}
					}					

	$cmname=str_replace ("'","'",$row['cmname']);
				$cmname=str_replace ('"','\"',$cmname);
				
				$cmaddress1=str_replace ("'","'",$row['caddress1']);
				$cmaddress1=str_replace ('"','\"',$cmaddress1);
				
				$cmaddress2=str_replace ("'","'",$row['caddress2']);
				$cmaddress2=str_replace ('"','\"',$cmaddress2);
				
				$cmaddress = $cmaddress1." ".$cmaddress2;
				
				$cmiddlename=str_replace ("'","'",$row['cmiddlename']);
				$cmiddlename=str_replace ('"','\"',$cmiddlename);
				
				$contactname=str_replace ("'","'",$row['contactname']);
				$contactname=str_replace ('"','\"',$contactname);
				$csurname=str_replace ("'","'",$row['csurname']);
				$csurname=str_replace ('"','\"',$csurname);
				
				$cmcontactperson = $contactname." ".$cmiddlename." ".$csurname;
				
				
				$cmphone=str_replace ("'","'",$row['contactnumber']);
				$cmphone=str_replace ('"','\"',$cmphone);
				
				$cmemail=str_replace ("'","'",$row['contactemail']);
				$cmemail=str_replace ('"','\"',$cmemail);
				
				$countrymid=str_replace ("'","'",$row['ccountry']);
				$countrymid=str_replace ('"','\"',$countrymid);
				
				$eve2= "select countryname from res_country_master where countrymid=$countrymid";
					$re2 = mysqli_query($conn, $eve2);
					 while($row2 = mysqli_fetch_assoc($re2))
					{
						$countryname=$row2['countryname'];
					}
				
					
					
				$op= $piddetail."&nbsp;&nbsp;&nbsp; ".$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;	
					
				
					
					
    $data[] = array(
    		"collegemid"=>$collegemid,
    		"op"=>$op,
    		"cmname"=>$cmname,
    		"cmaddress"=>$cmaddress,
    		"cmcontactperson"=>$cmcontactperson,
    		"cmphone"=>$cmphone,
    		"cmemail"=>$cmemail,
    		"countrymid"=>$countryname,
    		
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
