<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
if(!empty($_POST["action"]) and $_POST["action"] == "ajax_call"){ 
    require "../inc/ajax_header.php";
    $state = $_POST["state"];
    $user_id = $_POST["user_id"];
    $action = $_POST["action"];
    $commonFunctions->updateData('sys_users'," WHERE id = '".$_POST['user_id']."' ", "isActive = '".$state."' " ,'');
    exit();
}
require "../inc/header.php";
$msg = Session::get('msg');
Session::set("msg", NULL);
if(!empty($_POST['action']) and $_POST['action'] == "profile"){
	$column = $fileName = "";
    if($_FILES["logo"]){
        $target_dir = "../uploads/admin-setting/users/";
        $target_file = $target_dir . basename($_FILES["logo"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        @getimagesize($_FILES["logo"]["tmp_name"]);
        if(move_uploaded_file($_FILES["logo"]["tmp_name"], $target_file)){
            $column = " logo";
            $fileName = " logo = '".$_FILES["logo"]["name"]."' ";
            $commonFunctions->updateData('sys_users'," WHERE id = '".$_POST['user_id']."' ", $fileName ,'users-card.php?user_id='.$_POST['user_id']);
        }
    }else{
    	Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> User profile picture not saved!</div>');
    	echo "<script>location.href='users-card.php?user_id=".$_POST['user_id']."';</script>";
    }
}
if(!empty($_POST['action']) and $_POST['action'] == "reset_password"){
	if(!empty($_POST["password"])){
		$commonFunctions->updateData('sys_users'," WHERE id = '".$_POST['user_id']."' ", ' password = "'.SHA1($_POST['password']).'" ','users-card.php?user_id='.$_POST['user_id']);
	}else{
		Session::set('msg', '<div class="alert alert-danger alert-dismissible mt-3" id="flash-msg"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Error !</strong> Please enter password and confirm password!</div>');
    	echo "<script>location.href='users-card.php?user_id=".$_POST['user_id']."';</script>";
	}
}
$userData = $commonFunctions->getRowData('sys_users'," WHERE id = '".@$_GET['user_id']."' ");
?>
<link href="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/css/bootstrap4-toggle.min.css" rel="stylesheet">	
    <!-- Page-Title -->
    <div class="row">
        <div class="col-sm-12">
            <div class="page-title-box">
                <div class="row">
                    <div class="col form-row">
                    	<div class="col-sm-9">
                            <h4 class="page-title"><?php echo @$userData['first_name']." ".$userData['father_name']." ".$userData['last_name'];?></h4>
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="javascript:void(0);">Admin Settings</a></li>
                                <li class="breadcrumb-item"><a href="javascript:void(0);">User Details</a></li>
                            </ol>
                        </div>
                        <div class="col-sm-3">
                            <style type="text/css">
                                .toggle{
                                    height: 30px !important;
                                }
                                .toggle > .toggle-group > .btn{
                                    line-height: 1.5 !important;
                                }
                                .toggle.btn {
                                    min-height: 2.0rem;
                                }
                            </style>
                            <?php $toggle_checked = (@$userData['isActive'] == 0)?"checked":"";
                                $state = (@$userData['isActive'] == 0)?1:0;
                            ?>
                            <input type="checkbox" id="actionCheck" <?php echo $toggle_checked; ?> data-toggle="toggle" data-on="Active" data-off="In-Active" data-onstyle="success" data-offstyle="danger" >&nbsp;

                            <a href="users.php?state=edit&user_id=<?php echo $userData['id'];?>"><button type="button" class="btn btn-sm btn-outline-primary">Edit</button></a>&nbsp;
                            <a href="users.php?state=list"><button type="button" class="btn btn-sm btn-outline-secondary">Back to Users List</button></a>
                            <!-- <a href="users.php?state=copy&user_id=<?php echo $userData['id'];?>">
                            	<button type="button"class="btn btn-sm btn-outline-dark">Duplicate</button>
                            </a> -->
                            <!-- <button type="button" class="btn btn-sm btn-outline-danger">Delete</button> -->
                        </div>
                    </div><!--end col-->
                </div><!--end row-->                                                              
            </div><!--end page-title-box-->
        </div><!--end col-->
    </div><!--end row-->
    <!-- end page title end breadcrumb -->

    <div class="row">
        <div class="col-12">
            <div class="card">
            	<div class="card-body">
                    <div class="dastone-profile">
                        <div class="row">
                            <div class="col-lg-4 align-self-center mb-3 mb-lg-0">
                                <div class="dastone-profile-main">
                                    <div class="dastone-profile-main-pic">
                                        <img src="../uploads/admin-setting/users/<?php echo (@$userData['logo'])?$userData['logo']:'default-img.jpg';?>" alt="" height="110" class="thumb-lx rounded" style='max-width:140px;'>
                                        <span class="dastone-profile_main-pic-change">
                                            <i class="fas fa-camera"></i>
                                        </span>
                                        <form enctype="multipart/form-data" method="post" id="fileUploadForm">
                                            <input type="hidden" name="action" value="profile">
                                            <input type="hidden" name="user_id" value="<?php echo $userData['id'];?>">
                                            <input type="file" name="logo" value="" style='display: none;' id="fileUpload">
                                        </form>
                                    </div>
                                    
                                </div>                                                
                            </div><!--end col-->
                            <div class="col-lg-4 ml-auto align-self-center">
                                <div class="dastone-profile_user-detail">
                                    <h5 class="dastone-user-name"><?php echo @$userData['first_name']." ".$userData['father_name']." ".$userData['last_name'];?></h5>
                                    <p class="mt-0 mb-0"><i class="text-secondary font-22 align-middle"></i> <b> Designation / Job Title :</b> <?php echo $commonFunctions->getName($userData['roleid'],'sys_roles','role','id'); ?></p>
                                     
                                </div>
                            </div>
                            <div class="col-lg-4 ml-auto align-self-center">
                                <ul class="list-unstyled personal-detail mb-0">
                                    <li class="mt-2"><i class="las la-phone mr-2 text-secondary font-22 align-middle"></i> <b> phone </b> :  <?php echo (@$userData['contact'])?@$userData['contact']:"-";?></li>
                                     
                                    <li class="mt-2"><i class="las la-envelope text-secondary font-22 align-middle mr-2"></i> <b> Email </b> :   <a href="mailto:<?php echo (@$userData['email'])?@$userData['email']:"-";?>"><?php echo (@$userData['email'])?@$userData['email']:"-";?></a></li>                                                  
                                </ul>                                               
                            </div><!--end col-->
                            
                             
                        </div><!--end row-->
                    </div><!--end f_profile-->                                                                                
                </div>
            </div> 
        </div><!--end col-->
    </div><!--end row-->
    <div class="pb-4">
        <ul class="nav-border nav nav-pills mb-0" id="pills-tab" role="tablist">
            <li class="nav-item">
                <a class="nav-link active" id="user_details_tab" data-toggle="pill" href="#user_details">User Details</a>
            </li> 
            <li class="nav-item">
                <a class="nav-link" id="user_security_tab" data-toggle="pill" href="#security">Security</a>
            </li> 
        </ul>        
    </div><!--end card-body-->
    <style type="text/css">
    	label.form-control1 {display: block;}
    	label.label-title {font-weight: 600;}
    </style>
    <div class="row">
        <div class="col-12">
        	<?php echo !empty($msg)?$msg:''; ?>        
            <?php echo !empty($msg1)?$msg1:''; ?>      
            <div class="tab-content" id="pills-tabContent">
                <div class="tab-pane fade show active" id="user_details" role="tabpanel" aria-labelledby="user_details_tab">
                    <div class="row" >
                        <div class="col-lg-6 col-xl-6">
                            <div class="card">
                                <div class="card-header">
                                    <div class="row align-items-center">
                                        <div class="col-11">                      
                                            <h4 class="card-title text-left">Personal Information</h4>                      
                                        </div><!--end col-->
                                        <div class="col-1">                      
                                            <a href="users.php?state=edit&user_id=<?php echo $userData['id'];?>"><button type="button" class="text-right btn btn-sm btn-light waves-effect waves-light">Edit</button></a>              
                                        </div><!--end col-->                                                       
                                    </div>  <!--end row-->                                  
                                </div><!--end card-header-->
                                <div class="card-body">                                                                          
                                    <div class="form-group row">                                         
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">First Name</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo @$userData['first_name'];?> </h5>
                                        </div>
                                    </div>
                                    <div class="form-group row">                                         
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">Middle Name</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo @$userData['father_name'];?> </h5>
                                        </div>
                                    </div>
                                    <div class="form-group row">                                         
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">Last Name</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo @$userData['last_name'];?> </h5>
                                        </div>
                                    </div>
                                    <div class="form-group row">                                 
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">Designation / Job Title</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo $commonFunctions->getName($userData['roleid'],'sys_roles','role','id'); ?> </h5>
                                        </div>
                                    </div>                                          
                                </div>                                            
                            </div>
                            <!-- Personal Information Card - End --->
                            
                        </div>
                        <div class="col-lg-6 col-xl-6">
                            <!-- Contact Information Personal Card - End --->
                            <div class="card">
                                <div class="card-header">
                                    <div class="row align-items-center">
                                        <div class="col-11">                      
                                            <h4 class="card-title text-left">Contact Information <label class="col-xl-3 col-lg-3 mb-lg-0 align-self-center">( Personal )</label></h4>                      
                                        </div><!--end col-->
                                        <div class="col-1">                      
                                            <!-- <a href="users.php?state=edit&user_id=<?php echo $userData['id'];?>"><button type="button" class="text-right btn btn-sm btn-light waves-effect waves-light">Edit</button></a> -->              
                                        </div><!--end col-->                                                       
                                    </div>  <!--end row-->                                  
                                </div><!--end card-header-->
                                <div class="card-body">                                                                          
                                    <div class="form-group row">                                         
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">Phone Number</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo @$userData['phone'];?> </h5>
                                        </div>
                                    </div>
                                     
                                    <div class="form-group row">                                         
                                        <label class="col-xl-3 col-lg-3 text-left mb-lg-0 align-self-center">Email Address</label>
                                        <div class="col-lg-9 col-xl-8">
                                            <h5 class="card-title"> <?php echo @$userData['email'];?> </h5>
                                        </div>
                                    </div>                                                   
                                </div><!--end card-body-->
                            </div><!--end card-->
                            
                        </div>
                    </div>
                </div>
                 
                <div class="tab-pane" id="security" role="tabpanel" aria-labelledby="user_security_tab">
                    <div class="card">
                		<div class="card-body">
                        	<form id="reset-password-form" action="" method="post">
                        		<input type="hidden" name="user_id" id="user_id" value="<?php echo $userData['id'];?>" >
                        		<input type="hidden" name="action" value="reset_password">
                        		<div class="form-row">
                            		<div class="col-lg-2"></div>
                            		<div class="col-lg-8">
                            			<div class="form-row">
                                			<div class="form-group searchForm col-md-6">
                                				<label for="password" class="">Password</label>
                                				<input type="password" maxlength="128" name="password" id="password" value="" autofocus="autofocus" class="form-control" placeholder="Password" required="">
                                			</div>
                                			<div class="form-group searchForm col-md-6">
                                				<label for="cnf_password" class="">Confirm Password</label>
                                				<input type="text" maxlength="128" name="cnf_password" id="cnf_password" value="" autofocus="autofocus" class="form-control" placeholder="Confirm Password" required="">
                                			</div>
                                			<div class="form-group searchForm col-md-12" style="text-align: center;">
                                				<br>
                                				<button type="submit" class="btn btn-primary"> Save </button>
                                				<br>
                                				<br>
                                				<br>
                                			</div>
                                		</div>
                            		</div>
                            		<div class="col-lg-2"></div>
                            	</div>
                        	</form>
                    	</div><!--end row-->    
                    </div>
                </div>

            </div>
        </div>
    </div>

        
<?php require "../inc/footer.php"; ?>
<script type="text/javascript" src="https://www.jqueryscript.net/demo/Simple-jQuery-Based-Barcode-Generator-Barcode/jquery-barcode.js"></script>
<script src="https://cdn.jsdelivr.net/gh/gitbrent/bootstrap4-toggle@3.6.1/js/bootstrap4-toggle.min.js"></script>
<script type="text/javascript">
    
  	function generateBarcode(){
	    var value = "<?php echo strtolower(@$userData['barcode_value']); ?>";
	    var btype = "<?php echo strtolower(@$barcode_type); ?>";
	    var renderer = "css";
    	var quietZone = false;
	    
	    var settings = {
	      	output:renderer,
	      	bgColor: "#FFFFFF",
	      	color: "#000000",
	      	barWidth: "2",
	     	barHeight: "50",
	      	moduleSize: "5",
	      	posX: "10",
	      	posY: "20",
	      	addQuietZone: "1"
	    };
    	if (renderer == 'canvas'){
      		clearCanvas();
	      	$("#barcodeTarget").hide();
	      	$("#canvasTarget").show().barcode(value, btype, settings);
    	} else {
      		$("#canvasTarget").hide();
      		$("#barcodeTarget").html("").show().barcode(value, btype, settings);
    	}
  	}
      
  	function showConfig1D(){
	    $('.config .barcode1D').show();
	    $('.config .barcode2D').hide();
  	}
  
  	function showConfig2D(){
	    $('.config .barcode1D').hide();
	    $('.config .barcode2D').show();
  	}
  
  	function clearCanvas(){
	    var canvas = $('#canvasTarget').get(0);
	    var ctx = canvas.getContext('2d');
	    ctx.lineWidth = 1;
	    ctx.lineCap = 'butt';
	    ctx.fillStyle = '#FFFFFF';
	    ctx.strokeStyle  = '#000000';
	    ctx.clearRect (0, 0, canvas.width, canvas.height);
	    ctx.strokeRect (0, 0, canvas.width, canvas.height);
  	}
  
  	$(function(){
    	generateBarcode();
  	});
  	$(".dastone-profile_main-pic-change").click(function(){
  		$("#fileUpload").click();
  	});
  	$("#fileUpload").change(function(){
  		if(confirm("Please make sure do you want to change your profile picture?")){
  			$("#fileUploadForm").submit();
  		}else{
  			return false;
  		}
  	});
</script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.1/jquery.validate.min.js"></script>
<script type="text/javascript">
	var value = $("#password_reg").val();

$.validator.addMethod("checklower", function(value) {
  return /[a-z]/.test(value);
});
$.validator.addMethod("checkupper", function(value) {
  return /[A-Z]/.test(value);
});
$.validator.addMethod("checkdigit", function(value) {
  return /[0-9]/.test(value);
});
$.validator.addMethod("pwcheck", function(value) {
  return /^[A-Za-z0-9\d=!\-@._*]*$/.test(value) && /[a-z]/.test(value) && /\d/.test(value) && /[A-Z]/.test(value);
});
$('#reset-password-form').validate({
  rules: {
    password: {
      minlength: 6,
      maxlength: 30,
      required: true,
      //pwcheck: true,
      checklower: true,
      checkupper: true,
      checkdigit: true
    },
    cnf_password: {
      equalTo: "#password",
    },
  },
  messages: {
    password: {
      pwcheck: "Password is not strong enough",
      checklower: "Need atleast 1 lowercase alphabet",
      checkupper: "Need atleast 1 uppercase alphabet",
      checkdigit: "Need atleast 1 digit"
    }
  },
  highlight: function(element) {
    var id_attr = "#" + $(element).attr("id") + "1";
    $(element).closest('.form-group').removeClass('has-success').addClass('has-error');
    $(id_attr).removeClass('glyphicon-ok').addClass('glyphicon-remove');
    $('.form-group').css('margin-bottom', '5px');
    $('.form').css('padding', '30px 40px');
    $('.tab-group').css('margin', '0 0 25px 0');
    $('.help-block').css('display', '');
  },
  unhighlight: function(element) {
    var id_attr = "#" + $(element).attr("id") + "1";
    $(element).closest('.form-group').removeClass('has-error').addClass('has-success');
    $(id_attr).removeClass('glyphicon-remove').addClass('glyphicon-ok');
    $('#cnf_password').attr('disabled', false);
  },
  errorElement: 'span',
  errorClass: 'validate_cus',
  errorPlacement: function(error, element) {
    x = element.length;
    if (element.length) {
      error.insertAfter(element);
    } else {
      error.insertAfter(element);
    }
  },
  /*submitHandler: function(form) {
  	var user_id = $("#user_id").val();
  	$.ajax({
	    type: 'POST',
	    url: "users-card.php?user_id="+user_id,
	    data: {"user_id":user_id,"action":"reset_password","password":$("#password").val()},
	    success: function(resultData) { 
	    	alert("Save Complete") 
	    }
	});
  	return false;
  }*/
});

$("#actionCheck").change(function(){
    state = "<?php echo $state;?>";
    $.ajax({
        type: 'POST',
        url: "",
        data: {"state":state,"user_id":"<?php echo $userData['id'];?>","action":"ajax_call"},
        success: function(resultData) { 
            window.location.href = "";
        }
    });
});
</script>