<?php
session_start();
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php");
  require_once __DIR__ . '/../include/S3Helper.php';
   
   $s3 = new S3Helper();

 validate_admin();
  $title=mysqli_real_escape_string($GLOBALS['conn'],$_POST['title']);
  $target_url=mysqli_real_escape_string($GLOBALS['conn'],$_POST['target_url']);
  $description=mysqli_real_escape_string($GLOBALS['conn'],$_POST['description']);
  $cat_id=mysqli_real_escape_string($GLOBALS['conn'],$_POST['cat_id']);
  $show_on_home=mysqli_real_escape_string($GLOBALS['conn'],$_POST['show_on_home']);
  $catAr=getCategoryArray($cat_id,$array='');
  if(count($catAr)>0){
	$bcategories=implode(",",$catAr);  
  }
  if($_REQUEST['submitForm']=='yes'){
      if($_FILES['photo']['size']>0 && $_FILES['photo']['error']==''){
	  $Image=new SimpleImage();
	  $img=time().$_FILES['photo']['name'];
	  $img = $s3->uploadImageToS3(
                     $_FILES['photo'],
                     $img,
                     'upload_images/banner/',
                     [
                        'original' => null,
                        'thumb' => [`160`, 50],
                     ]
                  );
// 	  move_uploaded_file($_FILES['photo']['tmp_name'],"../upload_images/banner/".$img);
// 	  copy("../upload_images/banner/".$img,"../upload_images/banner/thumb/".$img);
// 	  $Image->load("../upload_images/banner/thumb/".$img);
// 	  $Image->resize(160,50);
// 	  $Image->save("../upload_images/banner/thumb/".$img);
	  }  
  
  if($_REQUEST['id']==''){
	   $obj->query("insert into $tbl_banner set title='$title',cat_id='$cat_id',bcategories='$bcategories',target_url='$target_url',show_on_home='$show_on_home',description='$description',photo='$img',status=1 ");
	   $_SESSION['sess_msg']='Banner added sucessfully';  
	  
       }else{ 
	   $sql="update $tbl_banner set  title='$title',cat_id='$cat_id',bcategories='$bcategories',show_on_home='$show_on_home',target_url='$target_url',description='$description',status=1 ";
	   if($img){
	   $imageArr=$obj->query("select photo from $tbl_banner where id=".$_REQUEST['id']);
	   $resultImage=$obj->fetchNextObject($imageArr);
	   @unlink("../upload_images/banner/".$resultImage->photo);
	   @unlink("../upload_images/banner/thumb/".$resultImage->photo);
	    $sql.=" , photo='$img' ";
	   }
	  $sql.=" where id='".$_REQUEST['id']."'";
	
	   $obj->query($sql);
	   $_SESSION['sess_msg']='Banner updated sucessfully';   
        }
   header("location:banner-list.php");
   exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from $tbl_banner 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)
{

<?php if($_REQUEST['id']==''){ ?>
if(obj.photo.value==''){
alert("Please select image");
obj.photo.focus();
return false;
}
<?php } ?>

}
</script>

</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php") ?>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
$('#category').autocomplete({
	search: function () {},
    source: function (request, response)
    {
        $.ajax(
        {
            url:"getCategoryTree.php" ,
            dataType: "json",
            data:
            {
                q: request.term,
            },
            success: function (data)
            {
                response(data);
            }
        });
    },
    minLength: 2,
   select: function( event, ui ) {
		$( "#category" ).val( ui.item.value );
		$( "#hdnId" ).val( ui.item.key );
        
		var cat_id=$( "#hdnId" ).val();	
		//alert(cat_id);
		return false;
	  }
});
	  
 })
</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 Banner<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Banners"  class="button" onclick="location.href='banner-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>Category :</strong></td>
									  <td align="left" class="paddBot11">
                                      <input type="text" name="category"  id="category" style="width:500px;"  value="<?php echo getCategoryTree($result->cat_id,$array='');?>" />
                                      <input type="hidden" name="cat_id" id="hdnId" value="<?php echo $_REQUEST['cat_id'];?>" />                    
                                    </td>
							  </tr> 
									<tr>
									  <td align="right" class="paddRt14 paddBot11"><strong>Title :</strong></td>
									  <td align="left" class="paddBot11">
                                      <input name="title"  size="36" id="title" value="<?php echo stripslashes($result->title);?>" >
                                      </td>
							  </tr> 
								<tr>
									  <td align="right" class="paddRt14 paddBot11"><strong>Short Description :</strong></td>
									  <td align="left" class="paddBot11">
                                      <input name="description"  size="36" id="description" value="<?php echo stripslashes($result->description);?>" >
                                      </td>
							  </tr> 	
					    <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Banner Image(742px X 262px):<br/>
                                      </strong></td>
									  <td align="left" class="paddBot11"><input type="file" name="photo"  /><br/>
									  
									  <img src="<?php echo SITE_URL_CDN ?>/upload_images/banner/thumb/<?php echo $result->photo; ?>"  /></td>
							  </tr>
									
									<tr>
									  <td align="right" class="paddRt14 paddBot11"><strong>Traget URL :</strong></td>
									  <td align="left" class="paddBot11"><textarea name="target_url" rows="8" cols="40" id="target_url"><?php echo stripslashes($result->target_url);?></textarea></td>
							  </tr>
                              <tr>
									  <td align="right" class="paddRt14 paddBot11"><strong>Show On Home:</strong></td>
									  <td align="left" class="paddBot11"><input type="checkbox" name="show_on_home"  value="1" <?php if($result->show_on_home==1){?>checked<?php } ?>></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>
