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

//CODE FOR ADD MEMBER
if(isset($_POST['add']))
{
	//UPLOAD IMAGE
	if($_FILES['member_photo']['name']!="")
	{	
		$photopath = pathinfo($_FILES['member_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['member_photo']['tmp_name'];
		$member_photo = time().".".$extension;
		$destination = "../uploads/team/".$member_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$member_photo = '';
	}

	$sql_insert = "INSERT INTO `pr_team` SET `member_name` = '".addslashes($_POST['member_name'])."',`member_designation` = '".addslashes($_POST['member_designation'])."',`member_photo` = '".$member_photo."',`member_status` = 'Active'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Member added successfully.';
	header("location: member_list.php");
	exit;
}

//CODE FOR UPDATE MEMBER
if(isset($_POST['edit']))
{
	//UPLOAD IMAGE
	if($_FILES['member_photo']['name']!="")
	{	
		unlink("../uploads/team/".$_POST['hid_member_photo']);
		
		$photopath = pathinfo($_FILES['member_photo']['name']);
		$extension = $photopath['extension'];
		$source = $_FILES['member_photo']['tmp_name'];
		$member_photo = time().".".$extension;
		$destination = "../uploads/team/".$member_photo;
		move_uploaded_file($source,$destination);
	}
	else
	{
		$member_photo = $_POST['hid_member_photo'];
	}

	$sql_update = "UPDATE `pr_team` SET `member_name` = '".addslashes($_POST['member_name'])."',`member_designation` = '".addslashes($_POST['member_designation'])."',`member_photo` = '".$member_photo."' WHERE `member_id` = '".$_POST['hid_member_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Member updated successfully.';
	header("location: member_list.php");
	exit;
}

//FETCH DATA FROM DATABASE
if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") 
{
	$fetch_record = mysqli_fetch_array(mysqli_query($connection,"SELECT * FROM `pr_team` WHERE `member_id` = '".$_REQUEST['member_id']."'"));
	$member_name = stripslashes($fetch_record['member_name']);
	$member_photo = stripslashes($fetch_record['member_photo']);
	$member_designation = stripslashes($fetch_record['member_designation']);
}
?>

<script language="javascript" type="text/javascript">
function frm_validation()
{
	if(document.getElementById('member_name').value == '')
	{
		alert("Please enter name");
		document.getElementById('member_name').focus();
		return false;
	}
	
	if(document.getElementById('member_designation').value == '')
	{
		alert("Please enter designation");
		document.getElementById('member_designation').focus();
		return false;
	}
	
	if(document.getElementById('member_photo').value == '' && document.getElementById('hid_member_photo').value == '')
	{
		alert("Please upload 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-documents"></div>
					<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
					<h1>Update Member</h1>
					<?php } else { ?>
					<h1>Add Member</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="hid_member_id" id="hid_member_id" value="<?php echo $_GET['member_id'];?>" />
				<input type="hidden" name="hid_member_photo" id="hid_member_photo" value="<?php echo $member_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="member_name" id="member_name" value="<?php echo $member_name;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Designation <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="member_designation" id="member_designation" value="<?php echo $member_designation;?>" />
						</div>                            
					</div>
					<div class="row-form clearfix">
						<div class="span3">Photo <font color="#FF0000">*</font></div>
						<div class="span9"><input type="file" name="member_photo" id="member_photo" /><br><font color="#ff0000;">[Photo size should be width 270px and height 220px]]</font>
						<?php if(isset($_REQUEST['action']) && $_REQUEST['action'] == "edit") { ?>
						<br />
						<img src="../uploads/team/<?php echo $member_photo;?>" alt="" height="220" width="270" />
						<?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='member_list.php'">Back To Members List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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