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

//CODE FOR ADD SERVICE
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['subcategory_icon']['name']!="")
	{	
		$photopath = pathinfo($_FILES['subcategory_icon']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['subcategory_icon']['tmp_name'];
		$subcategory_icon = time().".".$extension;
		$destination = "../uploads/subcategory/".$subcategory_icon;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$subcategory_icon = '';
	}
	
	$sql_insert = "INSERT INTO `sc_subcategory` SET `category_id` = '".$_POST['hid_category_id']."',`subcategory_name` = '".addslashes($_POST['subcategory_name'])."',`subcategory_icon` = '".$subcategory_icon."',`subcategory_details` = '".addslashes($_POST['subcategory_details'])."',`quantity_available` = '".addslashes($_POST['quantity_available'])."',`timing` = '".addslashes($_POST['timing'])."',`service_price` = '".addslashes($_POST['service_price'])."',`subcategory_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Service added successfully.';
	header("location: service_list.php?category_id=".$_POST['hid_category_id']);
	exit;
}

//CODE FOR UPDATE SERVICE
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['subcategory_icon']['name']!="")
	{	
		unlink("../uploads/subcategory/".$_POST['hid_subcategory_icon']);
		
		$photopath = pathinfo($_FILES['subcategory_icon']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['subcategory_icon']['tmp_name'];
		$subcategory_icon = time().".".$extension;
		$destination = "../uploads/subcategory/".$subcategory_icon;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$subcategory_icon = $_POST['hid_subcategory_icon'];
	}
	
	$sql_update = "UPDATE `sc_subcategory` SET `subcategory_name` = '".addslashes($_POST['subcategory_name'])."',`subcategory_icon` = '".$subcategory_icon."',`subcategory_details` = '".addslashes($_POST['subcategory_details'])."',`quantity_available` = '".addslashes($_POST['quantity_available'])."',`timing` = '".addslashes($_POST['timing'])."',`service_price` = '".addslashes($_POST['service_price'])."' WHERE `subcategory_id` = '".$_POST['hid_subcategory_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Service updated successfully.';
	header("location: service_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_subcategory` WHERE `subcategory_id` = '".$_GET['subcategory_id']."'"));
	$subcategory_name = stripslashes($fetch_record['subcategory_name']);
	$subcategory_icon = stripslashes($fetch_record['subcategory_icon']);
	$subcategory_details = stripslashes($fetch_record['subcategory_details']);
	$quantity_available = stripslashes($fetch_record['quantity_available']);
	$timing = stripslashes($fetch_record['timing']);
	$service_price = stripslashes($fetch_record['service_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('subcategory_name').value == '')
	{
		alert("Please enter service");
		document.getElementById('subcategory_name').focus();
		return false;
	}
	
	if(document.getElementById('subcategory_icon').value == '' && document.getElementById('hid_subcategory_icon').value == '')
	{
		alert("Please upload icon");
		return false;
	}
	
	if(document.getElementById('service_price').value == '')
	{
		alert("Please enter price");
		document.getElementById('service_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 Service For <?php echo stripslashes($ferch_category['category_name']);?></h1>
					<?php } else { ?>
					<h1>Add Service 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_subcategory_id" id="hid_subcategory_id" value="<?php echo $fetch_record['subcategory_id'];?>" />
				<input type="hidden" name="hid_subcategory_icon" id="hid_subcategory_icon" value="<?php echo $fetch_record['subcategory_icon'];?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">Service <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="subcategory_name" id="subcategory_name" value="<?php echo $subcategory_name;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Icon <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="subcategory_icon" id="subcategory_icon" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><img src="../uploads/subcategory/<?php echo $subcategory_icon;?>" 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"><input type="text" name="service_price" id="service_price" value="<?php echo $service_price;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Service Details <font color="#FF0000">*</font></div>
						<div class="span9">
						<?php 
						$oFCKeditor = new FCKeditor('subcategory_details');
						$oFCKeditor->Height=400;
						$oFCKeditor->Width=700;
						$oFCKeditor->BasePath = '../FCKeditor/';
						$oFCKeditor->Value= $subcategory_details;
						$oFCKeditor->Create(); 
					   ?>
					   </div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Time Duration</div>
						<div class="span9"><input type="text" name="timing" id="timing" value="<?php echo $timing;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Quantity Available</div>
						<div class="span9">
						    <select name="quantity_available" id="quantity_available">
						        <option value="No" <?php if($quantity_available == "No") { ?>selected<?php } ?>>No</option>
						        <option value="Yes" <?php if($quantity_available == "Yes") { ?>selected<?php } ?>>Yes</option>
						    </select>
						</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='service_list.php?category_id=<?php echo $_GET['category_id'];?>'">Back To Services List For <?php echo stripslashes($ferch_category['category_name']);?></button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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