<?php
$msg='';

include("include/settings.php");
include("checksession.php");

$usermaster=$mysqli->prepare("select count(*) from cdi_usermaster where user_id=?");
$usermaster->bind_param("i",$_GET['userid']);
$usermaster->execute();
$usermaster->bind_result($membercount);
$usermaster->fetch();
$usermaster->close();

if($membercount==0)
{
	header("location:index.php");
}

$memberdetailmaster=$mysqli->prepare("select * from cdi_usermaster where user_id=?");
$memberdetailmaster->bind_param("i",$_GET['userid']);
$memberdetailmaster->execute();
$memberdetailmaster=$memberdetailmaster->get_result();
$userrow=mysqli_fetch_object($memberdetailmaster);

if(isset($_POST['btnSaveMember']))
{
	$_POST["txtType"]='sales_exec';
	if(trim($_POST["txtEmailId"])!=null && trim($_POST["txtName"])!=null && trim($_POST["txtType"])!=null &&  trim($_POST["country"])!=null && (trim($_POST["txtPassword"])==trim($_POST["txtConfirmPassword"])))
	{	
		$my_user_email=strtolower($_POST["txtEmailId"]);
		if(trim($_POST['txtPassword'])!=null)
		{
			$password = $_POST["txtPassword"];
			if ((strlen($password) < 8 || strlen($password) > 20) || (!preg_match("#[0-9]+#",$password)) || (!preg_match("#[A-Z]+#",$password)) || (!preg_match("#[a-z]+#",$password))  || (!preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $password)))
			{
				$msg= "Password Must Contain 8-20 Characters! with At Least 1 Number, 1 uppercase,1 lowercase and 1 special character.";
			}
			else
			{
				$usermaster=$mysqli->prepare("select count(*) from cdi_usermaster where user_email_id=? and user_id!=?");
				$usermaster->bind_param("si",$my_user_email,$_GET['userid']);
				$usermaster->execute();
				$usermaster->bind_result($count);
				$usermaster->fetch();
				$usermaster->close();

		//		if($count>0)
		//		{
		//			$msg="Email Id Already Exists";
	//			}
	//			else
			//	{
					if($_POST['txtPassword']!=null || $my_user_email!=$_POST['txtHfEmailId'])
					{
						$md5password=md5('cybeRintelligencE'.$_POST['txtPassword'].$userrow->user_creationdate);
					}
					else
					{
						$md5password=$userrow->user_password;
					}	
					$usermaster=$mysqli->prepare("update cdi_usermaster set user_email_id=?,user_password=?,user_full_name=?,user_type=?,extension=?,extension3=?,country=?,user_status=? where user_id=?");
					$usermaster->bind_param("sssssssii",$my_user_email,$md5password,$_POST['txtName'],$_POST['txtType'],$_POST['extension'],$_POST['extension3'],$_POST['country'],$_POST['rbtnStatus'],$_GET['userid']);
					$usermaster->execute();
					$usermaster->close();
					header("location:manage-users.php");
			//	}
			}
		}
		else
		{
			$usermaster=$mysqli->prepare("select count(*) from cdi_usermaster where user_email_id=? and user_id!=?");
			$usermaster->bind_param("si",$my_user_email,$_GET['userid']);
			$usermaster->execute();
			$usermaster->bind_result($count);
			$usermaster->fetch();
			$usermaster->close();

			if($count>0)
			{
				$msg="Email Id Already Exists";
			}
			else
			{
				$md5password=$userrow->user_password;
				$usermaster=$mysqli->prepare("update cdi_usermaster set user_email_id=?,user_password=?,user_full_name=?,user_type=?,extension=?,extension3=?,country=?,user_status=? where user_id=?");
				$usermaster->bind_param("sssssssii",$my_user_email,$md5password,$_POST['txtName'],$_POST['txtType'],$_POST['extension'],$_POST['extension3'],$_POST['country'],$_POST['rbtnStatus'],$_GET['userid']);
				$usermaster->execute();
				$usermaster->close();
				header("location:manage-users.php");
			}
		}
	}
	else
	{
		$msg="Please enter all the fields correctly.";
	}
}

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Update User - Rishta Depot </title>
  
		
<?php include('./include/meta.php'); ?>

</head>
	
