<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Admin Staff";
$record_type="Admin Staff";
$script_name="manage_admin_staff.php";
$table_name="admin_details";
######################################################
if($id=="add_record") {
	$numCnt=getCount($table_name," where user_id='$user_id' and status!='D'");
	if($numCnt>0) {	
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		$form_check_alert=dynamic_check_form_values(array("rp--Please enter username" => "".$user_id."", "rp--Please enter password" => "".$pwd."", "rp--Please select country" => "".$country.""), $validation_ans_arr);
		
		if(!strlen($form_check_alert)>0) {
			$sql=db_query("insert into $table_name set user_id='$user_id', pwd='$pwd', name='$name', email='$email', address='$address', phone='$phone', country='$country', user_type='staff', addedOn=now()");
			$_SESSION['session_message']='New '.$record_type.' has been added successfully';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
			exit;
		}
		else {
			$msg=html_head();
			$msg.=$form_check_alert;
			$msg.=html_foot();
		}
	}
}


if($id=="update_record") {
	$numCnt=getCount($table_name," where user_id='$user_id' and status!='D' and slno!='$recordID'");
	if($numCnt>0) {	
		$_SESSION['session_message']=$record_type.' already exist';
	}
	else{
		$form_check_alert=dynamic_check_form_values(array("rp--Please enter username" => "".$user_id."", "rp--Please enter password" => "".$pwd."", "rp--Please select country" => "".$country.""), $validation_ans_arr);
		
		if(!strlen($form_check_alert)>0) {
			updateTable($table_name," user_id='$user_id', pwd='$pwd', name='$name', email='$email', address='$address', phone='$phone', country='$country' where slno='$recordID'");
			$_SESSION['session_message']=$record_type.' has been updated successfully';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
			exit;
		}
		else {
			$msg=html_head();
			$msg.=$form_check_alert;
			$msg.=html_foot();
		}
	}
}

