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

//CODE FOR ADD WORK
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['work_photo']['name']!="")
	{	
		$photopath = pathinfo($_FILES['work_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['work_photo']['tmp_name'];
		$work_photo = time().".".$extension;
		$destination = "../uploads/work/".$work_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$work_photo = '';
	}
	
	$sql_insert = "INSERT INTO `sc_works` SET `work_title` = '".addslashes($_POST['work_title'])."',`work_photo` = '".$work_photo."',`work_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Work addded successfully.';
	header("location: work_list.php");
	exit;
}

//CODE FOR UPDATE WORK
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['work_photo']['name']!="")
	{	
		unlink("../uploads/work/".$_POST['hid_work_photo']);
		
		$photopath = pathinfo($_FILES['work_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['work_photo']['tmp_name'];
		$work_photo = time().".".$extension;
		$destination = "../uploads/work/".$work_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$work_photo = $_POST['hid_work_photo'];
	}
	
	$sql_update = "UPDATE `sc_works` SET `work_title` = '".addslashes($_POST['work_title'])."',`work_photo` = '".$work_photo."' WHERE `work_id` = '".$_REQUEST['work_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Work updated successfully.';
	header("location: work_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_works` WHERE `work_id` = '".$_REQUEST['work_id']."'"));
	$work_title = stripslashes($fetch_record['work_title']);
	$work_photo = stripslashes($fetch_record['work_photo']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('work_title').value == '')
	{
		alert("Please enter work title");
		document.getElementById('work_title').focus();
		return false;
	}
	
	if(document.getElementById('work_photo').value == '' && document.getElementById('hid_work_photo').value == '')
	{
		alert("Please upload work photo");
		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-picture"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Recent Work</h1>
					<?php } else { ?>
					<h1>Add Recent Work</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="work_id" id="work_id" value="<?php echo $fetch_record['work_id'];?>" />
				<input type="hidden" name="hid_work_photo" id="hid_work_photo" value="<?php echo $fetch_record['work_photo'];?>" />
				<div class="block-fluid">
				    <div class="row-form clearfix">
						<div class="span3">Work Title <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="work_title" id="work_title" value="<?php echo $work_title;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">Work Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="work_photo" id="work_photo" />
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br /><a class="fancybox" rel="group" href="../uploads/work/<?php echo $work_photo;?>"><img src="../uploads/work/<?php echo $work_photo;?>" width="400" height="300" border="0" alt="" /></a>
						<?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='work_list.php'">Back To Recent Works List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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