<?php include('../includes/head.php'); ?>
<?php
	if(isset($_POST['submit'])){
		$cname = mysql_real_escape_string($_POST['cname']);
		$corder = $_POST['corder'];
		$desc = mysql_real_escape_string($_POST['desc']);
		
		$thumb = $_FILES['thumb']['name'];
		$logo = $_FILES['logo']['name'];
		$cover = $_FILES['cover']['name'];
		
		$addCollection = mysql_query("INSERT INTO `collection` (`cid`, `name`, `thumb`, `logo`, `cover`, `description`, `corder`, `date_of_added`) VALUES (NULL, '$cname', '$thumb', '$logo', '$cover', '$desc', '$corder', now());");
		
		if($addCollection){
			$cId = mysql_insert_id();
			
			if (!file_exists('../../collections/'.$cId.'')) {
				mkdir('../../collections/'.$cId.'', 0777, true);
			}
			
			if (!file_exists('../../collections/'.$cId.'/cover')) {
				mkdir('../../collections/'.$cId.'/cover', 0777, true);
			}
			
			if (!file_exists('../../collections/'.$cId.'/thumb')) {
				mkdir('../../collections/'.$cId.'/thumb', 0777, true);
			}
			
			if (!file_exists('../../collections/'.$cId.'/logo')) {
				mkdir('../../collections/'.$cId.'/logo', 0777, true);
			}
			
			
			$uploaddir = '../../collections/'.$cId.'/cover/';
			$uploadfile = $uploaddir . basename($_FILES['cover']['name']);
			
			if (move_uploaded_file($_FILES['cover']['tmp_name'], $uploadfile)) {
				echo "File is valid, and was successfully uploaded.\n";
				echo '<br/>';
			} else {
				echo "Possible file upload attack!\n";
				echo '<br/>';
			}
			
			
			$uploaddir1 = '../../collections/'.$cId.'/thumb/';
			$uploadfile1 = $uploaddir1 . basename($_FILES['thumb']['name']);
			
			if (move_uploaded_file($_FILES['thumb']['tmp_name'], $uploadfile1)) {
				echo "File is valid, and was successfully uploaded.\n";
				echo '<br/>';
			} else {
				echo "Possible file upload attack!\n";
				echo '<br/>';
			}
			
			$uploaddir2 = '../../collections/'.$cId.'/logo/';
			$uploadfile2 = $uploaddir2 . basename($_FILES['logo']['name']);
			
			if (move_uploaded_file($_FILES['logo']['tmp_name'], $uploadfile2)) {
				echo "File is valid, and was successfully uploaded.\n";
				echo '<br/>';
			} else {
				echo "Possible file upload attack!\n";
				echo '<br/>';
			}
			
			
			// Add Gallery
			
			//print_r($_FILES);
			$uploadSize = sizeof($_FILES['gallery']['name']);
			//echo $_FILES['gallery']['name'][0];
			
			if (!file_exists('../../collections/'.$cId.'/gallery')) {
				mkdir('../../collections/'.$cId.'/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, '$cId', '$imgName', now());");
				
				$uploaddir3 = '../../collections/'.$cId.'/gallery/';
				$uploadfile3 = $uploaddir3 . basename($_FILES['gallery']['name'][$i]);
				
				if (move_uploaded_file($_FILES['gallery']['tmp_name'][$i], $uploadfile3)) {
					echo "File is valid, and was successfully uploaded.\n";
					echo '<br/>';
				}
			}
			
			//echo "<script>alert('Property Added');window.location.href ='property-list.php';</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;">Add Collection</h2>
				<form action="add-collection.php" 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">
				</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">
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Thumb Image: (600px X 503px)</h3>
					<input type="file" name="thumb" class="form-control" required> 
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Upload Logo: (250px X 176px)</h3>
					<input type="file" name="logo" class="form-control" required> 
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Description: </h3>
					<textarea name="desc" id="desc" required ></textarea>
						<script type="text/javascript">
							CKEDITOR.replace('desc');
						</script> 
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Cover Image: </h3>
					<input type="file" name="cover" class="form-control" required /> 
				</div>
				<div class="col-sm-12">
					<h3 style="color:#000;">Gallery/Product Images: </h3>
					<input type="file" name="gallery[]" class="form-control" multiple required /> 
				</div>
				<div class="col-sm-12">
				<br/>
				</div>
				<div class="col-sm-12">
					<input type="submit" name="submit" class="btn btn-primary btn-lg">
				</div>
				<div class="col-sm-12">
				<br/>
				</div>
				</form>
			</div>
        </div><!--//container-->
    </section><!--//contact-->
<?php include('../includes/footerjs.php'); ?>
</body>
</html>