<?php 
session_start();
ob_start();
include_once("../dbconnect.php");

if($_GET['action']=="addcoupon")
{
	for($i=0;$i<count($_POST['customers']);$i++)
	{
		$getuemail=mysql_fetch_array(mysql_query("select Email from register where user_id=".$_POST['customers'][$i]));
	    $stringids.=$_POST['customers'][$i].",";
	    $string.=$getuemail[Email].",";
	}
	$get=substr($string,0,-1); 
    $getids=substr($stringids,0,-1);
	$insertcoupon=mysql_query("insert into coupons (`coupon_name`,`coupon_descr`,`coupon_amt`,`amttype`,`notimes`,`coupon_min_amt`,`coupon_code`,`coupon_start_date`,`coupon_end_date`,`coupon_users`) values ('".$_POST['coponname']."','".$_POST['coupondesc']."','".$_POST['couponamount']."','".$_POST['coupontype']."','".$_POST['usage']."','".$_POST['couponminamt']."','".$_POST['couponcode']."','".$_POST['couponstdate']."','".$_POST['couponenddate']."','".$getids."')");
	
	$last_id=mysql_insert_id();
	
	if($_POST['couponcode']=="")
	{
		$length = 10;
		$_string = ""; // start with a blank string
		$possible = "123456789bcdfghjkmnpqrstvwxyz"; // define possible characters
		$i = 0; // Counter
		// add random characters to $password until $length is reached
		while ($i < $length)
		{ 
			// pick a random character from the possible ones
			$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
			// we don't want this character if it's already in the password
			if (!strstr($_string, $char)) 
			{ 
			   $_string .= $char;
				$i++;
			}
		}
		$couponcode = $_string;
		$upcoupon=mysql_query("update coupons set coupon_code='".$couponcode."' where coupon_id=".$last_id);
	}
	if($_POST['coupontype']=="1"){
	$ram="%";
	}else{
		$ram="$";
	}
	$message.="Dear Customer
	Your account has been activated with a Discount Coupon Code ".$_POST['couponcode']."
	
    This Coupon code is valid for ".$_POST['usage']." Orders for an amount of ".$_POST['couponamount']." ".$ram."\n\n .
	
	*Note: Coupon Code is Valid only if the Order Amount exceeds".$_POST['couponminamt'].", and its valid from ".$_POST['couponstdate']." to ".$_POST['couponenddate']."
	".$emailsignature;
	$email_subject = "Coupon Code From bdazzledswim.com"; 
	$email_to = $get;
	$email_message = $message;
	$email_from=$fromemailaddress; 
    $headers = "From:   ".$email_from;
	mail($email_to,$email_subject,$email_message,$headers); 
	$_SESSION['sessionmessage'] = "Coupon Added Successfully";
	header("location: viewcoupons.php");
}
else if($_GET['action']=="update")
{
	
	for($i=0;$i<count($_POST['customers']);$i++)
	{
		$getuemail=mysql_fetch_array(mysql_query("select Email from register where user_id=".$_POST['customers'][$i]));
	    $stringids.=$_POST['customers'][$i].",";
	    $string.=$getuemail[Email].",";
	}
	$get=substr($string,0,-1); 
    $getids=substr($stringids,0,-1);
	if($_POST['couponcode']=="")
	{
		$length = 6;
		$_string = ""; // start with a blank string
		$possible = "123456789bcdfghjkmnpqrstvwxyz"; // define possible characters
		$i = 0; // Counter
		// add random characters to $password until $length is reached
		while ($i < $length)
		{ 
			// pick a random character from the possible ones
			$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
			// we don't want this character if it's already in the password
			if (!strstr($_string, $char)) 
			{ 
			   $_string .= $char;
				$i++;
			}
		}
		$couponcode = $_string; 
	}
	else {$couponcode=$_POST['couponcode']; }
	
	if($_POST['coupontype']=="1"){
	$ram1="%";
	}else{
		$ram1="$";
	}
	 $upcoupon=mysql_query("update coupons set coupon_name='".$_POST['coponname']."', coupon_descr='".$_POST['coupondesc']."', coupon_amt='".$_POST['couponamount']."', notimes='".$_POST['usage']."', coupon_min_amt='".$_POST['couponminamt']."', coupon_code='".$_POST['couponcode']."', coupon_code='".$couponcode."', coupon_start_date='".$_POST['couponstdate']."', coupon_end_date='".$_POST['couponenddate']."', coupon_users = '".$getids."', amttype = '".$_POST['coupontype']."' where coupon_id=".$_POST['coupon_id']);
	
	$message.="Dear Customer
	Your account has been activated with a Discount Coupon Code ".$_POST['couponcode']."
	
    This Coupon code is valid for ".$_POST['usage']." Orders for an amount of ".$_POST['couponamount']." ".$ram1."\n\n .
	
	*Note: Coupon Code is Valid only if the Order Amount exceeds".$_POST['couponminamt'].", and its valid from ".$_POST['couponstdate']." to ".$_POST['couponenddate']."
	".$emailsignature;
	
	$email_subject = "Coupon Code From bdazzledswim.com"; 
	
	$email_to = $get;
	$email_message = $message;
	$email_from=$fromemailaddress; 
    $headers = "From:   ".$email_from;
	mail($email_to,$email_subject,$email_message,$headers); 
	$_SESSION['sessionmessage'] = "Coupon Updated Successfully";
	header("location: viewcoupons.php?coupon_id=".$_POST['coupon_id']);
}
else if($_GET['action']=="delete")
{
	$deletecoupon=mysql_query("delete from coupons where coupon_id=".$_GET['coupon_id']);
	$_SESSION['sessionmessage'] = "Coupon Deleted Successfully";
	header("location: viewcoupons.php");
}
?>