<?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 where isdelete=1";
$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, purchase_rate, gst, hsn_code,prodct_description,product_type,id ";
$sql.=" FROM product_master WHERE 1=1 and isdelete=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_type 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_type"];
   $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";
	}

	$Delete  = "";
	if($userpermissionDelete==1) {
		//$Delete .="<a id='productMasterDelete_".$row['id']."' class='btn btn-primary' onclick='ConfirmproductMasterDelete(".$row['id'].")' >Delete</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);  

?>
