<?php session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
get_pagesecurity('45','edits','R');      
$_SESSION['cuPages']='rou_userlist.php';   
$cuPage='rou_userlist.php';    
$aid=$_SESSION['uid'];
$uty=$_SESSION['utype'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;    
}
$mhead='user';
$menuh='User &amp; Role';
$phead='userlist';
$page='Edit User';
$dtnow = date("Y-m-d h:i:s", time());
?>
<?php
include ('../inc/imgpro.php');
include('../layout/head.php');
include('../layout/header.php');
include('../layout/side_nav.php');
?>
<?php
if(isset($_POST['save_update'])){
$uid = remove_junk(escape($_POST['uid']));    
$email = remove_junk(escape($_POST['email']));
$name = remove_junk(escape($_POST['name']));
$bname = remove_junk(escape($_POST['bname']));     
$acess = remove_junk(escape($_POST['acess']));
$ibrid = remove_junk(escape($_POST['ibrid']));    
$item = PATHINFO($_FILES["item"]["name"]);     
$status = remove_junk(escape($_POST['status'])); 

if(isset($_POST['email'])){
$ducode = mysqli_query($con,"SELECT * FROM tbl_user WHERE email = '$email' AND id!='$uid'");
}
	
if($ducode->num_rows > 0) {
save_msg('w','Email Address alrady used! Plz try another');
echo "<script>window.location='rou_userlist.php'</script>";
}else{       
$sql="UPDATE tbl_user SET email='$email',acess='$acess',abrid='$ibrid',name='$name',bname='$bname',status='$status' WHERE id='$uid'";
$result = mysqli_query($con,$sql) or die(mysqli_error($con));
$eid=$con->insert_id;    
if($result){
if (!empty($_FILES["item"]["name"])){
get_destroy($uid,'U');    
get_upload($uid,$_FILES['item'],'U');    
}
$act =remove_junk(escape('Update: '.$name));    
write_activity($aid,'USR','User has been Updated',$act);    
save_msg('s','User Update Successfully!!!');
echo "<script>window.location='rou_userlist.php'</script>";	
}else{
save_msg('e','User update fail!!!!');
echo "<script>window.location='rou_userlist.php'</script>";		
}
}
}    
?>
<div class="content-wrapper">
<?php include('../layout/page_head.php'); ?>
<!-- Main content -->
<section class="content">

