<?php 
define('DOCUMENT_ROOT_CLASS','../');
define('DOCUMENT_ROOT_INC_FOLDER','');
$filepath = realpath(dirname(__FILE__));
require "../inc/header.php";
$msg = Session::get('msg');
Session::set("msg", NULL);
if(!empty($_POST['action']) and $_POST['action'] == "profile"){
	$column = $fileName = "";
    if($_FILES["erp_sys_user_logo"]){
        $target_dir = "../uploads/admin-setting/users/";
        $target_file = $target_dir . basename($_FILES["erp_sys_user_logo"]["name"]);
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        @getimagesize($_FILES["erp_sys_user_logo"]["tmp_name"]);
        if(move_uploaded_file($_FILES["erp_sys_user_logo"]["tmp_name"], $target_file)){
            $column = " erp_sys_user_logo";
            $fileName = " erp_sys_user_logo = '".$_FILES["erp_sys_user_logo"]["name"]."' ";
            $commonFunctions->updateData('sys_erp_adm_user'," 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_erp_adm_user'," WHERE id = '".$_POST['user_id']."' ", ' erp_sys_user_secutiry_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_erp_adm_user'," WHERE id = '".@$_GET['user_id']."' ");
?>
    <!-- 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-10">
                            <h4 class="page-title"><?php echo @$userData['erp_sys_user_name_first']." ".$userData['erp_sys_user_name_middle']." ".$userData['erp_sys_user_name_last'];?></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-2">
                            <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>
                            <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 p-0">
                    <div id="user_map" class="pro-map" style="height: 220px"></div>                                     
                </div><!--end card-body-->                                
                <div class="card-body">
                    <div class="dastone-profile">
                        <div class="row">
                        	<div class="col-lg-5 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['erp_sys_user_logo'])?$userData['erp_sys_user_logo']:'default-img.jpg';?>" alt="" height="110" class="rounded-circle">
                                        <span class="dastone-profile_main-pic-change">
                                            <i class="fas fa-camera"></i>
                                        </span>
                                    </div>
                                    <div class="dastone-profile_user-detail">
                                        <h5 class="dastone-user-name"><?php echo @$userData['erp_sys_user_name_first']." ".$userData['erp_sys_user_name_middle']." ".$userData['erp_sys_user_name_last'];?></h5>                    
                                    </div>
                                </div>                                                
                            </div><!--end col-->
                            
                            <div class="col-lg-7 ml-auto align-self-center">
                            	<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="erp_sys_user_logo" value="" style='display: none;' id="fileUpload">
                            	</form>
                                <ul class="list-unstyled personal-detail mb-0">
                                    <li class=""><i class="las la-phone mr-2 text-secondary font-22 align-middle"></i> <b> Mobile Number </b> : <?php echo (@$userData['erp_sys_org_phone_work'])?@$userData['erp_sys_org_phone_work']:"-";?></li>
                                    <li class="mt-2"><i class="las la-envelope text-secondary font-22 align-middle mr-2"></i> <b> Email </b> : <?php echo (@$userData['erp_sys_user_email_work'])?@$userData['erp_sys_user_email_work']:"-";?></li>
                                    <li class="mt-2"><i class="las la-user text-secondary font-22 align-middle mr-2"></i> <b> UserName </b> : 
                                        <?php echo (@$userData['erp_sys_user_id'])?@$userData['erp_sys_user_id']:"-";?>
                                    </li>                                                   
                                </ul>
                               
                            </div><!--end col-->
                            
                        </div><!--end row-->
                    </div><!--end f_profile-->                                                                                
                </div><!--end card-body-->          
            </div> <!--end card-->

        </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_permission_tab" data-toggle="pill" href="#permission">Permission</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="user_security_tab" data-toggle="pill" href="#security">Security</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" id="user_settings_tab" data-toggle="pill" href="#user_settings">Settings</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="card">
                		<div class="card-body">
                			<div class="form-row">
                				<div class="col-md-6">
                    				<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="border-collapse: collapse; border-spacing: 0px; width: 100%;" role="grid" aria-describedby="datatable-buttons_info">
                                    	<thead>
                                        	<tr role="row" class="even">
	                                            <td tabindex="0" class="sorting_1">Mobile Number 2</td>
	                                            <td style=""><?php echo @$userData['erp_sys_user_mobile2'];?></td>
	                                        </tr>
	                                    </thead>
	                                    <tbody>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Company Name</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_org_name'])?@$userData['erp_sys_org_name']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Job Title</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_job_title'])?@$userData['erp_sys_user_job_title']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Work Phone</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_org_phone_work'])?@$userData['erp_sys_org_phone_work']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Work Phone - Ext</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_org_phone_work_ext'])?@$userData['erp_sys_user_org_phone_work_ext']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="even">
	                                            <td tabindex="0" class="sorting_1">Work eMail</td>
	                                            <td style=""><?php echo (@$userData['erp_sys_user_email_work'])?@$userData['erp_sys_user_email_work']:"-";?></td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Work Fax</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_org_fax'])?@$userData['erp_sys_org_fax']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Work Mobile</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_mobile_work'])?@$userData['erp_sys_user_mobile_work']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <!-- <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Permission</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_secutiry_permission'])?@$userData['erp_sys_user_secutiry_permission']:"-";?>
	                                            </td>
	                                        </tr> -->
	                                    </tbody>
	                                </table>
	                            </div>

	                            <div class="col-md-6">
                    				<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap dataTable no-footer dtr-inline" style="border-collapse: collapse; border-spacing: 0px; width: 100%;" role="grid" aria-describedby="datatable-buttons_info">
                                    	<thead>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">LinkedIn</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_sm_linkedin'])?@$userData['erp_sys_user_id_sm_linkedin']:"-";?>
	                                            </td>
	                                        </tr>
	                                    </thead>
	                                    <tbody>
                                        	
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Twitter</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_sm_twitter'])?@$userData['erp_sys_user_id_sm_twitter']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Facebook</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_sm_facebook'])?@$userData['erp_sys_user_id_sm_facebook']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Instagram</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_sm_instagram'])?@$userData['erp_sys_user_id_sm_instagram']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Skype</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_skype'])?@$userData['erp_sys_user_id_msngr_skype']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Whatsapp Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_whatsapp'])?@$userData['erp_sys_user_id_msngr_whatsapp']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">IMO Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_imo'])?@$userData['erp_sys_user_id_msngr_imo']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Telegram Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_telegram'])?@$userData['erp_sys_user_id_msngr_telegram']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Signal Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_signal'])?@$userData['erp_sys_user_id_msngr_signal']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Botim Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_msngr_botim'])?@$userData['erp_sys_user_id_msngr_botim']:"-";?>
	                                            </td>
	                                        </tr>
	                                        <tr role="row" class="odd">
	                                            <td tabindex="0" class="">Viber Number</td>
	                                            <td style="">
	                                            	<?php echo (@$userData['erp_sys_user_id_messanger_viber'])?@$userData['erp_sys_user_id_messanger_viber']:"-";?>
	                                            </td>
	                                        </tr>
	                                    </tbody>
	                                </table>
	                            </div>
                            </div>

	                    </div>
                	</div>
                </div>
                <div class="tab-pane" id="user_settings" role="tabpanel" aria-labelledby="user_settings">
                    <div class="card">
                		<div class="card-body">
                        	3
                    	</div><!--end row-->    
                    </div>
                </div>
                <div class="tab-pane" id="permission" role="tabpanel" aria-labelledby="permission">
                    <div class="card">
                		<div class="card-body">
                        	2
                    	</div><!--end row-->    
                    </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 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;
  }*/
});
</script>