<?php include('./include/header.php'); ?>
  
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
	  <div class="container-full">
		<!-- Content Header (Page header) -->
		<div class="content-header">
			<div class="d-flex align-items-center">
				<div class="me-auto">
					<h4 class="page-title">Users</h4>
					
					<div class="d-inline-block align-items-center">
						<nav>
							<ol class="breadcrumb">
								<li class="breadcrumb-item"><a href="#"><i class="mdi mdi-home-outline"></i></a></li>
								<li class="breadcrumb-item" aria-current="page">Edit Users</li>
								<li class="breadcrumb-item active" aria-current="page">Users</li>
							</ol>
						</nav>
					</div>
				</div>
				
			</div>
		</div>	  

		<!-- Main content -->
		<section class="content">
			<div class="row">			  
				<div class="col-12">
					<div class="box">
						<div class="box-header with-border">
						  <h4 class="box-title">Edit Users</h4>
						  	<?php
					if(trim($msg)!=null)
					{
						?>
							<div class='row'>
								<div class='col-md-12'>
									<div class="alert alert-success errormessage" id="errormessage">
										<strong><?php echo $msg ?></strong>
									</div>
								</div>
							</div>
						<?php
					}
					?>
						</div>
						
						<!-- /.box-header -->
						<form action="#" method="post" enctype="multipart/form-data">
							<div class="box-body">
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									    
									  <label class="form-label">Email Id</label>
								<input type="text" id="txtEmailId" name="txtEmailId" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
								<input type="hidden" id="txtHfEmailId" name="txtHfEmailId" class="form-control" value="<?php echo $userrow->user_email_id ?>"/>
									</div>
								  </div>
								  <div class="col-md-6">
									<div class="form-group">
								      <label class="form-label">Name</label>
									 <input type="text" maxlength="50" id="txtName" name="txtName" class="form-control" value="<?php echo $userrow->user_full_name ?>"/>
								    </div>
								  </div>
								</div>
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Password </label>
									 	<input type="password" maxlength="50" id="txtPassword" name="txtPassword" class="form-control" />
									</div>
								  </div>
								  <div class="col-md-6">
									<div class="form-group">
								      <label class="form-label">Confirm Password</label>
									<input type="password" maxlength="50" id="txtConfirmPassword" name="txtConfirmPassword" class="form-control" />
								    </div>
								  </div>
								</div>
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Extension ID </label>
									 <input type="text" maxlength="50" id="extension" name="extension" class="form-control" value="<?php echo $userrow->extension ?>"/>
									</div>
								  </div>
								  <div class="col-md-6">
									<div class="form-group">
								      <label class="form-label">3cx Extension ID</label>
									  <input type="text" maxlength="50" id="extension3" name="extension3" class="form-control" value="<?php echo $userrow->extension3 ?>"/>
								    </div>
								  </div>
								</div>
									<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
									  <label class="form-label">Country</label>
									 <input type="text" maxlength="50" id="country" name="country" class="form-control-custom" value="<?php echo $userrow->country ?>"/>
									</div>
								</div>
								  </div>
								<div class="row">
								  <div class="col-md-6">
									<div class="form-group">
										<label class="form-label">Status</label>
										<div class="c-inputs-stacked">
											<input type="radio" value="1" id="rbtnActive" name="rbtnStatus" <?php echo $userrow->user_status==1?"checked":"" ?> /><label for="rbtnActive" class="me-30">Active</label> &nbsp;&nbsp;
								<input type="radio" value="0" id="rbtnInactive" name="rbtnStatus" <?php echo $userrow->user_status==0?"checked":"" ?> /><label for="rbtnInactive" class="me-30">Inactive</label>
										</div>
								    </div>
								  </div>
								</div>
								<div class="box-footer">
								<!--button type="submit" class="btn btn-primary">
								  <i class="ti-save-alt"></i> Save Sales Executive</button>
								<button type="button" class="btn btn-warning me-1">
								  <i class="ti-trash"></i> Cancel</button-->
								  			<input type="submit" id="btnSaveMember" name="btnSaveMember" value="Save User" class="btnCustom" onclick="return saveuser()"/> &nbsp;&nbsp;
								<input type="button" id="btnBack" name="btnBack" value="Go Back" class="btnCustom" onclick="return back()"/>
							</div>
							</div>
						</form>
                    </div>
                </div>
			</div>
        </section>
			<script>
		
		function saveuser()
		{
			var txtName=$('#txtName').val();
			var txtPassword=$('#txtPassword').val();
			var txtConfirmPassword=$('#txtConfirmPassword').val();
			var txtEmailId=$('#txtEmailId').val();
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

			if($.trim(txtName).length==0 || $.trim(txtPassword).length==0 || $.trim(txtConfirmPassword).length==0 || $.trim(txtEmailId).length==0)
			{
				alert("Please fill all the required fields.");
				return false;
			}
			else if(emailReg.test(txtEmailId) == false)
			{
				alert("Enter the valid email");
				return false;
			}
			else if(txtPassword!=txtConfirmPassword)
			{
				alert("Enter The Correct Passwords");
				return false;
			}
			else if(txtPassword.length!=0 && ((txtConfirmPassword.length < 8 || txtConfirmPassword.length>20) || (!/\d/.test(txtConfirmPassword)) || (!/[a-z]/.test(txtConfirmPassword)) || (!/[A-Z]/.test(txtConfirmPassword)) || (!/[!@#$%\^&*(){}[\]<>?/|\-]/.test(txtConfirmPassword))))
			{
				msg="Password must be 8-20 characters<br/>";
				msg+="Password must contain 1 digit<br/>";
				msg+="Password must contain 1 lower case character<br/>";
				msg+="Password must contain 1 upper case character<br/>";
				msg+="Password must contain 1 special character<br/>";
				$('.errormessage').html(msg);
				return false;
			}
			else
			{
				return confirm('Are You Sure?');
			}
		}
		
		function back() 
		{
			var c=confirm('Are You Sure');
			if(c==true)
			{
				window.location="manage-users.php";
			}
		}  	
		
		function isDecimal(evt, element) 
		{
			var charCode = (evt.which) ? evt.which : event.keyCode
			if ((charCode != 46 || $(element).val().indexOf('.') != -1) &&      // “.” CHECK DOT, AND ONLY ONE.
				(charCode < 48 || charCode > 57))
				return false;
			return true;
		}  	
		function isNumber(evt, element) 
		{
			var charCode = (evt.which) ? evt.which : event.keyCode
			if ((charCode < 48 || charCode > 57))
				return false;
			return true;
		}   
		$(document).ready(function() {
			$('body').on("keypress",'#txtExperience',function(){  
				return isDecimal(event, this)
			});
			$('body').on("keypress",'#txtCredits',function(){  
				return isNumber(event, this)
			});	
			$('#txtName').keypress(function (e) 
			{
				var regex = new RegExp("^[a-zA-Z ]+$");
				var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
				if (regex.test(str)) {
					return true;
				}

				e.preventDefault();
				return false;
			});
			$('input').bind("cut copy paste",function(e) {
				e.preventDefault();
			});
		});	
	</script>
	
<?php include('./include/footer.php'); ?>
