<?php
$user_name = Session::getUserDetails();

?>
<div class="modal fade stick-up " id="Change_Password" tabindex="-1" role="dialog" aria-hidden="true" style="width: 40%;margin: 0 auto;">
    <div class="modal-dialog ">
        <div class="modal-content">
            <div class="modal-header clearfix text-left">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-14"></i></button>
                <h5><span class="semi-bold"> <?php print CommonLanguage::getMessage('change_password');?></span></h5>
            </div>
            <div class="modal-body">
                <form id="EditUserPostForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-12" style="margin-left:8px">
                            <input type="hidden" id="User_Emp_ID" name="User_ID"value="<?php echo $user_name['User_ID'];?>">
                            <input type="hidden" id="User_Password" name="Password"value="<?php echo $user_name['User_Password'];?>">
                            <input type="hidden" id="User_Employee_ID" name="Employee_ID"value="<?php echo $user_name['Employee_ID'];?>">
                            <input type="hidden" id="User_Role_ID" name="Role_ID"value="<?php echo $user_name['Role_ID'];?>">
                            <div class="input-group ">
                                <span class="input-group-addon complete">
                                    <i class="fa fa-user"></i>
                                </span>
                                <input type="text" name="User_Name" placeholder="<?php print CommonLanguage::getMessage('username');?>" id="Edit_User_Name" value="<?php echo $user_name['User_Name'];?>"  class="form-control " > 
                            </div>
                        </div>
                        <br>
                        <br>
                        <div class="col-md-12 oldPass" >
                            <div class="input-group ">
                                <span class="input-group-addon complete">
                                    <i class="fa fa-key"></i>
                                </span>
                                <input type="hidden" name="Old_Password" value="<?php echo $user_name['User_Password']; ?>" id="old_user_password"/>
                                <input type="password"  placeholder="<?php print CommonLanguage::getMessage('enter_old_password');?>" id="Edit_User_Password1" name="User_Old_Password" class="form-control " >
                            </div>                                                                                         
                        </div>
                        <br>
                        <br>
                        <div class="col-md-12">
                            <div class="input-group ">
                                <span class="input-group-addon complete">
                                    <i class="fa fa-key"></i>
                                </span>
                                <input type="password"  placeholder="<?php print CommonLanguage::getMessage('enter_new_password');?>" id="Edit_User_Password2" name="User_Password" class="form-control " > 
                            </div>
                        </div>
                        <br>
                        <br>
                        <div class="col-md-12">
                            <div class="input-group ">
                                <span class="input-group-addon complete">
                                    <i class="fa fa-key"></i>
                                </span>
                                <input type="password"  placeholder="<?php print CommonLanguage::getMessage('reenter_new_password');?>" id="Edit_User_Password3" name="Confirm_Password" class="form-control " > 
                            </div>
                        </div>
                        <br>
                        <br>
                        <br>
                        <br>
                        <div class="modal-footer " >
                            <div class="pull-right">
                                <button class="btn btn-default" id="EditCloseUser"   > <?php print CommonLanguage::getMessage('Close');?> </button>
                                <button class="btn btn-success disabled " id="ChangeSubmitUser"  > <?php print CommonLanguage::getMessage('Save');?>   </button>
                        </div>
                    </div>
                    </div>
                </form>
        </div>
    </div>
</div>
</div>
<script>
    var wrong_user_password = <?php echo json_encode(CommonLanguage::getMessage('wrong_user_password'));?>,
        Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully'));?>;
    $(function() {
        $("body").addClass("NoSearch")
        $("body .search-link[data-toggle='search']").html("<i class='pg-search'></i>Click here to <span class='bold'>search</span>")
        $("#ChangeSubmitUser").on("click",function(){
            if(validatePass()){
                var user_pass_temp = $("#Edit_User_Password1").val();
                $("#EditUserValidationAlert").remove();
                var AlertBox = $('<div id="EditUserValidationAlert" class="WizardValidationAlert alert"></div>');
                AlertBox.insertAfter($("#Edit_User").find(".modal-dialog .modal-header").find("h5"))
                $("#EditUserisBusy").remove();
                $("#Change_Password").append('<div id="EditUserisBusy" style="position:absolute;left:0;right:0;top:0px;height:100%;background:rgba(255,255,255,0.9);z-index:9999;opacity:0"><img class="image-responsive-height demo-mw-50" src="img/demo/progress.svg" alt="Progress" style="position: absolute;left: 50%;top: 50%;margin: -25px 0 0 -25px;" /></div>');
                $("#EditUserisBusy").animate({opacity:1});
                var Data = new FormData(document.getElementById("EditUserPostForm"));
                $.ajax({
                    url:"HRMAjax/ChangePassword",
                    method:"POST",
                    data:Data,
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    complete:function(jqXHR, textStatus){
                        //console.log(jqXHR.responseJSON);

                        var samePass = (jqXHR.responseJSON);
                        //  console.log(samePass)

                        if(textStatus == 'success'){


                            if(samePass =="OK"){


                                $("#EditUserisBusy").remove();
                                $("#EditUserValidationAlert").addClass("alert-success").empty().html(Submitted_Successfully);
                                setTimeout(function(){
                                    $("#Change_Password").modal('hide')
                                },1500)
                                setTimeout(function(){
                                    $("#EditUserValidationAlert").hide()},1000)
                            }
                            else{
                                //$("#EditUserValidationAlert").addClass("alert-danger").empty().html("<span>Passwords Does not Match</span>");
                                $("#EditUserValidationAlert").addClass("alert-danger").empty().html(wrong_user_password);
                                $("#EditUserisBusy").remove();

                            }
                        }
                        else{
                            $("#EditUserisBusy").remove();
                        }
                    }
                    //$("#EditUserValidationAlert").addClass("alert-danger").empty().html("<span>Failed to submit, please try again</span>");

                })
                $("#Edit_User_Password1,#Edit_User_Password2,#Edit_User_Password3").val("");


            }
        });
        $("#Edit_User_Password1").on("keyup",function(){
            DisableSubmitEdit()
        })
        $("#EditCloseUser").on("click",function(){

            $("#Change_Password").modal("hide");

        })
    })
    function DisableSubmitEdit(){
        var IsValidated=true;
        if($("#Edit_User_Password1").val() == $("#User_Password").val()){
            IsValidated = false;
        }
        if(!IsValidated){
            $("#ChangeSubmitUser").addClass("disabled")}
        else{$("#ChangeSubmitUser").removeClass("disabled")}
    } 
    function validatePass(){
        var password = $("#Edit_User_Password2").val() ;
        var confpassword = $("#Edit_User_Password3").val() ;
        if(password != confpassword) {
            alert("Passwords do no match");
            return false;
        }
        return true;
    }
</script>