<?
include('_functions2.php');
$JSON=json_decode($_POST['JSON'],true);
// $date_created = getToday();
	$order=($_POST['order']);
	$count=($_POST['count']);
	$dval=($_POST['dvalue']);
	$orderguid = getGUID();
	$orderid = addOrder($orderguid, $order, $dval, false);
	//echo '_'.$order.'_';

	$epoch = microtime(true);
	// file_put_contents("JSON/JSON_".$epoch.".json", json_encode($JSON,JSON_UNESCAPED_UNICODE));


$i = 0;
foreach($JSON as $item) {
	$id=$item[field_id];
	$first=($item['first']);
	$last=($item['last']);
	$email=($item['email']);
	$certificate=($item['certificate']);
	$del=($item['action_del']);
	$status=($item['status']);
	$dvalue=($item['dvalue']);

	$from=($item['from']);
	$message=($item['message']);
	$hasmessage=($item['hasmessage']);

	$tip=($item['tip']);
	$card=($item['card']);
	$esend=($item['esend']);

	$gift=($item['gift']);


	if ($status === 'success') {} else {
		if ($id === 'null') {$id=0;}
		if ($first === 'null') {$first='';}
		if ($last === 'null') {$last='';}
		if ($email === 'null') {$email='';}
		if ($certificate === 'null') {$certificate='';}

		if ($del === 'Y' && $id !== '0' && $id !== 0) {
			// $query = "DELETE  FROM profile_edu WHERE id='$id'";
			// mysql_query($query);
		} else if ($del === 'N' && ($id === '0' || $id === 0)) {

			// echo "$first $last $email $certificate \n";
			$guid = getGUID();
			// echo $order . ' X ' . $certificate . "\n";




			$theType = 99;
			if ($esend === 1 || $esend === '1') {
				if ($gift === 1 || $gift === '1') {
					if ($hasmessage === 1 || $hasmessage === '1') {
						$theType = 1;
					} else {
						$theType = 3;
						$from = '';
						$message = '';
					}
					
				} else {
					$theType = 0;
					$from = '';
					$message = '';
				}
			}


			addCertificateGroup( $guid, $order, $first, $last, $email, $certificate, $dvalue, $tip, $card, $esend, $gift, false, $theType, $from, $message, $epoch );




			
			if ($esend === 1 || $esend === '1') {
				if ($gift === 1 || $gift === '1') {
					if ($hasmessage === 1 || $hasmessage === '1') {
						$JSON[$i]['status'] = emailCustomer($guid, $email, $order, $certificate, $first, $last, 1, $from, $message, $dvalue, $tip, $card, true);
						$theType = 1;
					} else {
						$JSON[$i]['status'] = emailCustomer($guid, $email, $order, $certificate, $first, $last, 3, '', '', $dvalue, $tip, $card, true);
						$theType = 3;
						$from = '';
						$message = '';
					}
					
				} else {
					$JSON[$i]['status'] = emailCustomer($guid, $email, $order, $certificate, $first, $last, 0,'','', $dvalue, $tip, $card, true);
					$theType = 0;
					$from = '';
					$message = '';
				}

				if ($JSON[$i]['status'] === 'success') {} else {
					disableOrderByOrder($order);
					disableGroupByOrder($order);
					disableCertificateByOrder($order);
					cacheDataInitialize();
				}

			} else {
				$JSON[$i]['status'] = 'saved';
				// addCertificateGroup( $guid, $order, $first, $last, $email, $certificate, $dvalue, $tip, $card, $esend, $gift, false, $theType, $from, $message, $epoch );
			}

		} else {

		}
	}

	$i++;

}
$result = json_encode($JSON);
echo $result;
file_put_contents("JSON/JSON_".$epoch.".json", json_encode($JSON,JSON_UNESCAPED_UNICODE));
?>