<script src="jquery.js"></script>
<?php
ini_set('post_max_size','120M');
require("connect.php");

$control = Array();
$production = Array();

$cfields = Array();
$cres = mysqli_query($dbC, "SHOW COLUMNS FROM control_form");
while($crow = mysqli_fetch_assoc($cres)){
	array_push($cfields, $crow['Field']);
}

$pfields = Array();
$pres = mysqli_query($dbC, "SHOW COLUMNS FROM production");
while($prow = mysqli_fetch_assoc($pres)){
	array_push($pfields, $prow['Field']);
}

$cqry = "INSERT IGNORE INTO control_form SET ";
$dres = mysqli_query($dbC, "SELECT * FROM control_form WHERE synced=0");
while($drow = mysqli_fetch_assoc($dres)){
	foreach($cfields as $field){
		if($field==$cfields[count($cfields)-1]){
			$cqry .= $field."='".$drow[$field]."'";
		} else {
			$cqry .= $field."='".$drow[$field]."',";
		}
	}
	array_push($control,$cqry);
	$cqry = "INSERT IGNORE INTO control_form SET ";
}

$pqry = "INSERT IGNORE INTO production SET ";
$dres = mysqli_query($dbC, "SELECT * FROM production WHERE synced=0");
while($drow = mysqli_fetch_assoc($dres)){
	foreach($pfields as $field){
		if($field==$pfields[count($pfields)-1]){
			$pqry .= $field."='".$drow[$field]."'";
		} else {
			$pqry .= $field."='".$drow[$field]."',";
		}
	}
	array_push($production, $pqry);
	$pqry = "INSERT IGNORE INTO production SET ";

}
print_r($control);
?>
<script>
// $(document).ready(function(){
// 	document.write('<b>Sync in Progress..</b>');
// 	$.ajax({
// 		type:"POST",
// 		url: "http://dynacompqc.eu/dataget.php",
// 		crossDomain: true,
// 		data: {control: <?php echo json_encode($control); ?>, production: <?php echo json_encode($production); ?>},
// 		success: function(r){
// 			res = JSON.parse(r);

// 			document.write('<b>Sync Result:</b><br /><br />');
// 			if(res.cerror==0 && res.perror==0){
// 				document.write('<b style="color:green;">Sync Complete.</b><br /><br />');
// 			} else {
// 				document.write('<b style="color:red;">Sync Fail.</b><br /><br />');
// 			}
// 			if(res.cerror>0){
// 				document.write("<b>Control errors:</b> "+res.cerror+"<br />");
// 				document.write("<b>Control error:</b> "+res.crep+"<br />");
// 				document.write("<b>Control sample query:</b> "+res.csample+"<br />");
// 			} else {
// 				$.ajax({
// 					type: "POST",
// 					url: "updatesync.php",
// 					data: {type:"control"},
// 					success: function(res){
// 						if(res==1){
// 							document.write("Control Table Updated.<br />");
// 						} else {
// 							document.write("Control Update Error: "+res+"<br />");
// 						}
// 					}
// 				});
// 			}
// 			if(res.perror>0){
// 				document.write("<b>Production errors:</b> "+res.perror+"<br />");
// 				document.write("<b>Production error:</b> "+res.prep+"<br />");
// 				document.write("<b>Production sample query:</b> "+res.psample+"<br />");
// 			} else {
// 				$.ajax({
// 					type: "POST",
// 					url: "updatesync.php",
// 					data: {type:"production"},
// 					success: function(res){
// 						if(res==1){
// 							document.write("Production Table Updated.<br />");
// 						} else {
// 							document.write("Production Update Error: "+res+"<br />");
// 						}
// 					}
// 				});
// 			}
// 		}
// 	});
// });

</script>
