<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php");
 validate_admin();
//error_reporting(E_ALL);
//ini_set('display_errors', '1');
//echo "<pre>";print_r($_REQUEST);die;
//phpinfo();
ini_set('max_input_vars', 3000000); 
ini_set('post_max_size', 3000000);
//echo phpinfo();die;
  $valid_for=$_POST['valid_for'];
  $discount_type=$_POST['discount_type'];
  $valid_time=$_POST['valid_time'];
  $discount=$_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']);
  $notification_type=mysqli_real_escape_string($GLOBALS['conn'],$_POST['notification_id']);
  $locationArr=$_POST['location'];
  
  $storeArr=$_POST['store_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);
  //$location='';
  $section = mysqli_real_escape_string($GLOBALS['conn'],$_POST['section_id']);
  

  $notification_title =mysqli_real_escape_string($GLOBALS['conn'],$_POST['notification_title']);
  $notification_text = mysqli_real_escape_string($GLOBALS['conn'],$_POST['notification_text']);
  
  
  $notification_frequency = mysqli_real_escape_string($GLOBALS['conn'],$_POST['notification_freqn']);

   
  require_once __DIR__ . '/../include/S3Helper.php';
  if($_REQUEST['submitForm']=='yes'){ 
    $s3Helper = new S3Helper();

    if($_FILES['photo']['size'] > 0 && ($_FILES['photo']['error'] == '' || $_FILES['photo']['error'] == 0)){
        $img = $_FILES['photo']['name'];

        $sizes = [
            'original' => null,        // no resize
            'thumb'    => [100, 80],   // resized to 100x80
            'tiny'     => [370, 223],  // resized to 370x223
        ];

        $img = $s3Helper->uploadImageToS3(
            $_FILES['photo'],               // file array
            $img,                           // filename
            'upload_images/notification',   // base folder in S3
            $sizes,                         // sizes config
            90                              // quality
        );

        $mgFIle = ",images='$img'";
        // echo $imageFile;
        // echo $img;
        // exit;
    }
	
	
  if($_REQUEST['id']==''){ 

	  $coupon_code=generateCouponCode();

	  $obj->query("insert into tbl_notifications  set notification_type='$notification_type',location='$location', 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,msg_title='$notification_title',msg_content='$notification_text',notification_frequency='$notification_frequency', store_id='$store' $mgFIle , section='$section' ");
	  $_SESSION['sess_msg']='Notification added sucessfully'; 

	  $notificationId = $obj->lastInsertedId();
	  
	 
	  
	  $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,notification_id='$notificationId' ,location='$location',store_id='$store'");
	  	  
      }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',location='$location',store_id='$store' where notification_id=".$_REQUEST['id']);	  
	  $obj->query("update tbl_notifications set notification_type='$notification_type',location='$location', 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', msg_title='$notification_title',msg_content='$notification_text' ,notification_frequency='$notification_frequency', section='$section', store_id='$store' $mgFIle where id=".$_REQUEST['id']);
	  $_SESSION['sess_msg']='Notification updated sucessfully';   
    }
	
   header("location:notification-list.php");
   exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from tbl_notifications where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);
$storeArr = explode(',',$result->store_id);
$locationArr = explode(',',$result->location);
//echo "<pre>";print_r($result);die;
}
$locationArr = array_filter($locationArr);

?>
<!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(obj.notification_id.value==''){
alert("Please select Notification Type.");
obj.notification_id.focus();
return false;	
}

if(obj.notification_freqn.value==''){
alert("Please select Notification Frequency.");
obj.notification_freqn.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;	
}

if(obj.notification_text.value==''){
alert("Please enter Notification text.");
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;
}
var expanded = false;

function showCheckboxes() {
  var checkboxes = document.getElementById("checkboxes");
  if (!expanded) {
    checkboxes.style.display = "block";
    expanded = true;
  } else {
    checkboxes.style.display = "none";
    expanded = false;
  }
}
function showByNotificationType(notificationId) {
 if(notificationId==3 || notificationId==4 || notificationId==5 ) {
	$('#location_area').show();
 }else {
	//$('#location_area').hide();
 }
}

</script>
<style>
	.multiselect {
  width: 250px;
  height: inherit;
}

.selectBox {
  position: relative;
}

.selectBox select {
  width: 100%;
  font-weight: bold;
}

.overSelect {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
}

#checkboxes {
  display: none;
  border: 1px #dadada solid;
}

#checkboxes label {
  display: block;
}

#checkboxes label:hover {
  background-color: #1e90ff;
}

</style>
<link href="css/jquery.multiselect.css" rel="stylesheet" />
</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: Add/Update Notifications 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Notifications"  class="button" onclick="location.href='notification-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">&nbsp;</td>
										<td width="82%" align="left" class="paddBot11"></td>
									</tr>
                                    <tr>
									<td align="right" class="paddBot11 paddRt14"><strong>Notification Type</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="notification_id" id="notification_id" style="width:250px;"  onchange="showByNotificationType(this.value);"  >

                                      <option value="">Select Notification</option>

                                      <?php $notificationType=getNotificationType();

									  foreach ($notificationType as $notifications) {

									  ?>

                                      <option value="<?php echo $notifications['id']; ?>"<?php if($notifications['id']==$result->notification_type){?>selected<?php } ?>><?php echo $notifications['name']; ?></option>

                                      <?php } ?>

                                      </select>
                                     </td>
									</tr>
									<tr>
									<td align="right" class="paddBot11 paddRt14"><strong>Notification Frequency</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="notification_freqn" id="notification_freqn" style="width:250px;" >

                                      <option value="">Select Frequency</option>

                                      <?php $notificationFreq=getNotificationFrequency();

