<?php 
require_once("header.php");
require_once("session_check.php");
require_once("../FCKeditor/fckeditor.php");

//CODE FOR ADD PRODUCT
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['product_photo']['name']!="")
	{	
		$photopath = pathinfo($_FILES['product_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['product_photo']['tmp_name'];
		$product_photo = time().".".$extension;
		$destination = "../uploads/product/".$product_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$product_photo = '';
	}
	
	$sql_insert = "INSERT INTO `sc_products` SET `category_id` = '".$_POST['hid_category_id']."',`product_name` = '".addslashes($_POST['product_name'])."',`product_photo` = '".$product_photo."',`product_details` = '".addslashes($_POST['product_details'])."',`product_price` = '".$_POST['product_price']."',`product_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Product added successfully.';
	header("location: product_list.php?category_id=".$_POST['hid_category_id']);
	exit;
}

//CODE FOR UPDATE PRODUCT
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['product_photo']['name']!="")
	{	
		unlink("../uploads/product/".$_POST['hid_product_photo']);
		
		$photopath = pathinfo($_FILES['product_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['product_photo']['tmp_name'];
		$product_photo = time().".".$extension;
		$destination = "../uploads/product/".$product_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$product_photo = $_POST['hid_product_photo'];
	}
	
	$sql_update = "UPDATE `sc_products` SET `product_name` = '".addslashes($_POST['product_name'])."',`product_photo` = '".$product_photo."',`product_details` = '".addslashes($_POST['product_details'])."',`product_price` = '".$_POST['product_price']."' WHERE `product_id` = '".$_POST['hid_product_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Product updated successfully.';
	header("location: product_list.php?category_id=".$_POST['hid_category_id']);
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_products` WHERE `product_id` = '".$_GET['product_id']."'"));
	$product_name = stripslashes($fetch_record['product_name']);
	$product_photo = stripslashes($fetch_record['product_photo']);
	$product_details = stripslashes($fetch_record['product_details']);
	$product_price = stripslashes($fetch_record['product_price']);
}
?>

<?php
$ferch_category = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_category` WHERE `category_id` = '".$_GET['category_id']."'"));
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('product_name').value == '')
	{
		alert("Please enter name");
		document.getElementById('product_name').focus();
		return false;
	}
	
	if(document.getElementById('product_photo').value == '' && document.getElementById('hid_product_photo').value == '')
	{
		alert("Please upload photo");
		return false;
	}
	
	if(document.getElementById('product_price').value == '')
	{
		alert("Please enter price");
		document.getElementById('product_price').focus();
		return false;
	}
}
</script>

<?php require_once("left_panel.php");?>
<div class="content">
	<div class="workplace">
		<div class="row-fluid">
			<div class="span12">
				<div class="head clearfix">
					<div class="isw-documents"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Product For <?php echo stripslashes($ferch_category['category_name']);?></h1>
					<?php } else { ?>
					<h1>Add Product For <?php echo stripslashes($ferch_category['category_name']);?></h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="hid_category_id" id="hid_category_id" value="<?php echo $_GET['category_id'];?>" />
				<input type="hidden" name="hid_product_id" id="hid_product_id" value="<?php echo $fetch_record['product_id'];?>" />
				<input type="hidden" name="hid_product_photo" id="hid_product_photo" value="<?php echo $fetch_record['product_photo'];?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">Name <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="product_name" id="product_name" value="<?php echo $product_name;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="product_photo" id="product_photo" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><img src="../uploads/product/<?php echo $product_photo;?>" border="0" alt="" width="150" height="150" />
						<?php } ?>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Price <font color="#FF0000">*</font></div>
						<div class="span9">Rs.<input type="text" name="product_price" id="product_price" value="<?php echo $product_price;?>" style="width:50%;" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Details <font color="#FF0000">*</font></div>
						<div class="span9">
						<?php 
						$oFCKeditor = new FCKeditor('product_details');
						$oFCKeditor->Height=400;
						$oFCKeditor->Width=700;
						$oFCKeditor->BasePath = '../FCKeditor/';
						$oFCKeditor->Value= $product_details;
						$oFCKeditor->Create(); 
					   ?>
					   </div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">&nbsp;</div>
						<div class="span9">
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
							<button class="btn" type="submit" name="edit">Save</button>
						<?php } else { ?>
							<button class="btn" type="submit" name="add">Save</button>
						<?php } ?>
							<button class="btn" type="button" name="back" onclick="window.location.href='product_list.php?category_id=<?php echo $_GET['category_id'];?>'">Back To Products List For <?php echo stripslashes($ferch_category['category_name']);?></button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

<?php require_once("footer.php"); ?>