<?php include('../includes/head.php'); ?>
<?php
	if(isset($_GET['cid'])){
		$id = $_GET['cid'];
		$getCollectionData = mysql_query("select * from campaign where id='$id'");
		$getCount = mysql_num_rows($getCollectionData);
		if($getCount>0){
			$cData = mysql_fetch_array($getCollectionData);
		}
	}else{
		header("location:campaign.php");
	}
?>
<?php
	if(isset($_POST['submitBasicData'])){
		$camId = $_POST['camId'];
		$corder = $_POST['corder'];
		$cname = mysql_real_escape_string($_POST['cname']);
		
		$updateCollectionData = mysql_query("UPDATE `campaign` SET `name` = '$cname', `norder` = '$corder' WHERE `campaign`.`id` = '$camId';");
		if($updateCollectionData){
			echo "<script>alert('Campaign Updated');window.location.href ='edit-campaign.php?cid=$camId';</script>";
		}else{
			echo "<script>alert('Failed');window.location.href ='edit-campaign.php?cid=$camId';</script>";
		}
	}
?>

<?php
	if(isset($_POST['submitThumb'])){
		$camId = $_POST['camId'];
		$thumbImg = $_FILES['thumb']['name'];		
		
		$thumbPic = $_POST['uploadedThumbPhoto'];
		
		
			if(!empty($_FILES['thumb']['name'])){
				
				$uploaddir = '../../campaign/'.$camId.'/thumb/';
				$uploadfile = $uploaddir . basename($_FILES['thumb']['name']);
				
				if (move_uploaded_file($_FILES['thumb']['tmp_name'], $uploadfile)) {
					unlink('../../campaign/'.$camId.'/thumb/'.$thumbPic.'');
					$updateThumb = mysql_query("UPDATE `campaign` SET `thumb` =  '$thumbImg' WHERE `id` = '$camId';");
					echo "<script>alert('Thumb Image Updated');window.location.href ='edit-campaign.php?cid=$camId';</script>";
					//echo "File is valid, and was successfully uploaded.\n";
					//echo '<br/>';
				}else{
					echo "<script>alert('Failed');window.location.href ='edit-campaign.php?cid=$camId';</script>";
					//echo "Possible file upload attack!\n";
					//echo '<br/>';
				}
			}
	}
?>

<?php
	if(isset($_POST['updateGallery'])){
		$camId = $_POST['camId'];
		
			if(!empty($_FILES['gallery']['name'])){
				
				$uploadSize = sizeof($_FILES['gallery']['name']);
				
				if (!file_exists('../../campaign/'.$camId.'/gallery')) {
					mkdir('../../campaign/'.$camId.'/gallery', 0777, true);
				}
				
				for($i=0;$i<$uploadSize;$i++){
					
					$imgName = $_FILES['gallery']['name'][$i];
					
					// Insert into DB
					$insertIntoDB = mysql_query("INSERT INTO `campaign_gallery` (`id`, `cid`, `path`, `date_of_added`) VALUES (NULL, '$camId', '$imgName', now());");
					
					$uploaddir2 = '../../campaign/'.$camId.'/gallery/';
					$uploadfile2 = $uploaddir2 . basename($_FILES['gallery']['name'][$i]);
					
					if (move_uploaded_file($_FILES['gallery']['tmp_name'][$i], $uploadfile2)) {
						echo "<script>alert('Gallery Images Updated');window.location.href ='edit-campaign.php?cid=$camId';</script>";
					}
				}
			}
	}
?>
<?php
	if(isset($_GET['galleryDelId'])){
		$delId = $_GET['galleryDelId'];
		$fileName = $_GET['fileName'];
		$cid = $_GET['cid'];
		
		$getQuery = mysql_query("DELETE FROM `campaign_gallery` WHERE `id` = '$delId'");
		if($getQuery){
			unlink('../../campaign/'.$cid.'/gallery/'.$fileName.'');
			echo "<script>alert('Gallery Images Deleted');window.location.href ='edit-campaign.php?cid=$cid';</script>";
		}
	}