foreach ($notificationFreq as $notificationFreqs) {

?>

<option value="<?php echo $notificationFreqs['id']; ?>"<?php if($notificationFreqs['id']==$result->notification_frequency){?>selected<?php } ?>><?php echo $notificationFreqs['name']; ?></option>

<?php } ?>

                                      </select>
                                     </td>
									</tr>
									<tr>
									<td align="right" class="paddBot11 paddRt14"><strong>Section</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <select name="section_id" id="section_id"   >

                                      <option value="">Select Section</option>
                                       
                                      <?php $SectionLists=getAllSection();

									  foreach ($SectionLists as $SectionList) {

									  ?>

                                      <option value="<?php echo $SectionList['section_name']; ?>"<?php if($SectionList['section_name']==$result->section){?>selected<?php } ?>><?php echo $SectionList['section_name']; ?></option>

                                      <?php } ?>
<option value="Shef">Shef</option>
                                      </select>
                                     </td>
									</tr>S
									<tr>
									<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) {

									  ?>

                                      <option value="<?php echo $storeList['storeid']; ?>"<?php if(in_array($storeList['storeid'],$storeArr)){?>selected<?php } ?>><?php echo $storeList['storename']; ?></option>

                                      <?php } ?>

                                      </select>
                                     </td>
									</tr>
									<tr <?php if(empty($locationArr)){ ?>style="display:none;"<?php } ?> id="location_area">
									<td align="right" class="paddBot11 paddRt14"><strong>Location</strong></td>
                                    <td width="100%" align="left" class="paddBot11">
                                    <select name="location[]" id="location"  multiple="multiple" >

                                      <option value="">Select Location</option>

									  <?php 
								   $locations = getAllCities();
								   $i=0;
								   foreach ($locations as $location) { 
								   if($i < 50000) {?>									 
                                      <option value="<?php echo $location['city'];?>"<?php if(in_array($location['city'],$locationArr)) { ?> selected <?php }?>><?php echo $location['city'];?></option>
                                      <?php 
									  
								   }
								   $i++;
								   } ?>

                                      </select>
                                     </td>
									</tr>
									<!--<tr>
									<td align="right" class="paddBot11 paddRt14"><strong>Location</strong></td>
                                    <td width="82%" align="left" class="paddBot11">
                                    <div class="multiselect">
									<div class="selectBox" onclick="showCheckboxes()">
									<select >
										<option>Select Location</option>
									</select>
									<div class="overSelect"></div>
									</div>
									<div id="checkboxes">
                                   <?php 
								   //$locations = getAllCities();
								   foreach ($locations as $location) { ?>

									<label for="one">
										<input type="checkbox" name="location[]" value="<?php echo $location['city'];?>" id="<?php echo $location['city'];?>" <?php if(in_array($location['city'],$locationArr)) { echo 'checked="checked"';}?> /><?php echo $location['city'];?></label>
										<?php } ?>
									</div>
								</div>
                                     </td>
									</tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Nationwise:</strong></td>
									  <td align="left" class="paddBot11"><input name="nation_wise"  type="radio" value="All" checked="checked"  /> Yes&nbsp;
									  <input name="nation_wise"  type="radio" value="App" checked="checked"  /> No&nbsp;
									  

                                    </td>
							  </tr>-->
									<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="All" checked="checked"  onclick="$('#email_area').hide();"/> App Users&nbsp;
									  

                                    <input name="valid_for"  type="radio" value="Particular" <?php if($result->valid_for=='Particular'){ ?>checked<?php } ?> onclick="$('#email_area').show();"/> Particular User</td>
							  </tr>
									
									<tr <?php if($result->valid_for!='Particular'){ ?>style="display:none;"<?php } ?> 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>
                              <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'){ ?>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);?>" /> $ 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);?>" /> $</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="paddBot11 paddRt14"><strong>Image: (Size: 1280pxX540px)</strong></td>
									  <td align="left" class="paddBot11">
										  <input name="photo" id="photo" type="file"  /><br/>
                                  
                                      </td>
							       </tr> 
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Notification Title :</strong></td>
									  <td align="left" class="paddBot11"><input name="notification_title" type="text" id="notification_title" size="50" value="<?php echo stripslashes($result->msg_title);?>" /></td>
							  </tr>
							  <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Notification Text :</strong></td>
									  <td align="left" class="paddBot11"><textarea name="notification_text" rows="5" cols="40" id="notification_text"><?php echo stripslashes($result->msg_content);?></textarea></td>
							  </tr>
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;Note: Use {COUPON_CODE} to include coupon code in message text</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>


