<?php
ini_set('post_max_size','80M');
require('connectremote.php');
require('sendMail.php');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

$control = $_POST['control'];
// echo print_r($control, true);
$cerr = 0;
$perr = 0;
$crep = "";
$cqry = "";
$prep = "";
$pqry = "";
$ids = "";
// if(isset($control) && is_array($control)){
// 	foreach($control as $qry){
if (isset($_POST['control'])) {
    	preg_match("/apodekth='(\d+)'/", $control, $matches);
		if(isset($matches[1]) && $matches[1]==0){
			preg_match("/id='(\d+)'/", $control, $id);
			$ids.=($ids=="" ? $id[1] : ", ".$id[1]);
		}
    	if(!mysqli_query($dbC, $control)){
    	    $cerr++;
    	    $crep = mysqli_error($dbC);
    	    $cqry = $qry;
    	}
}
	
// 	}
// }


if(isset($_POST['production'])){
	$production = $_POST['production'];
	foreach($production as $qry){
	    if(!mysqli_query($dbC, $qry)){
	        $perr++;
	        $prep = mysqli_error($dbC);
	        $pqry = $qry;
	    }
	}
}

$approved = "";
$apr = mysqli_query($dbC, "SELECT id FROM control_form WHERE flag=1");
while($row = mysqli_fetch_assoc($apr)){
	if($approved==""){
	    $approved.=$row['id'];
	} else {
	    $approved.=", ".$row['id'];
	}
}



echo json_encode(Array("cerror"=>$cerr, "crep"=>$crep , "csample"=>$cqry,"perror"=>$perr, "prep"=>$prep, "psample"=>$pqry, "mail"=>($ids!="" ? send($ids) : ""), "approved"=>$approved));

?>
