<?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 = 'cnmid';

}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 (coursename like '%".$searchValue."%'  or coursenamecode like '%".$searchValue."%') ";
}

 /* $columnName = 'coursename';
$columnSortOrder='asc';
$row=1;
$rowperpage=20; 
 */
## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from res_coursename_master");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

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


## Fetch records
$empQuery = "select * from res_coursename_master WHERE 1 ".$searchQuery." order by ".$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)) {
	
	
	$cnmid=$row['cnmid'];
	$coursenamecode=$row['coursenamecode'];
				$pidedit='';
				 $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 maedit=1 and  rolemid = ".$_SESSION['userrolemid'];
											$re23 = mysqli_query($conn, $eve23);
	
				if(mysqli_num_rows($re23)>0 || $_SESSION['utype'] == 'admin')
					{
				$pidedit="<a href='coursenameedit.php?id=".$row['cnmid']."' 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='coursenamedelete.php?id=".$row['cnmid']."'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=$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;
               // $op=$piddelete;
								
								
				$coursename=$row['coursename'];
				
				
					
					
    $data[] = array(
    		"cnmid"=>$i++,
    		"op"=>$op,
    		"coursenamecode"=>$coursenamecode,
    		"coursename"=>$coursename,
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
