<?php
require("connect.php");
if(isset($_POST['approve'])){
	if(!isset($_POST['control'])){
		$qry = "UPDATE production SET pros_emporio=IF(pros_emporio=1, 0, 1), approve_by='".$_POST['user']."' WHERE id=".$_POST['id'];
	} else{
	    if(isset($_POST['n']) && $_POST['n']==4){
		    $qry = "UPDATE control_form4 SET flag=IF(flag=1, 0, 1), approve_by='".$_POST['user']."' WHERE id=".$_POST['id'];
	    } else {
	        $qry = "UPDATE control_form SET flag=IF(flag=1, 0, 1), approve_by='".$_POST['user']."' WHERE id=".$_POST['id'];
	    }
	}

} else {
	$next = mysqli_fetch_array(mysqli_query($dbC, "SELECT MAX(orders_id) FROM orders"));
	$next = ($next[0] == '' || $next[0] == NULL) ? 0 : $next[0];
	if($_POST['type']==0){
		date_default_timezone_set('Europe/Athens');
		$qry = "INSERT INTO orders (orders_id, customers_id, order_total, date_purchased) VALUES ( ".(++$next).", ".$_POST['user'].", ".$_POST['price'].", '".date('Y-m-d h:i:s')."')";
	} else if($_POST['type']==1) {
		$qry = "INSERT INTO orders_products (orders_id, products_id, products_model, products_name, products_price, final_price, products_quantity) VALUES ('".++$next."','".$_POST['id']."','".$_POST['model']."','".$_POST['application']."','".$_POST['price']."','".$_POST['total']."','".$_POST['qty']."')";
	}
}


if(mysqli_query($dbC, $qry)){
	// echo "OK";
} else {
	echo mysqli_error($dbC);
}

?>
