<?php
require('connect.php');
// echo ($_POST["data"]);
// $data = json_decode($_POST['data']);

$data["kefalh"] = json_encode(['elafra'=>$_POST["data"]['kefalh_elafra'], 'mesaia'=>$_POST["data"]['kefalh_mesaia'], 'entona'=>$_POST["data"]['kefalh_entona']]);
$data["koilia"] = json_encode(['khlides'=>$_POST["data"]['koilia_khlides'], 'stikta'=>$_POST["data"]['koilia_stikta'], 'diaxuta'=>$_POST["data"]['koilia_diaxuta']]);
$data["swma"] = json_encode(['khlides'=>$_POST["data"]['swma_khlides'], 'stikta'=>$_POST["data"]['swma_stikta'], 'diaxuta'=>$_POST["data"]['swma_diaxuta']]);
$data["bragxia"] = json_encode(['rodina'=>$_POST["data"]['vragxia_rodina'], 'faia'=>$_POST["data"]['vragxia_faia'], 'mebn'=>$_POST["data"]['vragxia_mebn'], 'aimatwmata'=>$_POST["data"]['vragxia_aimat'], 'kafe'=>$_POST["data"]['vragxia_kafe'],
                        'apoxrwm'=>$_POST["data"]['vragxia_apoxr'], 'vlenna'=>$_POST["data"]['vragxia_vlenna'], 'aim_vlenna'=>$_POST["data"]['vragxia_aim_vlenn']]);
$data["matia"] = json_encode(['lampera'=>$_POST["data"]['matia_lampera'], 'thola'=>$_POST["data"]['matia_thola'], 'leuka'=>$_POST["data"]['matia_leuka'], 'epoulwmena'=>$_POST["data"]['matia_epoulwmena'], 'tufla'=>$_POST["data"]['matia_tufla'],
                      'aimoragika'=>$_POST["data"]['matia_aimoragika']]);
$data["dusmorfies"] = json_encode(['stravo_stoma'=>$_POST["data"]['stravo_stoma'], 'eksogkoma_katw'=>$_POST["data"]['eksogkoma_katw'], 'proekt_katw'=>$_POST["data"]['proekt_katw'], 'paramorf_kefali'=>$_POST["data"]['paramorf_kefali'],
                           'katheto_kefali'=>$_POST["data"]['katheto_kefali'], 'papagalaki'=>$_POST["data"]['papagalaki'], 'vragxiokaluma'=>$_POST["data"]['vragxiokaluma'], 'raxitika'=>$_POST["data"]['raxitika'], 'lordwsh'=>$_POST["data"]['lordwsh'],
                           'kontoxontra'=>$_POST["data"]['kontoxontra'], 'klish_ouras'=>$_POST["data"]['klish_ouras']]);
$data["alloiwseis"] = json_encode(['fagwmenes_oures'=>$_POST["data"]['fagwmenes_oures'], 'plhges_gdarmena'=>$_POST["data"]['plhges_gdarmena']]);
$data["parasita"] = json_encode(['pseires'=>$_POST["data"]['pseires'], 'lamanthropus'=>$_POST["data"]['lamanthropus']]);
$temp = [];
$i = 1;
While(isset($_POST["data"]["par_kiv".$i])){
	array_push($temp, $_POST["data"]["par_kiv".$i]);
	$i++;
}
$data["pinakas_par"] = json_encode($temp);
$_POST['data']['hm_paralav'] = ($_POST['data']['hm_paralav2']==1 ? '' : $_POST['data']['hm_paralav']);
$_POST['data']['hm_lixpar'] = ($_POST['data']['no_lixpar']==1 ? '' : $_POST['data']['hm_lixpar']);
// echo print_r($data, true);
$fields = Array();
$deny = Array("id","flag", "rcheck", "edit", "synced", "approve_by", "isactive");
$fq ="SHOW COLUMNS FROM control_form";
$fres = mysqli_query($dbC, $fq);
while($frow = mysqli_fetch_assoc($fres)){
	if(!in_array($frow['Field'], $deny)){
		array_push($fields, $frow['Field']);
	}
	
}
$len = count($fields);
if($_POST['type']=='new' || $_POST['type']=='midsave'){
	$qry = "INSERT INTO control_form SET ";
	$qrye = "";
} else {
	$qry = "UPDATE control_form SET ";
	$qrye = " WHERE id=".$_POST['id'];
}
foreach($fields as $field){
	if(is_array($_POST['data'][$field])){
		$_POST['data'][$field] = json_encode($_POST['data'][$field]);
	}
	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)));
}
?>
