<?php 
require_once("header.php");
require_once("session_check.php");

//CODE FOR ADD CATEGORY
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['category_icon']['name']!="")
	{	
		$photopath = pathinfo($_FILES['category_icon']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['category_icon']['tmp_name'];
		$category_icon = time().".".$extension;
		$destination = "../uploads/category/".$category_icon;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$category_icon = '';
	}
	
	if($_FILES['category_banner']['name']!="")
	{	
		$photopath = pathinfo($_FILES['category_banner']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['category_banner']['tmp_name'];
		$category_banner = md5(time()).".".$extension;
		$destination = "../uploads/category/".$category_banner;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$category_banner = '';
	}
	
	$sql_insert = "INSERT INTO `sc_category` SET `category_name` = '".addslashes($_POST['category_name'])."',`category_icon` = '".$category_icon."',`category_banner` = '".$category_banner."',`category_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Category added successfully.';
	header("location: category_list.php");
	exit;
}

//CODE FOR UPDATE CATEGORY
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['category_icon']['name']!="")
	{	
		unlink("../uploads/category/".$_POST['hid_category_icon']);
		
		$photopath = pathinfo($_FILES['category_icon']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['category_icon']['tmp_name'];
		$category_icon = time().".".$extension;
		$destination = "../uploads/category/".$category_icon;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$category_icon = $_POST['hid_category_icon'];
	}
	
	if($_FILES['category_banner']['name']!="")
	{	
		unlink("../uploads/category/".$_POST['hid_category_banner']);
		
		$photopath = pathinfo($_FILES['category_banner']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['category_banner']['tmp_name'];
		$category_banner = md5(time()).".".$extension;
		$destination = "../uploads/category/".$category_banner;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$category_banner = $_POST['hid_category_banner'];
	}
	
	$sql_update = "UPDATE `sc_category` SET `category_name` = '".addslashes($_POST['category_name'])."',`category_icon` = '".$category_icon."',`category_banner` = '".$category_banner."' WHERE `category_id` = '".$_REQUEST['category_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Category updated successfully.';
	header("location: category_list.php");
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `sc_category` WHERE `category_id` = '".$_REQUEST['category_id']."'"));
	$category_name = stripslashes($fetch_record['category_name']);
	$category_icon = stripslashes($fetch_record['category_icon']);
	$category_banner = stripslashes($fetch_record['category_banner']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('category_name').value == '')
	{
		alert("Please enter category");
		document.getElementById('category_name').focus();
		return false;
	}
	
	if(document.getElementById('category_icon').value == '' && document.getElementById('hid_category_icon').value == '')
	{
		alert("Please upload icon");
		return false;
	}
	
	if(document.getElementById('category_banner').value == '' && document.getElementById('hid_category_banner').value == '')
	{
		alert("Please upload banner");
		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 Category</h1>
					<?php } else { ?>
					<h1>Add Category</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="category_id" id="category_id" value="<?php echo $fetch_record['category_id'];?>" />
				<input type="hidden" name="hid_category_icon" id="hid_category_icon" value="<?php echo $fetch_record['category_icon'];?>" />
                <input type="hidden" name="hid_category_banner" id="hid_category_banner" value="<?php echo $fetch_record['category_banner'];?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">Category <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="category_name" id="category_name" value="<?php echo $category_name;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">Icon <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="category_icon" id="category_icon" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><img src="../uploads/category/<?php echo $category_icon;?>" border="0" alt="" width="150" height="150" />
						<?php } ?>
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Banner <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="category_banner" id="category_banner" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><img src="../uploads/category/<?php echo $category_banner;?>" border="0" alt="" width="700" height="400" />
						<?php } ?>
						</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='category_list.php'">Back To Category List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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