<?php
/*
* date last modified 10-14-2018
* HTML Files - add_coupon.html.php
* Mysql Tables - tbl_discount_coupon
*/
require_once('includes/functions.php');
$debug = true;
// add new app
if(isset($_POST['add'])) {
	$discount = $discountAmount = 0;
	
	if(isset($_REQUEST['expiry']) && strlen($_REQUEST['expiry'])>0) {
		$expiryDate = trim($_REQUEST['expiry']);
		//$expiryAr = explode('-',$expiry);
		//$expiryDate = $expiryAr[2].'-'.$expiryAr[1].'-'.$expiryAr[0];
	} else {
		$error = 'Select Expiry Date';
	}
	
	if(isset($_REQUEST['coupon_type']) && $_REQUEST['coupon_type']!=''){
		$coupon_type = trim($_REQUEST['coupon_type']);
	} else {
		$error = 'Enter Coupon Type';
	}
	
	if(isset($_REQUEST['discount_amount']) && $_REQUEST['discount_amount']!=''){
		$discountAmount = trim($_REQUEST['discount_amount']);
	}
	
	if(isset($_REQUEST['discount']) && $_REQUEST['discount']!=''){
		$discount = trim($_REQUEST['discount']);
	} 
	
	if(isset($_REQUEST['discount_type']) && $_REQUEST['discount_type']!=''){
		$discount_type = trim($_REQUEST['discount_type']);
		if($discount_type == 'percentage' && ($_REQUEST['discount']=='' || $_REQUEST['discount']<=0)){
			$error = 'Please enter discount percentage';
		} else if($discount_type == 'flat' && ($_REQUEST['discount_amount']=='' || $_REQUEST['discount_amount']<=0)){
			$error = 'Please enter discount amount';
		}
	} else {
		$error = 'Please select discount type';
	}
	
	if(isset($_REQUEST['code']) && $_REQUEST['code']!=''){
		$code = trim($_REQUEST['code']);
	} else {
		$error = 'Enter Coupon Code';
	}
	


	if(empty($error)) {						
		// update business
		$insert = "INSERT INTO tbl_discount_coupon (`code`, `discount_type`, `discount_amount`, `coupon_type`, discount, `end_date`, status) values 
		('".$code."', '".$discount_type."', '".$discountAmount."', '".$coupon_type."', '".$discount."', '".$expiryDate."', 'Disabled')";
		
		$execInsert = mysqli_query($con, $insert);				
		if($execInsert) {
			$success = 'Coupon Code Added Successfully';
			/*
			header('Location:index.php?page=manage_coupons&success='.$success);
			exit;*/
		?>
		<script>
			window.location='index.php?page=manage_coupons&success=<?php echo($success); ?>';
		</script>
	<?php
		}				
	} else {
		//$error = "You are not allowed to add business under a static page menu."; 
	}
}

// edit
if(isset($_GET['id']) && $_GET['id']!='') {
	$editId = $_GET['id'];
	$editRec = mysqli_fetch_array(mysqli_query($con, 'SELECT * FROM tbl_discount_coupon WHERE id='.$editId));
	$editAction = true;
	//$edExpiry = explode('-',$resEdit['end_date']);
	//$editExpiryDate = $edExpiry[2].'-'.$edExpiry[1].'-'.$edExpiry[0];
}

// update
if(isset($_POST['update'])) {
		
	$discount = $discountAmount = 0;
	
	if(isset($_REQUEST['expiry']) && strlen($_REQUEST['expiry'])>0) {
		$expiryDate = trim($_REQUEST['expiry']);
		//$expiryAr = explode('-',$expiry);
		//$expiryDate = $expiryAr[2].'-'.$expiryAr[1].'-'.$expiryAr[0];
	} else {
		$error = 'Select Expiry Date';
	}
	
	if(isset($_REQUEST['coupon_type']) && $_REQUEST['coupon_type']!=''){
		$coupon_type = trim($_REQUEST['coupon_type']);
	} else {
		$error = 'Enter Coupon Type';
	}
	
	if(isset($_REQUEST['discount_amount']) && $_REQUEST['discount_amount']!=''){
		$discountAmount = trim($_REQUEST['discount_amount']);
	}
	
	if(isset($_REQUEST['discount']) && $_REQUEST['discount']!=''){
		$discount = trim($_REQUEST['discount']);
	} 
	
	if(isset($_REQUEST['discount_type']) && $_REQUEST['discount_type']!=''){
		$discount_type = trim($_REQUEST['discount_type']);
		if($discount_type == 'percentage' && ($_REQUEST['discount']=='' || $_REQUEST['discount']<=0)){
			$error = 'Please enter discount percentage';
		} else if($discount_type == 'flat' && ($_REQUEST['discount_amount']=='' || $_REQUEST['discount_amount']<=0)){
			$error = 'Please enter discount amount';
		}
	} else {
		$error = 'Please select discount type';
	}
	
	if(isset($_REQUEST['code']) && $_REQUEST['code']!=''){
		$code = trim($_REQUEST['code']);
	} else {
		$error = 'Enter Coupon Code';
	}
	
	if(empty($error)) {						
		// update business
		$insert = "UPDATE tbl_discount_coupon SET code = '".mysqli_real_escape_string($con, $code)."',	
												  discount_type='".mysqli_real_escape_string($con, $discount_type)."',
												  discount_amount='".mysqli_real_escape_string($con, $discountAmount)."',
												  coupon_type='".mysqli_real_escape_string($con, $coupon_type)."',
												  discount='".mysqli_real_escape_string($con, $discount)."',
												  end_date ='".$expiryDate."' WHERE id=".$editId;
		$execInsert = mysqli_query($con, $insert);
		if($execInsert) {
			$success = 'Coupon Code Updated Successfully';
			/*header('Location:index.php?page=manage_coupons&success='.$success);
			exit;*/
			?>
			<script>
				window.location='index.php?page=manage_coupons&success=<?php echo($success); ?>';
			</script>
				<?php
		}				
	} else {
		//$error = "You are not allowed to add business under a static page menu."; 
	}
}
?>