<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start(); 
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 ='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_course_master.cmpbroucher like '%".$searchValue."%'  or res_course_master.cmfacility like '%".$searchValue."%'  or  res_course_master.cmduration like '%".$searchValue."%'  or res_course_master.cmfees like '%".$searchValue."%'  or res_course_master.cmeligibility like '%".$searchValue."%'  or res_course_master.cmremarks like '%".$searchValue."%'  or res_course_master.cmother like '%".$searchValue."%'  or res_course_master.levelofstudy in (select losmid from res_levelofstudy_master where levelofstudy like '%".$searchValue."%' ) or res_course_master.countrymid in (select countrymid from res_country_master where countryname like '%".$searchValue."%' ) or res_course_master.cnmid in (select cnmid from res_coursename_master where coursename like '%".$searchValue."%' ) or res_course_master.collegemid in (select collegemid from res_college_master where cmname like '%".$searchValue."%' )  
	                      ) ";
}


$aa="";

	if(isset($_SESSION['countrymid']))
	{
		//echo "Hii1";
		if($_SESSION['countrymid']=="All")
		{
		$aa="";
		}
		else
		{
			 $aa=" and res_course_master.countrymid=".$_SESSION['countrymid'];
		}
	}
	
	$bb="";
	if( isset($_SESSION['cnmid']))
	{
		//echo "Hii2";
		if($_SESSION['cnmid']=="All")
		{
		
		}
		else
		{
				$cname21=$_SESSION['cnmid'];
			 $bb=" and res_course_master.cnmid='$cname21'";
		}
	}
	
/* 
   $columnName = 'collegemid';
$columnSortOrder='asc';
$row=1;
$rowperpage=20;   */ 

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

## Total number of records with filtering
//echo $eve = "select count(*) as allcount from res_course_master,res_coursename_master WHERE res_course_master.cnmid = res_coursename_master.cnmid $aa $bb ".$searchQuery;
 $sel = mysqli_query($conn,"select count(*) as allcount from res_course_master,res_coursename_master WHERE res_course_master.cnmid = res_coursename_master.cnmid $aa $bb ".$searchQuery );
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
  $empQuery = "select * from res_course_master,res_college_master where res_course_master.collegemid = res_college_master.collegemid and res_course_master.collegemid = res_college_master.collegemid $aa $bb ".$searchQuery."  order by res_course_master.".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
//$empQuery = "select * from res_inquiry_master WHERE 1 ";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$i=1;
while ($rt = mysqli_fetch_assoc($empRecords)) {
	
	
	$coursemid=$rt['coursemid'];
				
				
				$collegemid=$rt['collegemid'];
				$losmid=$rt['levelofstudy'];
				$countrymid=$rt['countrymid'];
				$cnmid=$rt['cnmid'];
				
				$cmpbroucher=$rt['cmpbroucher'];
				$cmcoursebroucher=$rt['cmcoursebroucher'];
				$cmpbroucher1="<a target='_blank' href='./docs/$cmpbroucher'>download</a>";
				
				$cobroucher="<a target='_blank' href='./docs/$cmcoursebroucher'>download</a>";
				
				$piddetail='';
				 $currentfile1 = basename($_SERVER['PHP_SELF']);
				  
				  $currentfile = str_replace('ajax','',$currentfile1);
											 $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='coursedetail.php?id=".$rt['coursemid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-eye fa-lg'>  </a>";
					}
					$pidedit='';
					 $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='courseedit.php?id=".$rt['coursemid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit 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')
					{
				$piddelete="<a href='coursedelete.php?id=".$rt['coursemid']."' 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>";		
					}				
					$op=$piddetail."&nbsp;&nbsp;&nbsp; ".$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;			
				$cmdesc=str_replace ("'","'",$rt['cmdesc']);
				$cmdesc=str_replace ('"','\"',$cmdesc);
				
				$cprogramname=str_replace ("'","\'",$rt['cprogramname']);
				$cprogramname=str_replace ('"','\"',$cprogramname);
				
				$ctutionfees=str_replace ("'","\'",$rt['ctutionfees']);
				$ctutionfees=str_replace ('"','\"',$ctutionfees);
				
				$ctutionfeesin=str_replace ("'","\'",$rt['ctutionfeesin']);
				$ctutionfeesin=str_replace ('"','\"',$ctutionfeesin);
				
				$ctutionfees = $ctutionfees.'/'.$ctutionfeesin;
				
				$cmdocument=str_replace ("'","\'",$rt['cmdocument']);
				$cmdocument=str_replace ('"','\"',$cmdocument);
				
				$cprogramcategory=str_replace ("'","\'",$rt['cprogramcategory']);
				$cprogramcategory=str_replace ('"','\"',$cprogramcategory);
				
				$costofliving=str_replace ("'","\'",$rt['costofliving']);
				$costofliving=str_replace ('"','\"',$costofliving);
				
				$clangeligibility=str_replace ("'","\'",$rt['clangeligibility']);
				$clangeligibility=str_replace ('"','\"',$clangeligibility);
				
				$cmother=str_replace ("'","\'",$rt['cmother']);
				$cmother=str_replace ('"','\"',$cmother);
				
				
					
						$cmname=$rt['cmname'];
						$cmpbroucher=$rt['cmpbroucher'];
						if($cmpbroucher==""){
							$cmpbroucher1="";
							}
						else{
						$cmpbroucher1="<a target='_blank' href='./docs/$cmpbroucher'>download</a>";
						}
						$cmcoursebroucher=$rt['cmcoursebroucher'];
						if($cmcoursebroucher==""){
							$cmcoursebroucher1="";
							}
						else{
						$cmcoursebroucher1="<a target='_blank' href='./docs/$cmcoursebroucher'>download</a>";
						}
						
						
					
					
					$countryname="";
					$eve5= "select * from   res_country_master where countrymid=$countrymid";
					$re5 = mysqli_query($conn, $eve5);
					 while($rt5 = mysqli_fetch_assoc($re5))
					{
			
						//$losmid=$rt4['losmid'];
						$countryname=$rt5['countryname'];
						
					}
					$coursename="";
					$eve55= "select * from   res_coursename_master where cnmid=$cnmid";
					$re55 = mysqli_query($conn, $eve55);
					 while($rt55 = mysqli_fetch_assoc($re55))
					{
			
						
						$coursename=$rt55['coursename'];
						
					}
					$levelofstudy="";
					$eve55= "select * from   res_levelofstudy_master where losmid=$losmid";
					$re55 = mysqli_query($conn, $eve55);
					 while($rt55 = mysqli_fetch_assoc($re55))
					{
			
						$losmid=$rt55['losmid'];
						$levelofstudy=$rt55['levelofstudy'];
						
					}
					
				
				
					
					
    $data[] = array(
    		"coursemid"=>$i++,
    		"op"=>$op,
    		"cnmid"=>$coursename,
    		"cmdesc"=>$cmdesc,
    		"collegemid"=>$cmname,
    		"levelofstudy"=>$levelofstudy,
    		
    		"cprogramname"=>$cprogramname,
    		"ctutionfees"=>$ctutionfees,
    		"cprogramcategory"=>$cprogramcategory,
    		"costofliving"=>$costofliving,
    		"clangeligibility"=>$clangeligibility,
    		
    		"countrymid"=>$countryname,
    		
    		
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
