<?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 == 4)
{
	$columnName = 'statemid';

}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 (statename like '%".$searchValue."%' or countrymid in (select countrymid from res_country_master where countryname like '%".$searchValue."%') )  ";
}

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

## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from res_state_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_state_master WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_state_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)) {
	
	
	$statemid=$row['statemid'];
	
				$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='stateedit.php?id=".$row['statemid']."' 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')
					{
				$eve11 = "select statemid from res_city_master where statemid=$statemid";
				$re11 = mysqli_query($conn, $eve11);
	
				if(mysqli_num_rows($re11)>0 )
					{
					}else{
				$piddelete="<a href='statedelete.php?id=".$row['statemid']."'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;
								
								
				$statename=str_replace ("'","'",$row['statename']);
				$statename=str_replace ('"','\"',$statename);
				$statecode=str_replace ("'","'",$row['statecode']);
				$statecode=str_replace ('"','\"',$statecode);
				$countrymid=$row['countrymid'];
				
				$eve1 = "select * from res_country_master where countrymid=$countrymid";
				$re1 = mysqli_query($conn, $eve1);
				$countryname='';
				if(mysqli_num_rows($re1) > 0)
				{
					
					 while($rt1 = mysqli_fetch_assoc($re1))
					 {
							
						$countryname=$rt1['countryname'];
					 }
				}
				
				
					
					
    $data[] = array(
    		"statemid"=>$i++,
    		"op"=>$op,
    		"statecode"=>$statecode,
    		"statename"=>$statename,
    		"countrymid"=>$countryname,
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
