<?php include_once('../includes/head.php'); ?>
<?php include_once('includes/admin_auth.php'); ?>
<?php
	$get_productlist = mysql_query("select * from collection");
	$product_list='';
	$get_count = mysql_num_rows($get_productlist);
	if($get_count>0){
		while($row = mysql_fetch_array($get_productlist)){
			$id = $row['cid'];
			$cname = $row['name'];
			$thumb = $row['thumb'];
			$cover = $row['cover'];
			$logo = $row['logo'];
			$thumb = $row['thumb'];
			$order = $row['corder'];
			$date = $row['date_of_added'];
			
			$product_list.='<tr>
								<th><img src="../../collections/'.$id.'/thumb/'.$thumb.'" class="img-responsive" style="width:60px;"></th>
								<th>'.$cname.'</th>
								<th>'.$order.'</th>
								<th>'.$date.'</th>
								<th><a href="edit-collection.php?cid='.$id.'">Edit</a> | <a href="index.php?delete='.$id.'&cover='.$cover.'&thumb='.$thumb.'&logo='.$logo.'" onclick="return checkDelete()">Delete</a></th>
							</tr>
							';
		}
	}
?>

<?php
	if(isset($_GET['delete'])){
		$delteid = $_GET['delete'];
		$cover = $_GET['cover'];
		$thumb = $_GET['thumb'];
		$logo = $_GET['logo'];
		
		$delet_product = mysql_query("delete from collection where cid='$delteid'");
		if($delet_product){
			unlink('../../collections/'.$delteid.'/thumb/'.$thumb.'');
			unlink('../../collections/'.$delteid.'/cover/'.$cover.'');
			unlink('../../collections/'.$delteid.'/logo/'.$logo.'');
			rmdir('../../collections/'.$delteid.'/thumb');
			rmdir('../../collections/'.$delteid.'/cover');
			rmdir('../../collections/'.$delteid.'/logo');
			
			$getGallery = mysql_query("DELETE FROM `gallery` WHERE `cid` = '$delteid'");
			if($getGallery){
				$files = glob('../../collections/'.$delteid.'/gallery/*'); // get all file names
				foreach($files as $file){ // iterate files
				  if(is_file($file))
					unlink($file); // delete file
				}
				rmdir('../../collections/'.$delteid.'/gallery');
			}
			rmdir('../../collections/'.$delteid.'');
			
			echo "<script>alert('Collection Deleted Successfully');window.location.href ='index.php'</script>";
		}else{
			
		}
	}
?>
	</head>
	<body>
		<?php include_once('../includes/header.php'); ?>
        <div class="container">
			<div class="row">
				<?php include_once('../includes/sidebar.php'); ?>
				<!-- Add new sidebar box here -->
				</div><!--Dont remove it-->
				<div class="col-sm-12 admin-content" style="padding:0;">
					<div class="title col-sm-12" style="padding:0;">Collection List</div><br>
					<hr/>
						<table class="table table-bordered" ><!-- id="t-table" -->
							<tr>
								<th>Thumb</th>
								<th>Name</th>
								<th>Order</th>
								<th>Date</th>
								<th>Action</th>
							</tr>
							<?php echo $product_list; ?>
						</table>
				</div>
			</div>
			<hr>
		</div>
		<?php include_once('../includes/footerjs.php'); ?>
		<script>
			var table2_Props = {
				paging: true,  
                paging_length:8,  
                rows_counter:true,  
                rows_counter_text:"Rows:",
				btn_reset: true,
				col_2: "select",
				col_0: "none",
				col_1: "none",
				col_4: "none",
				col_5: "none",
				display_all_text: "--",
				highlight_keywords: true,
				sort_select: true
			};
			var tf2 = setFilterGrid("t-table", table2_Props);
		</script>
		<script language="JavaScript" type="text/javascript">
			function checkDelete(){
				return confirm('Are you sure?');
			}
		</script>
	</body>
</html>