<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeUser($db,'');
include_once "includes/pagepermission.php";

include "common/dpselect-functions.php";

//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
//check permission - END

$id=$_REQUEST['id'];
if($id!="")
{
	
//check edit permission - START	
if(trim($res_modm_prm['EditPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END	

$operation="Edit";
$act="update";
$btn='Update';

$str_ed = "select * from a_users where IsActive != '2' and user_ID = '".base64_decode($id)."' ";
$res_ed = $db->get_a_line($str_ed);

$edit_id = $res_ed['user_ID'];

$chk='';
if($res_ed['IsActive']=='1')
{
	$chk='checked';
}

}
else
{
	
//check edit permission - START	
if(trim($res_modm_prm['AddPrm'])=="0") {
?>
<script>
  window.location="error.php";
</script>
<?php	
}
//check edit permission - END
	$operation="Add";
	$act="insert";
	$btn='Save';
}
?>  



<div class="wrapper">
    <div class="container">
        <!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-12">										
                <h4 class="page-title"><?php echo $operation; ?> User Information</h4>				
            </div>
        </div>
		<!-- Page-Title - END -->	
					
		<div class="row">
            <div class="col-sm-12">
				<form id="jvalidate" name="frmUser" role="form" class="form-horizontal" action="#" method="post" enctype="multipart/form-data" >
				<input type="hidden" name="action" value="<?php echo $act; ?>" />
				<input type="hidden" name="edit_id" id="edit_id" value="<?php echo $edit_id; ?>"  />
				
				<div class="card-box">
				
					<div class="form-group row">
						<label class="col-md-2 form-control-label">First Name</label>
						<div class="col-md-10">
							<input type="text" class="form-control jsrequired" name="txtuser_firstname" id="txtuser_firstname" value="<?php echo $res_ed['user_firstname']; ?>" />
						</div>
					</div>  
					
					
					<div class="form-group row">
						<label class="col-md-2 form-control-label">Last Name</label>
						<div class="col-md-10">
							<input type="text" class="form-control jsrequired" name="txtuser_lastname" id="txtuser_lastname" value="<?php echo $res_ed['user_lastname']; ?>" />
						</div>
					</div> 
								

					<div class="form-group row">
						<label class="col-md-2 form-control-label">Email Id as Login Id</label>
						<div class="col-md-10">
							<input type="text" class="form-control jsrequired email" name="txtuser_email" id="txtuser_email"
							<?php if ($act == 'update') { ?> readonly="readonly" <?php } ?> value="<?php echo $res_ed['user_email']; ?>" />
						</div>
					</div>  
									
					<?php if($edit_id == ""){ ?>
					<div class="form-group row">
						<label class="col-md-2 form-control-label">Password</label>
						<div class="col-md-10">
							<input type="password" class="form-control jsrequired password" name="txtuser_password" id="txtuser_password" value="" />
						</div>
					</div>					
					<?php } ?>									
									
					<div class="form-group row">
						<label class="col-md-2 form-control-label">Role</label>
						<div class="col-md-8">					    
							<?php 
							echo getSelectBox_rolelist($db,'txtRoleId','jsrequired',$res_ed['RoleId']);
							?>
						</div>
					</div> 
									
					<div class="form-group row">
						<label class="col-md-2 form-control-label">Photo</label>
						<div class="col-md-10 userinfo-photo">					    
						<?php 
							if (!empty($res_ed['user_photo']) && ($act == 'update')) { 
								if(file_exists(CAAROOTADMIN_DIR."/uploads/adminusers/".$res_ed['user_photo']))
								{ ?>
									<img id="preview_img" src="uploads/adminusers/<?php echo $res_ed['user_photo']; ?>" width="50px" align="absmiddle"/>
								<?php
								}
								else{ ?>
									<img id="preview_img" src="uploads/NoImageAvailable.png" width="50px" align="absmiddle"/>
								<?php }
							} 
						?>					
											  
						<input id="user_photo" name="user_photo" type="file" class="file" data-show-upload="false" data-show-caption="true" accept="image/jpeg,image/png,jpg|png" /> Allowed Extension ( jpg, png, gif )
						
						</div>
					</div>							
					
					<div class="form-group row">
                        <label class="col-md-2 form-control-label" for="chkstatus">Status</label>
                        <div class="col-md-10">
                            <input type="checkbox" id="chkstatus" name="chkstatus" <?php echo $chk; ?> />
                        </div>
                    </div>
					
					<?php
					if (($act == 'update')) { 
					?>
					<div class="form-group">
						<label class="col-md-2 col-xs-12 control-label">&nbsp;</label>
						<div class="col-md-6 col-xs-12">                                            
							<div class="input-group">
								<a href="javascript:void(0);" class="cpassword" data-toggle="modal" data-target="#myModal">Change Password</a>
							</div>
						</div>
					</div>
					<?php } ?>
					
					<div class="form-group row">
						<div class="col-md-2"></div>
						<div class="col-md-10">														
							<button class="btn btn-primary waves-effect waves-light" type="button" onClick="javascript:funSubmtWithImg('frmUser','userinfo_actions.php','jvalidate','user','userinfo_mng.php');" ><span id="spSubmit"><?php echo $btn; ?></span></button>	
										 
							<button class="btn btn-secondary waves-effect m-l-5" type="button" onClick="javascript:funCancel('frmUser','jvalidate','user','userinfo_mng.php');" >Cancel</button>							
							
						</div>
					</div>
					
				</div>
				
					
				</form>
            </div>
        </div> <!-- end row -->		
		

<!-- Change Password Modal - START -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">Change Password</h4>
				</div>
				
				<div class="modal-body">  
					<div class="form-group row">
						<label class="col-md-3 col-xs-12 control-label">Password</label>
						<div class="col-md-6 col-xs-12">                                            
							<div class="input-group">
								<input type="password" class="form-control " name="newpwd" id="newpwd" value=""/>
							</div>                                            
						</div>
					</div>			
					
					<div class="form-group row">
						<label class="col-md-3 col-xs-12 control-label">Confirm Password</label>
						<div class="col-md-6 col-xs-12">                                            
							<div class="input-group">
								<input type="password" class="form-control " name="newcnfrmpwd" id="newcnfrmpwd" value=""/>
							</div>                                            
						</div>
					</div>
							
				</div>
				
				<div class="modal-footer">
					<!--<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>-->
					<button type="button" class="btn btn-primary" onclick="pwdchange();">Update Password</button>
				</div>
				
			</div>
		</div>
	</div>

 <!-- Change Password Modal - END -->		

<?php include "includes/footer.php";  ?> 
	</div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 


<script type="text/javascript"> 

$("document").ready(function(){

    $("#user_photo").change(function() {
        $("#preview_img").css("display","none");
    });
});


function pwdchange(){	  
	if($('#newpwd').val() == '' || $('#newcnfrmpwd').val() == ''){
		 alert('Please Enter password and Confirm Password Details');
	}	
	else if($('#newpwd').val()!=$('#newcnfrmpwd').val()){
		 alert('Password not matches');
	}
	else{
		var user_id = $('#edit_id').val(); 
		var user_email = $('#txtuser_email').val(); 		
		var new_pwd = $('#newpwd').val();
		$.ajax({
			url: 'userinfo_pwdchange.php',
			type: 'POST',
			data: 'user_id='+user_id+'&user_email='+user_email+'&new_pwd='+new_pwd+'',
			success: function(result) {	 														
					if(result =='success'){	
						alert("Password has been changed successfully");						
						$('#myModal').modal('hide');						
		 		    }						
					else {
                        alert(result);	
					}   
			}
		}); 				
	}
 }

</script>



</body>
</html>
	  