<div class="row">
<div class="col-md-8">
<div class="box box-solid">
<div class="box-header with-border">
<h3 class="box-title">Edit User</h3>
</div>
<div class="box-body">
<?php echo display_msg();?>  
<form action="rou_useredit.php" onsubmit="return validate()" enctype="multipart/form-data" method="post" accept-charset="utf-8">
<div class="col-md-12 popup_details_div">
<div class="col-md-2"></div>
<div class="col-md-8">
<?php 
if(isset($_POST['editusr'])){
$ids = $_POST['editusr'];
$sql="SELECT * FROM tbl_user WHERE id='".$ids."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
while ($adm=mysqli_fetch_array($admin)){
?>    
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Email</label>
<div class="input-group">
<span class="input-group-addon">E</span>   
<input type="email" class="form-control" maxlength="45" name="email" id="email" value="<?php echo $adm['email'];?>" placeholder="Email" autocomplete="off">
</div>    
<input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="uid" autocomplete="off" readonly>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group" >
<label><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Name (English)';}else{echo 'নাম (ইংরেজিতে)';}?></label>
<div class="input-group">
<span class="input-group-addon">N</span>
<input type="text" maxlength="35" class="form-control" name="name" id="name" value="<?php echo $adm['name'];?>" placeholder="e. g Sumon" autocomplete="off">
</div>
</div>
<div class="form-group" >
<label><?php if(get_fild_data('tbl_setting','1','sval')==0){echo 'Name (বাংলায়)';}else{echo 'নাম (বাংলায়)';}?></label>
<div class="input-group">
<span class="input-group-addon">N</span>
<input type="text" maxlength="255" class="form-control" name="bname" id="bname" value="<?php echo $adm['bname'];?>" placeholder="e. g সুমন" autocomplete="off">
</div>
</div>    
</div>
</div>    
<div class="row">
<div class="col-md-6">
<div class="form-group" >
<label for="inputName" class="control-label mb-10">Access Lavel</label>
<div class="input-group">
<span class="input-group-addon">AL</span>
<select class="form-control select2" name="acess">
<option value="">-Select-</option>
<?php									
$queryd=mysqli_query($con,"SELECT * FROM tbl_usergroup ORDER BY id ASC")or die(mysqli_error($con));
while ($rowd=mysqli_fetch_array($queryd)){
?>
<?php if($rowd['id']==$adm['acess']){ ?>
<option selected value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
<?php }else{?>    
<option value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
<?php } ?> 
<?php } ?>     
</select>
</div>
</div>
<div class="form-group" >
<label class="control-label mb-10">Branch</label>
<div class="input-group">
<span class="input-group-addon">BR</span>
<select class="form-control select2" name="ibrid" id="ibrid">
<option value="">-Select-</option>
<?php if($adm['abrid']==0){?> 
<option selected value="0">No Branch</option>    
<?php }else{ ?> 
<option value="0">No Branch</option>    
<?php } ?>       
<?php									
$queryd=mysqli_query($con,"SELECT * FROM tbl_branch ORDER BY name ASC")or die(mysqli_error($con));
while ($rowd=mysqli_fetch_array($queryd)){
?>
<?php if($rowd['id']==$adm['abrid']){ ?>
<option value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
<?php }else{ ?>    
<option value="<?php echo $rowd['id'];?>"><?php echo $rowd['name'];?></option>
<?php } ?>    
<?php } ?>                                                                
</select>
</div>
</div>    
<div class="form-group" >
<label>Status</label>
<div class="input-group">
<span class="input-group-addon">ST</span>    
<select class="form-control" name="status">
<option <?php if($adm['status']==1){echo 'selected';}?> value="1">Active</option>
<option <?php if($adm['status']==0){echo 'selected';}?> value="0">De-Active</option>
</select>    
</div>
</div>    
</div>
<div class="col-md-6">
<div class="form-group">
<label>Image</label>
<div style="width:200px; height:245px;">
<img src="../img/user/<?php if(empty($adm['image'])) {echo "duser.jpg";}else{echo $adm['image'];} ?>" id="profile-img-tag" style="width: 100%; height: 100%; object-fit: contain;">
</div>
<br>    

<input type="file" class="btn btn-flat bg-purple btn-sm" name="item" id="profile-img" accept=".png, .gif, .jpg, .jpeg">

</div>     
</div>    
</div>
<?php }} ?>    
</div>    
<div class="col-md-2"></div>
</div>
    
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<input type="submit" name="save_update" id="submit" class="btn btn-flat bg-purple btn-sm " value="Update"/> <a href="rou_userlist.php" class="btn btn-flat bg-gray  ">Close</a>
</div> 
</div>     
</form>    
</div>
</div>
</div>
<div class="col-md-4">
<div class="row">
<div class="col-md-12">
<div class="box box-solid">
<div class="box-header">
<h3 class="box-title">History </h3>
</div>
<!-- /.box-header -->
<div class="box-body" >
<?php if($_SESSION['access']=='1'){echo read_activity($aid,'USR','A');}else{echo read_activity($aid,'USR','U');}?>
</div>
</div>
</div>
</div>
</div>
</div>

<?php include('../layout/quick.php');?>     
</section>
<!-- /.main content -->    
</div>

<?php 
include('../layout/footer.php');
?>
<!-- page script -->
<script type="text/javascript">
$(document).ready(function () {  
var email = new LiveValidation('email');
email.add(Validate.Presence);
email.add(Validate.Email);
<?php if(get_fild_data('tbl_setting','1','sval')==0){?>    
var name = new LiveValidation('name');
name.add(Validate.Presence);
<?php }else{ ?>
var bname = new LiveValidation('bname');
bname.add(Validate.Presence);    
<?php } ?>    
var acess = new LiveValidation('acess');
acess.add(Validate.Presence);
var ibrid = new LiveValidation('ibrid');
ibrid.add(Validate.Presence);    
});    
    
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
            
reader.onload = function (e) {
$('#profile-img-tag').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#profile-img").change(function(){
readURL(this);
});
$("#profile-img-tag").click(function() {
$("#profile-img").click();
});    
</script>    
<!-- /page script -->
</html>    