if($action=="Delete"){
	if(sizeof($_POST['cid'])==0) {
		$_SESSION['session_message']='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	for ($x=0; $x < sizeof($_POST['cid']); $x++){
		$arr=$_POST['cid'];
		updateTable($table_name," status='D' where slno='$arr[$x]'");		
	}
	
	$_SESSION['session_message']='Record has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($action=="Activate"){
	if(sizeof($_POST['cid'])==0) {
		$_SESSION['session_message']='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		exit;
	}
	for ($x=0; $x < sizeof($_POST['cid']); $x++){
		$arr=$_POST['cid'];
		updateTable($table_name," status='Y' where slno='$arr[$x]'");
	}
	
	$_SESSION['session_message']='Selected record(s) activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($action=="Deactivate"){
	if(sizeof($_POST['cid'])==0) {
		$_SESSION['session_message']='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		exit;
	}
	for ($x=0; $x < sizeof($_POST['cid']); $x++){
		$arr=$_POST['cid'];
		updateTable($table_name," status='N' where slno='$arr[$x]'");
	}
	
	$_SESSION['session_message']='Selected record(s) deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}


if($id=="deactivate_record") {
	updateTable($table_name," status='N' where slno='$recordID' and status='Y' ");	
	$_SESSION['session_message']=$record_type.' has been deactivated successfully.';
	
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($id=="activate_record") {
	updateTable($table_name," status='Y' where slno='$recordID' and status='N' ");
	
	$_SESSION['session_message']=$record_type.' has been activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if ($id=="delete_record") {
	updateTable($table_name," status='D' where slno='$recordID'");
	
	$_SESSION['session_message']=$record_type.' has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header($heading);
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgcolor_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
 
<?php
$qry1= db_query("select * from $table_name where status!='D' and user_type='staff'");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name where status!='D' and user_type='staff' order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();

if($reccnt>0){
	?>
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"><strong>SL</strong></td>
	  <td width="10%"><strong>Username</strong></td>
	  <td width="8%"><strong>Password</strong></td>
	  <td width="15%"><strong>Email ID</strong></td>
	  <td width="20%"><strong>Address</strong></td>
	  <td width="8%"><strong>Phone</strong></td>
	  <td width="12%"><strong>Country</strong></td>
	  <td width="13%"><strong>Added On</strong></td>
	  <td width="9%"><strong>Options</strong></td>
	 </tr>
	 <?php
	 $sl=$start+1;
	 while ($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>">
		  <td valign="top"><input type=checkbox name="cid[]" value="<?=$arr[slno]?>" class="NoStyle"><?=$sl;?></td>
		  <td valign="top"><?=$arr['user_id'];?></td>
		  <td valign="top"><?=$arr['pwd'];?></td>
		  <td valign="top"><?=$arr['email'];?></td>
		  <td valign="top"><?=ucfirst(nl2br($arr['address']));?></td>
		  <td valign="top"><?=$arr['phone'];?></td>
		  <td valign="top"><?=$arr['country'];?></td>
		  <td valign='top'><?=show_date($arr['addedOn'])?></td>
		  <td valign='top'>
		   <?php
		   if($arr['status']=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate_record&recordID=<?=$arr[slno];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   if($arr['status']=='N') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate_record&recordID=<?=$arr[slno];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   ?>
		   &nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&recordID=<?=$arr[slno];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>
		   &nbsp;<a href="<?=$script_name;?>?id=delete_record&recordID=<?=$arr[slno]?>&start=<?=$start?>&pagesize=<?=$pagesize?>" onClick="return confirm('Are you sure you want to delete this record')"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" title="Delete This <?=$record_type;?>" border="0"></a>
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="5">
	   <input type="submit"  class="button" name="action" value="Delete"  onclick="return chkdelete();">
	   <input type="submit"  class="button" name="action" value="Activate"  onclick="return chkactivate();">
	   <input type="submit"  class="button" name="action" value="Deactivate"  onclick="return chkactivate();">
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table>
	</form>
	
	<?php	
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><strong>No Record Found.............</strong></td></tr>
	</table>
	<?php
}
?>
<br/>
<table width="96%" align="center" cellpadding="0" cellspacing="0" border="0">
 <tr><td align="right"><input type="button" onclick="window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?id=add_new#bot'" value="Add New <?=$record_type;?>" class="button"></td></tr>
</table>

<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<a name="bot"></a>
	<br/><br/>
	<form name="formn1" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%"  border="1" cellpadding="3" cellspacing="2" class="lightBorder">
	 <?php
	 if($recordID=="" || $id!="edit_record") {
		 ?>
		 <tr><td colspan="3" align="left" class="lightBlueBg PadFive" height="20"><strong><a name="bot">Add New <?=$record_type;?></a></strong></td></tr>
		 <?php
	 }
	 else {
		 ?>
		 <tr>
		  <td align="left" class="lightBlueBg PadFive" height="20"><strong>Edit <?=$record_type;?></strong></td>
		  <td align="right" class="lightBlueBg  PadFive" height="20"><a href="<?=$script_name;?>"><strong>Add New <?=$record_type;?></strong></a></td>
		 </tr>
		 <?php
	 }
	 $result=getResult($table_name," where slno='$recordID'");
	 ?>
	 <tr><td colspan="2" align="right"><font color="red">*</font> fields are mandatory</td></tr>
	 <tr>
	  <td class="brdrbtm" width="20%"><strong>Admin Staff Username <font color="red">*</font></strong></td>
	  <td class="brdrbtm"><input style="width:250px;" type="text" name="user_id" value="<?=$result['user_id']; ?>" class="txtfieldadmin" id="NOBLANK~Please enter admin staff username~DM~" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Password <font color="red">*</font></strong></td>
	  <td class="brdrbtm"><input style="width:250px;" type="text" name="pwd" value="<?=$result['pwd']; ?>" class="txtfieldadmin" id="NOBLANK~Please enter admin staff password~DM~" /></td>
	 </tr> 
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Name <font color="red">*</font></strong></td>
	  <td class="brdrbtm"><input style="width:250px;" type="text" name="name" value="<?=$result['name']; ?>" class="txtfieldadmin" id="NOBLANK~Please enter admin staff name~DM~" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Email </strong></td>
	  <td class="brdrbtm"><input style="width:250px;" type="text" name="email" value="<?=$result['email']; ?>" class="txtfieldadmin" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Address </strong></td>
	  <td class="brdrbtm"><textarea name="address" class="txtfieldadmin" style="width:250px;" rows="3"><?=$result['address'];?></textarea></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Phone </strong></td>
	  <td class="brdrbtm"><input style="width:250px;" type="text" name="phone" value="<?=$result['phone']; ?>" class="txtfieldadmin" /></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><strong>Admin Staff Country <font color="red">*</font></strong></td>
	  <td class="brdrbtm"><?=country("country",$result['country'],"255px");?></td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"></td>
	  <td class="brdrbtm">
	   <?php
	   if($recordID!="") {
		   ?>
		   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
		   <input type="hidden" name="recordID" value="<? echo $result[slno]; ?>">
		   <input type="hidden" name="id" value="update_record">
		   <?php
	   }
	   else{
		   ?>
		   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
		   <input type="hidden" name="id" value="add_record">
		   <?php
	   }
	   ?>
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table>
	</form>	
	<?php
}

admin_footer();
exit;
?>