?>
<script src="ckeditor/ckeditor.js"></script>
<style>
	h3{
		font: normal 14px/20px Philosopher;
	}
	.collectionForm .col-sm-12{
		padding:0;
	}
</style>
</head>
<body>
	<?php include('../includes/header.php'); ?>
	<!-- ******CONTACT****** --> 
    <section id="product">
        <div class="container">
            <div class="col-sm-12">
				<h2 style="font-family:Philosopher; font-size:24px;">Campaign</h2>
				<form action="edit-campaign.php?cid=<?php echo $id; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Name: </h3>
					<input type="text" name="cname" class="form-control" required placeholder="Campaign Name" value="<?php echo $cData['name']; ?>">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Campaign Order: </h3>
					<input type="text" name="corder" class="form-control" required placeholder="Campaign Order" value="<?php echo $cData['norder']; ?>">
				</div>
				<div class="col-sm-12">
				<br/>
				</div>
				<div class="col-sm-12">
					<input type="submit" name="submitBasicData" class="btn btn-primary btn-lg" value="Update" style="margin-top:15px;">
					<input type="hidden" value="<?php echo $id; ?>" name="camId">
				</div>
				</form>
			</div>
			
			
				<form action="edit-campaign.php?cid=<?php echo $id; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
					<div class="col-sm-12">
						<h2 style="font-family:Philosopher; font-size:24px;">Upload Thumb</h2>
							<table class="table table-bordered">
								<tr>
									<td style="text-align:center;"><img src="../../campaign/<?php echo $id; ?>/thumb/<?php echo $cData['thumb']; ?>" style="width:100px;float:none;margin:0 auto;" ></td>
									<td>
										<h3 style="font-family:Philosopher;">Upload Thumb Image</h3>
										<input type="file" name="thumb" class="form-control" required> 
										<input type="submit" name="submitThumb" class="btn btn-primary" value="Upload" style="margin-top:15px;">
										<input type="hidden" value="<?php echo $id; ?>" name="camId">
										<input type="hidden" value="<?php echo $cData['thumb']; ?>" name="uploadedThumbPhoto">
									</td>
								</tr>
							</table>
					</div>
				</form>
				
				
				<form action="edit-campaign.php?cid=<?php echo $id; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
						<h2 style="font-family:Philosopher; font-size:24px;">Upload Gallery</h2>
							<?php
								$galleryList='';
								$getGallery = mysql_query("select * from campaign_gallery where cid='$id'");
								$getCount = mysql_num_rows($getGallery);
								if($getCount>0){
									while($galleryRows = mysql_fetch_array($getGallery)){
										$cgid = $galleryRows['id'];
										$path = $galleryRows['path'];
										
										$galleryList.='<tr>
															<td><img src="../../campaign/'.$id.'/gallery/'.$path.'" style="width:50px;"></td>
															<td><a href="edit-campaign.php?galleryDelId='.$cgid.'&fileName='.$path.'&cid='.$id.'">Delete</a></td>
														</tr>';
									}
								}else{
									$galleryList.='No Phtots found';
								}
							?>
							
							<table class="table table-bordered">
								<tr>
									<th>Gallery Image</th>
									<th>Action</th>
								</tr>
								<?php echo $galleryList; ?>
							</table>
					<div class="col-sm-12">
						<h3 style="color:#000;">Add Gallery/Product Images: </h3>
						<input type="file" name="gallery[]" class="form-control" multiple required /> 
					</div>
					<div class="col-sm-12">
						<input type="submit" name="updateGallery" class="btn btn-primary" value="Update" style="margin-top:15px;">
						<input type="hidden" value="<?php echo $id; ?>" name="camId">
					</div>
				</form>
				
			
        </div><!--//container-->
    </section><!--//contact-->
<?php include('../includes/footerjs.php'); ?>
</body>
</html>