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

//CODE FOR ADD ADMIN
if(isset($_POST['add']))
{
	$sql_insert = "INSERT INTO `pr_administrator` SET `fname` = '".addslashes($_POST['fname'])."',`lname` = '".addslashes($_POST['lname'])."',`admin_username` = '".addslashes($_POST['email'])."',`admin_password` = '".md5($_POST['admin_password'])."',`admin_original_password` = '".$_POST['admin_password']."',`admin_type` = 'Sub Admin'";
	$exe_insert = mysqli_query($connection,$sql_insert);
	
	$_SESSION['add_succ_msg'] = 'Sub Administrator added successfully.';
	header("location: sub_admin_list.php");
	exit;
}

//CODE FOR UPDATE ADMIN
if(isset($_POST['edit']))
{
	$sql_update = "UPDATE `pr_administrator` SET `fname` = '".addslashes($_POST['fname'])."',`lname` = '".addslashes($_POST['lname'])."',`admin_username` = '".addslashes($_POST['email'])."',`admin_password` = '".md5($_POST['admin_password'])."',`admin_original_password` = '".$_POST['admin_password']."' WHERE `admin_id` = '".$_REQUEST['admin_id']."'";
	$exe_update = mysqli_query($connection,$sql_update);
	
	$_SESSION['edit_succ_msg'] = 'Sub Administrator updated successfully.';
	header("location: sub_admin_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_administrator` WHERE `admin_id` = '".$_REQUEST['admin_id']."'"));
	$fname = stripslashes($fetch_record['fname']);
	$lname = stripslashes($fetch_record['lname']);
	$email = stripslashes($fetch_record['admin_username']);
	$admin_original_password = stripslashes($fetch_record['admin_original_password']);
}
?>

<?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 Sub Administrator</h1>
					<?php } else { ?>
					<h1>Add Sub Administrator</h1>
					<?php } ?>
				</div>
				<form action="" method="post" enctype="multipart/form-data" onsubmit="return frm_validation();">
				<input type="hidden" name="admin_id" id="admin_id" value="<?php echo $fetch_record['admin_id'];?>" />
				<div class="block-fluid">
					<div class="row-form clearfix">
						<div class="span3">First Name <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="fname" id="fname" value="<?php echo $fname;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">Last Name <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="lname" id="lname" value="<?php echo $lname;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">Email Address <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="email" id="email" value="<?php echo $email;?>" /></div>
					</div>
					<div class="row-form clearfix">
						<div class="span3">Password <font color="#FF0000">*</font></div>
						<div class="span9"><input type="text" name="admin_password" id="admin_password" value="<?php echo $admin_original_password;?>" /></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='sub_admin_list.php'">Back To Sub Administrators List</button>
						</div>
					</div>
				</div>
				</form>
			</div>
		</div>
	</div>
</div> 

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