<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
 validate_admin();
  $valid_for=mysqli_real_escape_string($GLOBALS['conn'],$_POST['valid_for']);
  $discount_type=mysqli_real_escape_string($GLOBALS['conn'],$_POST['discount_type']);
  $valid_time=mysqli_real_escape_string($GLOBALS['conn'],$_POST['valid_time']);
  $discount=mysqli_real_escape_string($GLOBALS['conn'],$_POST['discount']);
  $to_email=mysqli_real_escape_string($GLOBALS['conn'],$_POST['to_email']);
  $expire_date=mysqli_real_escape_string($GLOBALS['conn'],$_POST['expire_date']);
  $minimum_purchase=mysqli_real_escape_string($GLOBALS['conn'],$_POST['minimum_purchase']);
  $coupon_code_type=mysqli_real_escape_string($GLOBALS['conn'],$_POST['coupon_code_type']);
  $coupon_type=mysqli_real_escape_string($GLOBALS['conn'],$_POST['general']);
  $applicable_for=mysqli_real_escape_string($GLOBALS['conn'],$_POST['applicable_for']);
  $source_id=mysqli_real_escape_string($GLOBALS['conn'],$_POST['source_id']);
  //echo  "<pre>";print_r($_POST);die;
  
  $locationArr=$_POST['location'];
  
  $storeArr=$_POST['store_id'];
  $sectionArr=$_POST['section_id'];
  foreach ($storeArr as $storeArrs) {
	  if($storeArrs==260 ) {
		array_push($storeArr,279);  
	  }
	  if($storeArrs==279 ) {
		array_push($storeArr,260);  
	  }
	  if($storeArrs==194) {
		array_push($storeArr,195);  
	  }
	  if($storeArrs==195) {
		array_push($storeArr,194);  
	  }
	  if($storeArrs==270) {
		array_push($storeArr,271);  
	  }
	  if($storeArrs==271) {
		array_push($storeArr,270);  
	  }
	  if($storeArrs==245) {
		array_push($storeArr,277);  
	  }
	  if($storeArrs==277) {
		array_push($storeArr,245);  
	  }
  }
  //echo  "<pre>";print_r($storeArr);die;
  //array_push($a,"blue","yellow");
  $store = implode(',',$storeArr);
  $store= trim($store,",");
  $location = implode(',',$locationArr);
  $section =  implode(',',$sectionArr);
  $section= trim($section,",");
  
  $custom_coupon_code= mysqli_real_escape_string($GLOBALS['conn'],$_POST['custom_coupon_code']);
 
   $chkSrcSql=$obj->query("SELECT id,name FROM coupon_source ");

   //echo "select count(coupon_code) as cnt_no from $tbl_coupon where coupon_code='$custom_coupon_code'";die;
  if($_REQUEST['submitForm']=='yes'){
	  //echo "select count(coupon_code) as cnt_no from $tbl_coupon where coupon_code='$custom_coupon_code'";die;
  $chkSql=$obj->query("select count(coupon_code) as cnt_no from $tbl_coupon where coupon_code='$custom_coupon_code' and  coupon_code!=''");
  $chkResult=$obj->fetchNextObject($chkSql);	
//echo "<pre>";print_r($chkResult);

//die;  
  /*if($chkResult->cnt_no == 0) {
	$_SESSION['sess_msg']='Coupon code already exist!'; 
    header("location:coupon-addf_val.php");
    exit();	
  }*/
		  if($_REQUEST['id']=='')
		  {
			  if($chkResult->cnt_no == 0) 
			  {  
					  if($coupon_code_type=='custom_c') {
						 $coupon_code=mysqli_real_escape_string($GLOBALS['conn'],$_POST['custom_coupon_code']);;
					  }else {
						 $coupon_code=generateCouponCode();  
					  }
					  
					  if($_POST['general']=='specific') {
					  $specific_coupon = mysqli_real_escape_string($GLOBALS['conn'],$_POST['general']);
					  }
					  
					 
					  
					  
					  $obj->query("insert into $tbl_coupon set valid_for='$valid_for',discount_type='$discount_type',valid_time='$valid_time',discount='$discount',to_email='$to_email',expire_date='$expire_date',coupon_code='$coupon_code',generate_date=now(),minimum_purchase='$minimum_purchase',status=1,store_id='$store',section='$section',location='$location',coupon_code_type ='$coupon_code_type',coupon_type='$coupon_type',
					  applicable_on='$applicable_for',coupon_source='$source_id'");
					  
					   if(!empty($specific_coupon)) {
						  $notificationId = $obj->lastInsertedId();	  
						  $obj->query("update tbl_coupon set notification_id ='$notificationId' where id=$notificationId");
					  }	  
								  
					  $_SESSION['sess_msg']='Coupon added sucessfully';
                      header("location:coupon-list.php");
		              exit();					  
			  }else
			  {
					  $_SESSION['sess_msg']='Coupon code already exist!'; 
			  }			  
			  
		  }else 
		  { 
			  $obj->query("update $tbl_coupon set valid_for='$valid_for',discount_type='$discount_type',valid_time='$valid_time',discount='$discount',to_email='$to_email',expire_date='$expire_date',minimum_purchase='$minimum_purchase' ,store_id='$store',section='$section',location='$location',coupon_code_type ='$coupon_code_type' ,coupon_type='$coupon_type' ,applicable_on='$applicable_for' 
,coupon_source='$source_id'
			  where id=".$_REQUEST['id']);
			  $_SESSION['sess_msg']='Coupon updated sucessfully';
              header("location:coupon-list.php");
		      exit();			  
		  }
		   //header("location:coupon-list.php");
		   //exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from $tbl_coupon where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);
$storeArr = explode(',',$result->store_id);
$sectionArr = explode(',',$result->section);
}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
if(getRadioValue('valid_for')=='Particular' && !obj.to_email.value.match(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/) ){
alert("Please enter valid email.");
	obj.to_email.focus();
	return false;
	
}

if(getRadioValue('general')=='specific'){
	if(obj.section_id.value==''){
	alert("Please select category.");
	obj.section_id.focus();
	return false;	
    }
	if(obj.store_id.value==''){
	alert("Please choose store.");
	obj.store_id.focus();
	return false;	
    }
}


if(getRadioValue('coupon_code_type')=='custom_c'){
	if(obj.custom_coupon_code.value==''){
	alert("Please enter custom coupon code.");
	obj.custom_coupon_code.focus();
	return false;	
    }
}

if(obj.valid_time.value==''){
alert("Please enter Number of Uses.");
obj.valid_time.focus();
return false;	
}

if(obj.discount.value==''){
alert("Please enter discount.");
obj.discount.focus();
return false;	
}

if(obj.minimum_purchase.value==''){
alert("Please enter minimum purchase amount.");
obj.minimum_purchase.focus();
return false;	
}


if(obj.expire_date.value==''){
alert("Please select expire date.");
obj.expire_date.focus();
return false;	
}

}
function getRadioValue(groupName) {
    var _result;
    try {
        var o_radio_group = document.getElementsByName(groupName);
        for (var a = 0; a < o_radio_group.length; a++) {
            if (o_radio_group[a].checked) {
                _result = o_radio_group[a].value;
                break;
            }
        }
    } catch (e) { }
    return _result;
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<link rel="stylesheet" href="calender/css/jquery-ui.css">

	<script src="calender/js/jquery-ui.js"></script>
	
	<script>
	$(function() {
		$( "#expire_date" ).datepicker({
			changeMonth: true,
			changeYear: true,
			dateFormat:"yy-mm-dd",
			yearRange:'2014:<?php echo date('Y')+5; ?>'
		});
	});
	</script>
<tr>
	<td align="right" class="paddRtLt70" valign="top">
		<table width="99%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			
				<td align="right" valign="top">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td align="left" valign="middle" class="headingbg bodr text14">
					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Update Coupon 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Coupons"  class="button" onclick="location.href='coupon-list.php'" /></span></td>
						</tr>
						
						<tr>
							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
                            <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
									<tr>
										<td width="18%" align="right" class="paddBot11 paddRt14">Coupon Source:</td>
										<td width="82%" align="left" class="paddBot11">
									<select name="source_id" id="source_id" >
                                      <option value="">Select Source</option>
                                      <?php 									  
									 while($lineSrc=$obj->fetchNextObject($chkSrcSql)){
									  ?>
                                      <option value="<?php echo $lineSrc->id; ?>"  <?php if($result->coupon_source==$lineSrc->id or $_POST['source_id']==$lineSrc->id){ ?> selected <?php } ?>><?php echo $lineSrc->name; ?></option>
                                      <?php } ?>
                                      </select>
										</td> 
									</tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Coupon Type:</strong></td>
									  <td align="left" class="paddBot11">
									  <input name="general"  type="radio" value="general" checked="checked"  onclick="$('#category_cp').hide();$('#store_cp').hide();"  /> General&nbsp;
									  

                                    <input name="general"  type="radio" value="specific" <?php if($result->coupon_type=='specific' or $_POST['general']=='specific'){ ?>checked<?php } ?> onclick="$('#category_cp').show();$('#store_cp').show();" />Specific</td>
							  </tr>
								<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Coupon Code Type:</strong></td>
									  <td align="left" class="paddBot11">
                                      <input name="coupon_code_type"  type="radio" value="random_c" checked="checked" onclick="$('#custom_coupon').hide();" <?php if($result->coupon_code_type=='random_c' or $_POST['coupon_code_type']=='random_c'){ ?>checked<?php } ?> />Automatic&nbsp;
                                      <input name="coupon_code_type"  type="radio" value="custom_c" <?php if($result->coupon_code_type=='custom_c' or $_POST['coupon_code_type']=='custom_c'){ ?>checked<?php } ?>  onclick="$('#custom_coupon').show();"/> Custom</td>
							  </tr>	
							  <?php  if(($result->coupon_code_type=='custom_c') or ($_POST['coupon_code_type']=='custom_c')) { ?>
							<tr  id="custom_coupon">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Custom Coupon Code:</strong></td> 
									  <td align="left" class="paddBot11">
									  
									  <input name="custom_coupon_code" type="text" id="custom_coupon_code" size="36" 
									  value="<?php  if(!empty($_POST['custom_coupon_code'])) {echo $_POST['custom_coupon_code']; }else{ echo $result->coupon_code ; }?>" 
									  <?php if($_REQUEST['id']!=''){ ?> disabled <?php } ?> />
									  
									  </td>
							  </tr>	
							  <?php } else {?>
							  <tr  id="custom_coupon" style="display:none;" >
                                      <td align="right" class="paddBot11 paddRt14"  ><strong>Custom Coupon Code:</strong></td> 
									  <td align="left" class="paddBot11">
									  
									  <input name="custom_coupon_code" type="text" id="custom_coupon_code" size="36" 
									  value="<?php  if(!empty($_POST['custom_coupon_code'])) {echo $_POST['custom_coupon_code']; }else{ echo $result->coupon_code ; }?>" 
									  <?php if($_REQUEST['id']!=''){ ?> disabled <?php } ?> />
									  
									  </td>
							  </tr>	
							  <?php } ?>
							  
									<?php
									$specificDisp = "none";
                                    if($result->coupon_type=='specific') {
										
									
									?>
									
									
									<tr id="category_cp" >
									<td align="right" class="paddBot11 paddRt14"><strong>Category</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="section_id[]" id="section_id"  multiple="multiple" >

                                      <option value="">Select Category</option>

                                      <?php $SectionLists=getAllSection();

									  foreach ($SectionLists as $SectionList) {

									  ?>

                                      <option value="<?php echo $SectionList['section_name']; ?>"<?php if(in_array($SectionList['section_name'],$sectionArr)){?>selected<?php } ?>> <?php echo $SectionList['section_name']; ?></option>

                                      <?php } ?>

                                      </select>
                                     </td>
									</tr>
									<tr id="store_cp"  >
									<td align="right" class="paddBot11 paddRt14"><strong>Store</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="store_id[]" id="store_id"  multiple="multiple" >

                                      <option value="">Select Store</option>

                                      <?php $storeLists=getAllStore();
									  

									  foreach ($storeLists as $storeList) {
                                      if($storeList['storeid'] !=309) {
									  ?>

                                      <option value="<?php echo $storeList['storeid']; ?>"<?php if(in_array($storeList['storeid'],$storeArr)){?>selected<?php } ?>><?php echo $storeList['storename']; ?></option>

                                      <?php }} ?>

                                      </select>
                                     </td>
									</tr>
									<?php }else { ?>
									<tr id="category_cp"  style="display:none;">
									<td align="right" class="paddBot11 paddRt14"><strong>Category</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="section_id[]" id="section_id" multiple="multiple"  >

                                      <option value="">Select Category</option>

                                      <?php $SectionLists=getAllSection();

									  foreach ($SectionLists as $SectionList) {
                                      
									  ?>

                                      <option value="<?php echo $SectionList['section_name']; ?>"<?php if(($result->section==$SectionList['section_name']) or ($SectionList['section_name']==$_POST['section_id'])){?>selected<?php } ?>><?php echo $SectionList['section_name']; ?></option>

                                      <?php } ?>

                                      </select>
                                     </td>
									</tr>
									<tr id="store_cp" style="display:none;" >
									<td align="right" class="paddBot11 paddRt14"><strong>Store</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="store_id[]" id="store_id"  multiple="multiple" >

                                      <option value="">Select Store</option>

                                      <?php $storeLists=getAllStore();
									  

									  foreach ($storeLists as $storeList) {
 if($storeList['storeid'] !=309) {
									  ?>

                                      <option value="<?php echo $storeList['storeid']; ?>"<?php if(in_array($storeList['storeid'],$storeArr)){?>selected<?php } ?>><?php echo $storeList['storename']; ?></option>

                                      <?php } }?>

                                      </select>
                                     </td>
									</tr>
									<?php } ?>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Valid For:</strong></td>
									  <td align="left" class="paddBot11"><input name="valid_for"  type="radio" value="All" checked="checked"  onclick="$('#email_area').hide();"/> ALL Users&nbsp;
									  <input name="valid_for"  type="radio" value="APP_FIRST_TIME_USER" <?php if($result->valid_for=='APP_FIRST_TIME_USER' or $_POST['valid_for']=='APP_FIRST_TIME_USER'){ ?>checked<?php } ?>   onclick="$('#email_area').hide();"/> App First Time Users&nbsp;
									  <input name="valid_for"  type="radio" value="All_APP" <?php if($result->valid_for=='All_APP' or $_POST['valid_for']=='All_APP'){ ?>checked<?php } ?>   onclick="$('#email_area').hide();"/> App Users&nbsp;
									  <input name="valid_for"  type="radio" value="WEB" <?php if($result->valid_for=='WEB' or $_POST['valid_for']=='WEB'){ ?>checked<?php } ?>   onclick="$('#email_area').hide();"/> Web Users&nbsp;
									  <input name="valid_for"  type="radio" value="WEB_FIRST_TIME_USER" <?php if($result->valid_for=='WEB_FIRST_TIME_USER' or $_POST['valid_for']=='WEB_FIRST_TIME_USER'){ ?>checked<?php } ?>   onclick="$('#email_area').hide();"/> Web First Time Users&nbsp;
 
                                    <input name="valid_for"  type="radio" value="Particular" <?php if($result->valid_for=='Particular' or $_POST['valid_for']=='Particular'){ ?>checked<?php } ?> onclick="$('#email_area').show();"/> Particular User</td>
							  </tr>
									<?php
									
                                    if($result->valid_for=='Particular') {
										
									
									?> 
									<tr  id="email_area">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Email:</strong></td>
									  <td align="left" class="paddBot11"><input name="to_email" type="text" id="to_email" size="36" value="<?php echo stripslashes($result->to_email);?>" /></td>
							  </tr>
									<?php }else { ?>
									<tr style="display:none;" id="email_area">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Email:</strong></td>
									  <td align="left" class="paddBot11"><input name="to_email" type="text" id="to_email" size="36" value="<?php echo stripslashes($result->to_email);?>" /></td>
							  </tr>
									<?php } ?>
									
							<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Applicable For:</strong></td>
									  <td align="left" class="paddBot11">
									  <input name="applicable_for"  type="radio" value="delivery_pickup" checked="checked"/>Delivery Or Pickup &nbsp;
                                      <input name="applicable_for"  type="radio" value="delivery" <?php if($result->applicable_on=='delivery' or $_POST['applicable_for']=='delivery'){ ?>checked<?php } ?>/>Delivery&nbsp;
                                      <input name="applicable_for"  type="radio" value="pickup" <?php if($result->applicable_on=='pickup' or $_POST['applicable_for']=='pickup'){ ?>checked<?php } ?> /> Pickup</td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Discount Type:</strong></td>
									  <td align="left" class="paddBot11">
                                      <input name="discount_type"  type="radio" value="Percent" checked="checked"/>Percent&nbsp;
                                      <input name="discount_type"  type="radio" value="Direct" <?php if($result->discount_type=='Direct' or $_POST['discount_type']=='Direct'){ ?>checked<?php } ?> /> Direct</td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>No. of Uses:</strong></td>
									  <td align="left" class="paddBot11">
                                      <input name="valid_time" type="text" id="valid_time" size="36" value="<?php echo stripslashes($result->valid_time);?>" /> Times</td>
							  </tr>
										<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Discount:</strong></td>
									  <td align="left" class="paddBot11"><input name="discount" type="text" id="discount" size="36" value="<?php echo stripslashes($result->discount);?>" /> INR or %</td>
							  </tr>
                              	<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Minimum Purchase:</strong></td>
									  <td align="left" class="paddBot11"><input name="minimum_purchase" type="text" id="minimum_purchase" size="36" value="<?php echo stripslashes($result->minimum_purchase);?>" /> INR</td>
							  </tr>
                              	<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Valid Till:</strong></td>
									  <td align="left" class="paddBot11"><input name="expire_date" type="text" id="expire_date" size="36" value="<?php if($result->expire_date!='0000-00-00'){ echo stripslashes($result->expire_date);}?>" /></td>
							  </tr>
									
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="18%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" /></td>
									</tr>
								</table></form>
							</td>
						</tr>
						
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?php include('footer.php'); ?>
</table>
</body>
</html>


