<?php
require_once("config/configDB.php"); 

// storing  request (ie, get/post) global array to a variable  
$requestData= $_REQUEST;


$userpermissionEdit 	= $_POST['userpermissionEdit'];
$userpermissionDelete 	= $_POST['userpermissionDelete'];
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$columns = array( 
// datatable column index  => database column name
	0 => 'id', 
	1 => 'productname',
	2 => 'purchase_rate',
    3 => 'gst',
	4 => 'hsn_code',
	5 => 'prodct_description',
    6 => 'productcode'
);

$sql = "SELECT id FROM product_master  ";
$query=mysqli_query($conn, $sql) or die("productgriddata.php: get product Data step 1");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  

$sql = "SELECT productcode, productname,isdelete, purchase_rate, gst, hsn_code,prodct_description,product_tamil,id ";
$sql.=" FROM product_master WHERE 1=1 ";
if( !empty($requestData['search']['value']) ) {   

	$sql.=" AND ( id  LIKE '".$requestData['search']['value']."%' ";    		$sql.=" OR productname LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR purchase_rate LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR gst LIKE '".$requestData['search']['value']."%' ";
	 $sql.=" OR product_tamil LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR hsn_code LIKE '".$requestData['search']['value']."%' ";
	$sql.=" OR prodct_description LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR productcode LIKE '".$requestData['search']['value']."%' )";
}
$query=mysqli_query($conn, $sql) or die("productgriddata.php: get product Data step 2");
$totalFiltered = mysqli_num_rows($query); 

$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";

$query=mysqli_query($conn, $sql) or die("productgriddata.php: get product Data step 3");
	
//cname, city, pincode, mobile, gst_type, ccode, open_balance
$data = array();
while( $row=mysqli_fetch_array($query) ) { 
	$nestedData=array(); 

	$nestedData[] = $row["id"];
	$nestedData[] = $row["productcode"];
    //$nestedData[] = $row["collectiondate"];
   
	$nestedData[] = $row["productname"];
	$nestedData[] = $row["product_tamil"];
   $nestedData[] = $row["purchase_rate"];
   //$nestedData[] = $row["gst"];
   //$nestedData[] = $row["hsn_code"];
	
    
	
	//$date = new DateTime($row["employee_joining_date"]); 
	//$nestedData[] = $date->format('d-m-Y'); //only fetching date
	//$nestedData[] = $row["open_balance"];
	
	
	
	$edit  = "";
	if($userpermissionEdit==1) {
		$edit .="<a href='productMasterEdit.php?id=".$row['id']."' id='productMaster_".$row['id']."' class='btn btn-primary'>Edit</a>&nbsp";
	}

	$deleted_status=$row["isdelete"];
	
	
	$Delete  = "";
	if($deleted_status=="1")
	{
				//$Delete  = "";
	if($userpermissionEdit==1) {
		$Delete .="<a   class='btn btn-danger '  href='fetchAjaxData/deleteProductmasterData.php?pid=".$row['id']."'>De-Activate</a>";
	}

	}
	
	else
	{
		
	if($userpermissionEdit==1) {
		$Delete .="<a   class='btn btn-success '  href='fetchAjaxData/activateProductmasterData.php?pid=".$row['id']."'>Activate</a>";
	}
	}
	$nestedData[] = $edit."&nbsp".$Delete;
	
	$data[] = $nestedData;
}

$json_data = array(
			"draw"            => intval( $requestData['draw'] ),   
			"recordsTotal"    => intval( $totalData ),  
			"recordsFiltered" => intval( $totalFiltered ), 
			"data"            => $data   
			);

echo json_encode($json_data);  

?>
