<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php");
 validate_admin();

  if($_REQUEST['submitForm']=='yes'){
  $url=$_POST['url'];
  $status=$_POST['status'];
  $email_type=$_POST['email_type'];
 if($_FILES['photo']['size']>0 && $_FILES['photo']['error']==''){
  $photo='offer'.time().substr($_FILES['photo']['name'],-5);
      //ftp start
	if(SITE_URL==MAINSITE_URL){
$conn_id = ftp_connect("3.133.214.148") or die("Error in FTP connection");  // the FTP server you want to connect to.
$login_status = ftp_login($conn_id, "mainuser@quicklly.com", "n3At8hv3LZKCiF6") or die("Error in FTP login");  // Login to the FTP server.

//to upload from local
//ftp_pasv($conn_id, true);
//to create dir
//ftp_mkdir($conn_id, "imageststs");
  $target_file = $_FILES['photo']['tmp_name'];
  $destination_file = "upload_images/email-offer/".$photo;  // since you want to upload to a directory called "images".
$upload_status = ftp_put($conn_id, $destination_file, $target_file, FTP_BINARY);

if (!$upload_status)
{
   die("Something went wrong, unable to upload featured image, contact to site administrator."); // do whatever it is that you want to do when you are unable to upload the file.
}

// Close the FTP connection after you are done.
ftp_close($conn_id);
}else{
}
  //ftp ends

  move_uploaded_file($_FILES['photo']['tmp_name'],"../upload_images/email-offer/".$photo);
  }
 	   
  if($_REQUEST['id']==''){
	  $obj->query("insert into tbl_email_offer set image='$photo',url='$url',status='$status',email_type=$email_type");
	  $_SESSION['sess_msg']='Email Offer added successfully';  
	  
       }else{ 	 
       $sql=" update tbl_email_offer set url='$url',status='$status',email_type='$email_type'"; 
	   if($photo){
		$imageArr=$obj->query("select image from tbl_email_offer where id='".$_REQUEST['id']."' "); 
		$resultImage=$obj->fetchNextObject($imageArr);
		@unlink("../upload_images/email-offer/".$resultImage->image); 
		$sql.=" ,image='$photo' ";
	   }
	  $sql.=" where id='".$_REQUEST['id']."'";
	   $obj->query($sql);
	  $_SESSION['sess_msg']='Email Offer updated successfully';   
        }
   header("location:email-offer-list.php");
   exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from tbl_email_offer where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);
}
	
?>
<!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=utf-8" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
if(obj.name.value==''){
alert("Please enter Heading");
obj.name.focus();
return false;
}
if(obj.url.value==''){
alert("Please enter url");
obj.url.focus();
return false;
}


}
</script>
<script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>

<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:<?php if($_REQUEST['id']==''){?> Add<?php } else{?>Update<?php } ?>	Email Offer 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View  Email Offer"  class="button" onclick="location.href='email-offer-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 align="right" class="paddBot11 paddRt14"><strong> Heading:</strong></td>
									<td align="left" class="paddBot11">
									<input type="text" name="name" id="name" size="150" value="<?php echo stripslashes($result->name);?>">
									</tr>-->
                   <tr>
									<td align="right" class="paddBot11 paddRt14"><strong> Banner link:</strong></td>
									<td align="left" class="paddBot11">
									<input type="text" name="url" value="<?php echo $result->url;?>" style="width: 350px;">
									</tr>
									 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Offer Banner:</strong></td>
									  <td align="left" class="paddBot11"><input name="photo" type="file" style="width: 350px;"  /><br/>
                                      <?php if(is_file("../upload_images/email-offer/".$result->image)) {?>
                                      <img src="../upload_images/email-offer/<?php echo $result->image; ?>" width="100" height="50" />
                                      <?php } ?>
                                      </td>
							       </tr>
							       <tr>
									<td align="right" class="paddBot11 paddRt14"><strong> Status:</strong></td>
									<td align="left" class="paddBot11">
									<select name="status" required style="width: 350px;">
									<option value=""> Select Status</option>
									<option value="1" <?php if($result->status=='1'){?>selected<?php }?>>Active</option>
									<option value="0" <?php if($result->status=='0'){?>selected<?php }?>>Deactive</option>
									</select>
									</tr>
									 <tr>
									<td align="right" class="paddBot11 paddRt14"><strong> Email Type:</strong></td>
									<td align="left" class="paddBot11">
									<select name="email_type" required style="width: 350px;">
									<option value=""> Select Email Type</option>
									<option value="1" <?php if($result->email_type=='1'){?>selected<?php }?>>Quicklly Email</option>
									<option value="2" <?php if($result->email_type=='2'){?>selected<?php }?>>Marketing Email</option>
									</select>
									</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>