<?php include_once('../includes/head.php'); ?>
<?php include_once('includes/admin_auth.php'); ?>
<script src="ckeditor/ckeditor.js"></script>
<?php
	if(isset($_GET['edit'])){
		$productId = $_GET['edit'];
		
		$getProductList = mysql_query("select * from products where id='$productId'");
		$getArray = mysql_fetch_array($getProductList);
		
		$id = $getArray['id'];
		$name = $getArray['name'];
		$pid = $getArray['pid'];
		$des = $getArray['description'];
		$ext = $getArray['ext'];
		$doc = $getArray['doc'];
		$docext = $getArray['docext'];
		
		$cat1 = mysql_query("SELECT * FROM `category`");
		$count1 = mysql_num_rows($cat1);
		$cat_list='';
		if($count1>0){
			while($row = mysql_fetch_array($cat1)){
				$id1 = $row['id'];
				$cat_name1 = $row['name'];
				if($pid==$id1){
					$cat_list.='<option value="'.$id1.'" selected>'.$cat_name1.'</option>';
				}else{
					$cat_list.='<option value="'.$id1.'">'.$cat_name1.'</option>';
				}
				
			}
		}
	}
?>
<?php
	if(isset($_POST['update'])){
		echo 'ala re';
		$id = $_POST['id'];
		$name = $_POST['name'];
		$desc = $_POST['desc'];
		$cat = $_POST['cat'];
		
			if(!empty($_FILES['photo']['name'])){
				echo 'ala re 1';
				$path_parts=pathinfo($_FILES["photo"]["name"]);
				$ext = $path_parts['extension'];
				$photoname="$id.$ext";
				$updateProduct1 = mysql_query("update products set ext = '$ext' where id='$id'");
				echo move_uploaded_file($_FILES['photo']['tmp_name'],"products/$photoname");
			}
			if(!empty($_FILES['pdf']['name'])){
				$path_parts1=pathinfo($_FILES["pdf"]["name"]);
				$ext1 = $path_parts1['extension'];
				$pdfName="$id.$ext1";
				$updateProduct2 = mysql_query("update products set docext = '$ext1' where id='$id'");
				echo move_uploaded_file($_FILES['pdf']['tmp_name'],"pdf/$pdfName");
			}
		
		$updateProduct = mysql_query("update products set name='$name', description = '$desc', pid = '$cat' where id='$id'");
		if($updateProduct){
			echo "<script>alert('Product Updated Successfully');window.location.href ='edit-product.php?edit=".$id."'</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;">Product List</div><br>
					<hr/>
						<form action="edit-product.php" method="post" enctype="multipart/form-data">
						<table class="table table-bordered">
							<tr>
								<th width="20%">Name</th>
								<th><input type="text" name="name" value="<?php echo $name; ?>" class="form-control"></th>
							</tr>
							<tr>
								<th>Description</th>
								<th>
									<textarea name="desc" id="desc" ><?php echo $des; ?></textarea>
									<script type="text/javascript">
										CKEDITOR.replace( 'desc' );
									</script>
								</th>
							</tr>
							<tr>
								<th>Category</th>
								<th><select class="form-control" name="cat"><?php echo $cat_list; ?></select></th>
							</tr>
							<tr>
								
								<th>Thumb</th>
								<th>
									<img src="products/<?php echo $id; ?>.<?php echo $ext; ?>" style="width:150px;" class="img-responsive"><br/>
									<input type="file" name="photo" class="form-control">
								</th>
							</tr>
							<tr>
								<th>Doc</th>
								<th>
									<a href="pdf/<?php echo $id; ?>.<?php echo $docext; ?>" target="_blank"><?php echo $docext; ?></a>
									<input type="file" name="pdf" class="form-control">
								</th>
							</tr>
							<tr>
								<th><input type="submit" name="update" value="Update" class="btn btn-primary">
									<input type="hidden" name="id" value="<?php echo $id; ?>">
								</th>
								<th></th>
							</tr>
						</table>
						</form>
				</div>
			</div>
			<hr>
		</div>
		<?php include_once('../includes/footerjs.php'); ?>
	</body>
</html>