<?php 
include("includes/config.php");
include("includes/func.php");
include("classes/user.class.php");
include("classes/safeSQL.class.php");
	
/*if (($_SESSION['admin_id'] == "") && ($_SESSION['admin_login']  != "true"))
{
	redirect("login");
}

if($_SESSION['user_id'] != '0' )
{
	$user_id_login 		= 	$_SESSION['user_id'];

}else{
	
	$user_id_login		=	$_SESSION['admin_id'];
}*/

if (($_SESSION['id'] == ""))
{
	redirect("login");
}

include("templates/header.php");

$user 				= new user;
$safeSQL 			= new safeSQL;

$action 			= '';
$actionToExec 		= '';
$errorMsg 			= '';
$_SESSION['msg'] 	= '';
$page_title 		= '';
$page_heading 		= '';
$mui 				= '';
$btn 				= '';

if (isset($_GET['act'])) {
	$action = $safeSQL->$_GET['act'];
}

$page_title 	= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Add_New_User');
$page_heading 	= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Add_New_User');
$mui 			= 'add';
$btn_txt		= $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Save');

if ($action == 'delete')
	{
		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = "Error occured while retreving data.";
			redirect("user_management.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$res = $user->deleteUser($user_id);
		
		if ($res === true) 
			{
				$_SESSION['msg'] = "users has been deleted Successfully.";
				redirect("user_management.php");
				
			}
		else
			{
				$errorMsg = $res;
			}
	}
	
if ($action == 'edit')
	{
		if(!is_numeric($safeSQL->$_GET['uid'])) {
			$_SESSION['msg'] = "Error occured while retreving data.";
			redirect("user_management.php");
			
		}
		
		$user_id = intval($safeSQL->$_GET['uid']);
		
		$row = $user->getUserInfoById($user_id);
		
		if ($row) 
			{
				
				$first_name 				= $row['firstname'];
				$last_name 					= $row['lastname'];
				$email 						= $row['email'];
				$usertypeid 				= $row['usertypeid'];
				$username 					= $row['username'];
				$pass 						= $row['password'];
			}
		
		$page_title 	= 'Edit User';
		$page_heading 	= 'Edit User';
		$mui 			= 'update';
		$btn_txt		= 'Update';
	}
	
	
if (isset($_POST['mui'])) {
	$actionToExec = $safeSQL->$_POST['mui'];
}

if ($actionToExec == 'add')
	{			
			$first_name 				= $safeSQL->$_POST['first_name'];
			$last_name 					= $safeSQL->$_POST['last_name'];
			$email 						= $safeSQL->$_POST['email'];
			$user_type 					= $safeSQL->$_POST['user_type'];
			$username 					= $safeSQL->$_POST['username'];
			$pass 						= $safeSQL->$_POST['pass'];
			$re_pass					= $safeSQL->$_POST['re_pass'];
				
		
			if ($user->isUserName($username)) {
				$errorMsg = "Entered User Name already exists.<br/>";
			}
			
			if ($pass == '') {
				$errorMsg .= "Please enter you password.<br/>";
			}
			
			if ($pass != $re_pass && $pass != '') {
				$errorMsg .= "Password not matched, please try again.<br/>";
			}
		
		if ($errorMsg == '') {
		
			$temp = array($first_name, $last_name, $email, $user_type, $username, $pass);

			$res = $user->addUser($temp);

			if ($res === true) 
				{
					$_SESSION['msg'] = "User has been added Successfully.";
					redirect("user_management.php");
					
				}
			else
				{
					$errorMsg = $res;
				}
		}	 
			
	}

if ($actionToExec == 'update')
	{
		$user_id					= $safeSQL->$_POST['user_id'];
		$first_name 				= $safeSQL->$_POST['first_name'];
		$last_name 					= $safeSQL->$_POST['last_name'];
		$email 						= $safeSQL->$_POST['email'];
		$user_type 					= $safeSQL->$_POST['user_type'];
		$username 					= $safeSQL->$_POST['username'];		
		$pass 						= $safeSQL->$_POST['pass'];
		$re_pass					= $safeSQL->$_POST['re_pass'];
		$old_user_name					= $safeSQL->$_POST['old_user_name'];
		
	//	$old_email = $user->getEmail($old_user_name);
		
		if ($old_user_name != $username) {
			
			if ($user->isUserName($username)) {
				$errorMsg .= "Entered User Name already exists.<br/>";
			}
		}

		if ($pass == '') {
			$errorMsg .= "Please enter you password.<br/>";
		}
		
		if ($pass != $re_pass && $pass != '') {
			$errorMsg .= "Password not matched, please try again.<br/>";
		}
		
		if ($errorMsg == '') {

			$temp = array($first_name, $last_name, $email, $user_type, $username, $pass, $user_id);
			
			$res = $user->updateUser($temp);
			
			if ($res === true) 
				{
					$_SESSION['msg'] = "User has been updated Successfully.";
					redirect("user_management.php");
				}
			else
				{
					$errorMsg = $res;
				}
		} 
	}
		
?>

<?php $page_title ?>

<script type="text/javascript">
$(document).ready(function(e) {

		$("#frmUser").submit(function(){
		
					$('#First_Name_error').text('');
					$('#last_name_error').text('');
					$('#email_error').text('');
					$('#user_type_error').text('');
					$('#username_error').text('');
					$('#pass_error').text('');
					$('#re_pass_error').text('');				
					
					var first_name		=	$('#first_name').val();
					var last_name		=	$('#last_name').val();
					var email			=	$('#email').val();
					var user_type		=	$('#user_type').val();
					var username		=	$('#username').val();
					var pass			=	$('#pass').val();
					var re_pass			=	$('#re_pass').val();
					
					var email_reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;					

					if(first_name == "")
					{
						$('#First_Name_error').text(' *Please Enter First Name.');
						$('#First_Name_error').css({color: "red", "font-size": "10px"});
						return false;
					}
					
					if(last_name == "")
					{
						$('#last_name_error').text(' *Please Enter Last Name.');
						$('#last_name_error').css({color: "red", "font-size": "10px"});
						return false;
					}
					
					if((email == '') || (email_reg.test(email) == false))
					{
						$('#email_error').text(' *Please Enter Valid Email Address.');
						$('#email_error').css({color: "red", "font-size": "10px"});
						return false;					
					}

					if(user_type == "" || user_type == null)
					{
						$('#user_type_error').text(' *Please Select User Type.');
						$('#user_type_error').css({color: "red", "font-size": "10px"});
						return false;
					}
					
					if(username == "")
					{
						$('#username_error').text(' *Please Enter User Name.');
						$('#username_error').css({color: "red", "font-size": "10px"});
						return false;
					}					
					
					if(pass == "")					
					{
						$('#pass_error').text(' *Please Enter Password.');
						$('#pass_error').css({color: "red", "font-size": "10px"});
						return false;						
					}

					if(re_pass == "")					
					{
						$('#re_pass_error').text('*Please Confirm Password.');
						$('#re_pass_error').css({color: "red", "font-size": "10px"});
						return false;						
					}
					
					if(re_pass != pass)					
					{
						$('#re_pass_error').text('*Password Does Not Match.');
						$('#re_pass_error').css({color: "red", "font-size": "10px"});
						return false;						
					}										
						
						$('#submitButton').attr("disabled", "false");
						return true;		
		});	
});
	
function check(val,i)
{
//alert(val);
	if(val != "false")
	{
		document.getElementById('admin'+i).value = 'false';
		//alert(document.getElementById('admin'+i).value);
	}
	else
	{
		document.getElementById('admin'+i).value = 'true';
		//alert(document.getElementById('admin'+i).value);	
	}
	return document.getElementById('admin'+i).value;
} 
</script>
<!-- Start main box area in contains form-->
<div id="mainPanel">
<!-- Start form -->
	<h2><?php echo $page_heading ?></h2>
    <br clear="all" />
<div id="form">
	<form id="frmUser" name="frmUser" action="" method="post">
    	<input type="hidden" name="mui" id="mui" value="<?php echo $mui ?>" />
		<input type="hidden" name="user_id" id="user_id" value="<?php echo $user_id ?>" />
        <input type="hidden" name="old_user_name" id="old_user_name" value="<?php echo $username; ?>" />
    <table id="tabling1" width="100%" border="0" cellspacing="0" cellpadding="0">
        
		<?php 
            if ($_SESSION['msg'] != '') { 
           		echo "<tr><td colspan='2' nowrap align='center' id='tableheader'><span class='text1Col'>" ;   
			    print $_SESSION['msg'];
                $_SESSION['msg'] = '';
				echo "</span></td>";
            } 
        ?>        
        

        <?php if ($errorMsg != '') { ?>
        <tr>
            <td colspan="2"><span style="color:#ff0000; line-height:20px"><?php echo $errorMsg; ?></span></td>
        </tr>
        <?php } ?>
        <!--    id 	firstname 	lastname 	email 	shop_id 	usertypeid 	username 	password 	delete -->
        
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'First_Name');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="first_name" type="text" class="textfield1" id="first_name" value="<?php echo $first_name ?>" size="30" ><span id="First_Name_error"></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Last_Name');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="last_name" type="text" class="textfield1" id="last_name" value="<?php echo $last_name ?>" size="30" ><span id="last_name_error"></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Email');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="email" type="text" class="textfield1" id="email" value="<?php echo $email ?>" size="30" ><span id="email_error"></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'User_Type');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<select name="user_type" id="user_type" class="textfield1">
                		<option value="">Select User Type</option>
                        <option value="1" <?php if($usertypeid == '1'){echo "selected";}?>>Admin</option>
                        <option value="2" <?php if($usertypeid == '2'){echo "selected";}?>>User</option>
                	  </select><span id="user_type_error"></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Usre_Name');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="username" id="username" type="text" class="textfield1 email" value="<?php echo $username; ?>" size="30" ><span id="username_error"></span>
            </td>
        </tr>        
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Password');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="pass" type="password" class="textfield1" id="pass" value="<?php echo $pass; ?>" size="30" ><span id="pass_error"></span>
            </td>
        </tr>
        <tr> 
            <td nowrap width="34%" align="right"><?php echo $Get_XML_Responce->readXmlArrtibut('xml_user_management.xml', 'Confirm_Password');?>:<span style="color:red">*</span></td>
            <td width="66%" nowrap>
                &nbsp;<input name="re_pass" type="password" class="textfield1" id="re_pass" value="<?php echo $pass; ?>" size="30" ><span id="re_pass_error"></span>
            </td>
        </tr>
        <tr> 
            <td colspan="2" nowrap style="text-align:center">
                <input type="button" value="Cancel" class="form_button" onClick="cancelAct('user_management')" > 
                &nbsp; 
                <input type="submit" name="submitButton" class="form_button" id="submitButton" value="<?php echo $btn_txt ?>" >
            </td>
        </tr>
    </table>
		</form>
		
</div><!-- end form -->

</div><!-- end main box -->
<?php include("templates/footer.php"); ?>