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

}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 (cityname like '%".$searchValue."%' or citycode like '%".$searchValue."%' or statemid in (select statemid from res_state_master where statename like '%".$searchValue."%') )  ";
}
/* 
 $columnName = 'citymid';
$columnSortOrder='asc';
$row=1;
$rowperpage=20; 
 */
## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from res_city_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_city_master WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
$empQuery = "select * from res_city_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)) {
	
	
	$citymid=$row['citymid'];
	
				$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='cityedit.php?id=".$row['citymid']."' 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 ccity from res_contact_master where ccity=$citymid";
				$re11 = mysqli_query($conn, $eve11);
	
				if(mysqli_num_rows($re11)>0 )
					{
					}else{
				$piddelete="<a href='citydelete.php?id=".$row['citymid']."'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;
								
								
				$cityname=str_replace ("'","'",$row['cityname']);
				$cityname=str_replace ('"','\"',$cityname);
				$citycode=str_replace ("'","'",$row['citycode']);
				$citycode=str_replace ('"','\"',$citycode);
				$statemid=$row['statemid'];
				
				$eve1 = "select * from res_state_master where statemid=$statemid";
				$re1 = mysqli_query($conn, $eve1);
				$statename='';
				if(mysqli_num_rows($re1) > 0)
				{
					
					 while($rt1 = mysqli_fetch_assoc($re1))
					 {
							
						$statename=$rt1['statename'];
					 }
				}
				
				
					
					
    $data[] = array(
    		"citymid"=>$i++,
    		"op"=>$op,
    		"citycode"=>$citycode,
    		"cityname"=>$cityname,
    		"statemid"=>$statename,
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
