<?php include('../includes/head.php'); ?>
<?php
	if(isset($_GET['cid'])){
		$cid = $_GET['cid'];
		$getCollectionData = mysql_query("select * from collection where cid='$cid'");
		$getCount = mysql_num_rows($getCollectionData);
		if($getCount>0){
			$cData = mysql_fetch_array($getCollectionData);
		}
	}else{
		header("location:index.php");
	}
?>
<?php
	if(isset($_POST['updateGallery'])){
		$collId = $_POST['collId'];
		
			if(!empty($_FILES['gallery']['name'])){
				
				$uploadSize = sizeof($_FILES['gallery']['name']);
				
				if (!file_exists('../../collections/'.$collId.'/gallery')) {
					mkdir('../../collections/'.$collId.'/gallery', 0777, true);
				}
				
				for($i=0;$i<$uploadSize;$i++){
					
					$imgName = $_FILES['gallery']['name'][$i];
					
					// Insert into DB
					$insertIntoDB = mysql_query("INSERT INTO `gallery` (`id`, `cid`, `path`, `date_of_added`) VALUES (NULL, '$collId', '$imgName', now());");
					
					$uploaddir2 = '../../collections/'.$collId.'/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-collection.php?cid=$collId';</script>";
					}
				}
			}
	}
?>

<?php
	if(isset($_GET['galleryDelId'])){
		$delId = $_GET['galleryDelId'];
		$fileName = $_GET['fileName'];
		$cid = $_GET['cid'];
		
		$getQuery = mysql_query("DELETE FROM `gallery` WHERE `id` = '$delId'");
		if($getQuery){
			unlink('../../collections/'.$cid.'/gallery/'.$fileName.'');
			echo "<script>alert('Gallery Images Deleted');window.location.href ='edit-collection.php?cid=$cid';</script>";
		}
	}
?>
<?php
	if(isset($_POST['submitCover'])){
		$collId = $_POST['collId'];
		$coverImg = $_FILES['cover']['name'];		
		
		$coverPic = $_POST['uploadedCoverPhoto'];
		
		
			if(!empty($_FILES['cover']['name'])){
				
				$uploaddir = '../../collections/'.$collId.'/cover/';
				$uploadfile = $uploaddir . basename($_FILES['cover']['name']);
				
				if (move_uploaded_file($_FILES['cover']['tmp_name'], $uploadfile)) {
					unlink('../../collections/'.$collId.'/cover/'.$coverPic.'');
					$updateThumb = mysql_query("UPDATE `collection` SET `cover` =  '$coverImg' WHERE `cid` = '$collId';");
					echo "<script>alert('Cover Image Updated');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "File is valid, and was successfully uploaded.\n";
					//echo '<br/>';
				}else{
					echo "<script>alert('Failed');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "Possible file upload attack!\n";
					//echo '<br/>';
				}
			}
	}
?>
<?php
	if(isset($_POST['submitLogo'])){
		$collId = $_POST['collId'];
		$logoImg = $_FILES['logo']['name'];		
		
		$logoPic = $_POST['uploadedLogoPhoto'];
		
		
			if(!empty($_FILES['logo']['name'])){
				
				$uploaddir = '../../collections/'.$collId.'/logo/';
				$uploadfile = $uploaddir . basename($_FILES['logo']['name']);
				
				if (move_uploaded_file($_FILES['logo']['tmp_name'], $uploadfile)) {
					unlink('../../collections/'.$collId.'/logo/'.$logoPic.'');
					$updateThumb = mysql_query("UPDATE `collection` SET `logo` =  '$logoImg' WHERE `cid` = '$collId';");
					echo "<script>alert('Logo Image Updated');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "File is valid, and was successfully uploaded.\n";
					//echo '<br/>';
				}else{
					echo "<script>alert('Failed');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "Possible file upload attack!\n";
					//echo '<br/>';
				}
			}
	}
?>
<?php
	if(isset($_POST['submitThumb'])){
		$collId = $_POST['collId'];
		$thumbImg = $_FILES['thumb']['name'];		
		
		$thumbPic = $_POST['uploadedThumbPhoto'];
		
		
			if(!empty($_FILES['thumb']['name'])){
				
				$uploaddir = '../../collections/'.$collId.'/thumb/';
				$uploadfile = $uploaddir . basename($_FILES['thumb']['name']);
				
				if (move_uploaded_file($_FILES['thumb']['tmp_name'], $uploadfile)) {
					unlink('../../collections/'.$collId.'/thumb/'.$thumbPic.'');
					$updateThumb = mysql_query("UPDATE `collection` SET `thumb` =  '$thumbImg' WHERE `cid` = '$collId';");
					echo "<script>alert('Thumb Image Updated');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "File is valid, and was successfully uploaded.\n";
					//echo '<br/>';
				}else{
					echo "<script>alert('Failed');window.location.href ='edit-collection.php?cid=$collId';</script>";
					//echo "Possible file upload attack!\n";
					//echo '<br/>';
				}
			}
	}
