<?php 
session_start();
header('Content-Type: text/html; charset=utf-8');


$client_id = $_SESSION['client_id'];


require_once "writedblog.php";
require_once "logger.php";

$dbset = json_decode($_SESSION['settingsApp'],true);
$db = new mysqli ($dbset['DB']['DB_HOST'],$dbset['DB']['DB_USER'],$dbset['DB']['DB_PASS'],$dbset['DB']['DB_NAME']);



$db->query("SET CHARSET SET utf8");
$db->query("SET NAMES 'utf8'");
if (mysqli_connect_errno()) {
	echo "Error connecting to DB";
	exit;
}


if ($_REQUEST['func'] == 'show') {

$query = "SELECT `id`,`NameGroup`,`NameCargo`, barcode,`Comments`,`Cost1`,`Cost2`,`Cost3` FROM `mc_cargo` WHERE instance_id = ".$client_id.";";
$data = array();
if( $stmt = $db->prepare($query) ){
if( $stmt->execute() ){
$stmt->bind_result($id,$NameGroup,$NameCargo,$barecode, $Comments,$Cost1,$Cost2,$Cost3);
while( $stmt->fetch() ){
	$data[] = array(
			'id' => $id,
			'NameGroup' => $NameGroup,
			'NameCargo' => $NameCargo,
			'barcode'=> $barecode,
			'Comments' => $Comments,
			'Cost1' => $Cost1,
			'Cost2' => $Cost2,
			'Cost3' => $Cost3
		);
}
}
}
$stmt->close();

echo'{rows:'.json_encode($data).'}';

$db->close();
exit;
}



if ($_REQUEST['func'] == 'addedit') {
	
$id = $_REQUEST['id'];
$Name = $_REQUEST['Name'];
$Group = $_REQUEST['Group'];
$Cost1 = $_REQUEST['Cost1'];
$Cost2 = $_REQUEST['Cost2'];
$Cost3 = $_REQUEST['Cost3'];
$barcode = $_REQUEST['barcode'];
	
	if($id || $id!=''){
		$query = "UPDATE `mc_cargo` SET `NameGroup`='".$Group."',`NameCargo`='".$Name."',`Cost1`=".$Cost1.",`Cost2`=".$Cost2.",`Cost3`=".$Cost3.", barcode = '".$barcode."' WHERE id = ".$id."";
	}else{
		$query = "INSERT INTO `mc_cargo` (`NameGroup`,`NameCargo`,`Cost1`,`Cost2`,`Cost3`, barcode, `instance_id`) VALUES ('".$Group."','".$Name."',".$Cost1.",".$Cost2.",".$Cost3.", '".$barcode."', ".$client_id.")";
	}
	//echo $query;
	if( $stmt = $db->prepare($query) ){
	if( $stmt->execute() ){
	$stmt->close();
		echo json_encode(array('success'=>true, 'message'=>''));
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	
	$db->close();
	exit;
}



if ($_REQUEST['func'] == 'group') {

$query = "SELECT `id`,`NameGroup` FROM `mc_cargo` WHERE instance_id = ".$client_id." 
		GROUP BY NameGroup;";
$data = array();
if( $stmt = $db->prepare($query) ){
if( $stmt->execute() ){
$stmt->bind_result($id,$NameGroup);
while( $stmt->fetch() ){
	$data[] = array(
			'id' => $id,
			'value' => $NameGroup
		);
}
}
}
$stmt->close();

echo '{rows:'.json_encode($data).'}';

$db->close();
exit;

}



if(isset($_REQUEST['delete'])){
	$arrayId = $_REQUEST['arrayId'];
	$stringId = implode(",",$arrayId);
	
	$query = "DELETE FROM mc_cargo WHERE instance_id = ".$client_id." AND id IN (".$stringId.")";
	if( $stmt = $db->prepare($query) ){
	if( $stmt->execute() ){
	$stmt->close();
		echo json_encode(array('success'=>true, 'message'=>''));
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	}else{ echo json_encode(array('success'=>false, 'message'=>$db->errno . ' ' . $db->error)); }
	
	$db->close();
	exit;
}

?>