<?php
ini_set("default_charset", "UTF-8");
header('Content-type: text/html; charset=UTF-8');
require('connect.php');

if(isset($_POST['type']) && $_POST['type']=='edit'){
	$qry = "UPDATE production SET ";
	$qrye = " WHERE id=".$_POST['id'];
} else {
	$qry = "INSERT INTO production SET ";
	$qrye = "";
}
$temp = [];
$i = 1;
While(isset($_POST["data"]["par_kiv".$i])){
	array_push($temp, $_POST["data"]["par_kiv".$i]);
	$i++;
}
$data["varh_proiontwn"] = json_encode($temp);
$fields = Array();
$deny = Array("id","flag", "approve_by", "synced");
$fq ="SHOW COLUMNS FROM production";
$fres = mysqli_query($dbC, $fq);
while($frow = mysqli_fetch_assoc($fres)){
	if(!in_array($frow['Field'], $deny)){
		array_push($fields, $frow['Field']);
	}
	
}
// print_r($fields);

$len = count($fields);
foreach($fields as $field){
	if(is_array($_POST['data'][$field])){
		$_POST['data'][$field] = json_encode($_POST['data'][$field], JSON_UNESCAPED_UNICODE);
	}
	if($field!=$fields[$len-1]){
		if(isset($_POST['data'][$field])){
			$qry.=$field."='".$_POST['data'][$field]."',";
		} else {
			$qry.=$field."='".$data[$field]."',";
		}

	} else {
		if(isset($_POST['data'][$field])){
			$qry.=$field."='".$_POST['data'][$field]."'";
		} else {
			$qry.=$field."='".$data[$field]."'";
		}
	}
}
$qry.=$qrye;
// echo $qry;
if(mysqli_query($dbC, $qry)){
	echo json_encode(Array("status"=>1, "id"=>mysqli_insert_id($dbC)));
} else {
	echo json_encode(Array("status"=>-1, "error"=>mysqli_error($dbC), "qry"=>$qry));
}


?>