?>
<?php
	if(isset($_POST['submitBasicData'])){
		$collId = $_POST['collId'];
		$corder = $_POST['corder'];
		$cname = mysql_real_escape_string($_POST['cname']);
		echo $description = mysql_real_escape_string($_POST['desc']);
		
		$updateCollectionData = mysql_query("UPDATE `collection` SET `name` = '$cname', `description` = '$description', `corder` = '$corder' WHERE `cid` = '$collId';");
		if($updateCollectionData){
			echo "<script>alert('Collection Updated');window.location.href ='edit-collection.php?cid=$collId';</script>";
		}else{
			echo "<script>alert('Failed');window.location.href ='edit-collection.php?cid=$collId';</script>";
		}
	}
?>
<script src="ckeditor/ckeditor.js"></script>
<style>
	h3{
		font: normal 14px/20px Philosopher;
	}
	.collectionForm .col-sm-12{
		padding:0;
	}
	.collectionForm h3{
		    margin: 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;">Edit Collection</h2>
				
				<form action="edit-collection.php?cid=<?php echo $cid; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
					<div class="col-sm-12">
						<h3 style="color:#000;">Collection Name: </h3>
						<input type="text" name="cname" class="form-control" required placeholder="Collection Name" value="<?php echo $cData['name']; ?>">
					</div>
					<div class="col-sm-12">
						<h3 style="color:#000;">Collection Order: </h3>
						<input type="text" name="corder" class="form-control" required placeholder="Collection Order" value="<?php echo $cData['corder']; ?>">
					</div>
					<div class="col-sm-12">
						<h3 style="color:#000;">Description: </h3>
						<textarea name="desc" id="desc" required ><?php echo $cData['description']; ?></textarea>
							<script type="text/javascript">
								CKEDITOR.replace('desc');
							</script> 
					</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 $cid; ?>" name="collId">
					</div>
				</form>
				
				
				<form action="edit-collection.php?cid=<?php echo $cid; ?>" 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="../../collections/<?php echo $cid; ?>/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 $cid; ?>" name="collId">
										<input type="hidden" value="<?php echo $cData['thumb']; ?>" name="uploadedThumbPhoto">
									</td>
								</tr>
							</table>
					</div>
				</form>
				
				<form action="edit-collection.php?cid=<?php echo $cid; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
					<div class="col-sm-12">
						<h2 style="font-family:Philosopher; font-size:24px;">Upload Logo</h2>
						
						<table class="table table-bordered">
							<tr>
								<td style="text-align:center;"><img src="../../collections/<?php echo $cid; ?>/logo/<?php echo $cData['logo']; ?>" style="width:100px;float:none;margin:0 auto;background:#e2e2e2;" ></td>
								<td>
									<h3 style="font-family:Philosopher;">Upload logo Image</h3>
									<input type="file" name="logo" class="form-control" required>
									<input type="submit" name="submitLogo" class="btn btn-primary" value="Upload" style="margin-top:15px;">
									<input type="hidden" value="<?php echo $cid; ?>" name="collId">
									<input type="hidden" value="<?php echo $cData['logo']; ?>" name="uploadedLogoPhoto">
								</td>
							</tr>
						</table>
					</div>
				</form>
				
				<form action="edit-collection.php?cid=<?php echo $cid; ?>" method="post" enctype="multipart/form-data" class="collectionForm">
					<div class="col-sm-12">
						<h2 style="font-family:Philosopher; font-size:24px;">Upload Cover</h2>
						<table class="table table-bordered">
							<tr>
								<td style="text-align:center;"><img src="../../collections/<?php echo $cid; ?>/cover/<?php echo $cData['cover']; ?>" style="width:100px;float:none;margin:0 auto;" ></td>
								<td>
									<h3 style="font-family:Philosopher;">Upload Cover Image</h3>
									<input type="file" name="cover" class="form-control" required />
									<input type="submit" name="submitCover" class="btn btn-primary" value="Upload" style="margin-top:15px;">
									<input type="hidden" value="<?php echo $cid; ?>" name="collId">
									<input type="hidden" value="<?php echo $cData['cover']; ?>" name="uploadedCoverPhoto">
								</td>
							</tr>
						</table>
					</div>
				</form>
				
				<form action="edit-collection.php?cid=<?php echo $cid; ?>" 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 gallery where cid='$cid'");
								$getCount = mysql_num_rows($getGallery);
								if($getCount>0){
									while($galleryRows = mysql_fetch_array($getGallery)){
										$id = $galleryRows['id'];
										$path = $galleryRows['path'];
										
										$galleryList.='<tr>
															<td><img src="../../collections/'.$cid.'/gallery/'.$path.'" style="width:50px;"></td>
															<td><a href="edit-collection.php?galleryDelId='.$id.'&fileName='.$path.'&cid='.$cid.'">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 $cid; ?>" name="collId">
					</div>
				</form>
				<div class="col-sm-12">
				<br/>
				</div>
				</form>
			</div>
        </div><!--//container-->
    </section><!--//contact-->
<?php include('../includes/footerjs.php'); ?>
</body>
</html>