<?php

$user                   = new User;
$user_id      = @Session::getUserID();
$user_name = $user->getUser($user_id);
if(isset($_POST["User_ID"]) && isset($_FILES["image_user"])){
    if (md5(sha1(md5($_POST['User_Old_Password']))) != $_POST['Old_Password']) {
        $val = "false";
    }
    else {
        $val ="true";
        $User_ID = $_POST["User_ID"];
        $uploaddir = upload_path.'users/';
        $RET=  $user->setData($_POST)->SaveUploadedPhoto("image_user",$uploaddir.$User_ID.'.jpg');
    }
    die(json_encode($val));
}
?>
<style>
    .input-group-addon.complete {
        background-color:#4c0870;
        color: #ffffff;
    }
    .user_profile_pic {
        text-align: center;
        border-radius: 100%;
        -moz-border-radius: 100%;
        -webkit-border-radius: 100%;
        width: 210px;
        height: 210px;
        border: 5px solid #4c0870;
        overflow: hidden;
        margin-left: 26px;
        background: #fafafa;
    }
    .user_profile_pic img {
        width: 188px;
        height: 188px;
        margin-top: 6px;
        border-radius: 100%;
        -moz-border-radius: 100%;
        -webkit-border-radius: 100%;
        overflow: hidden;
        -webkit-transition: all .3s ease-in-out;
        -moz-transition: all .3s ease-in-out;
        transition: all .3s ease-in-out;
    }
    #changeprofileUploadPhoto{
        width:25%;
        overflow:hidden;
        position:relative;
    }
    #changeprofileUploadPhoto{
        margin-left:92px;
        margin-top:20px;
        background-color:#4c0870 !important;
        color:#fff;
        width:initial;
    }
    #changeprofileUploadPhoto > input,#changeprofileUploadPhoto > input{
        position:absolute;
        left:0px;
        top:0px;
        width:100%;
        height:100%;
        opacity:0.01;
        cursor:pointer;
    }
</style>
<?php
$filename ='uploads/users/'.$user_id.'.jpg';
if (file_exists(dirname(__FILE__).'/../../'.$filename)) {
} else {
    $filename='img/profiles/avatar.jpg';
}
?>
<div class="modal fade stick-up " id="profile_User" tabindex="-1" role="dialog" aria-hidden="true" style="width: 75%;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_profile');?> </span></h5>
            </div>
            <div class="modal-body">
                <form id="changeprofilePostForm" method="POST" onsubmit="return false">
                    <div class="row">
                        <div class="col-md-4 b-r b-dashed b-grey">
                            <div class="col-md-4 profile-col">
                                <div class="user_profile_pic">
                                    <div class="profile-border">
                                        <img id="changeprofileUploadImagePreview" src="<?php echo $filename?>" alt="">
                                    </div>
                                </div>
                                <button class="btn btn-default" id="changeprofileUploadPhoto"><?php print CommonLanguage::getMessage('Upload');?><input type="file" data-target="#changeprofileUploadImagePreview" onchange="window.changeprofileCurrentFileUpload = event.target.files;ValidateForchangeprofilePhoto(this,'Picture')" name="image_user" value="Upload" onload="$(this).data('loaded', 'loaded');"></button>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <input type="hidden" id="User_ID" name="User_ID"value="<?php echo $user_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="profile_User_Name" value="<?php echo $user_name['User_Name'];?>"  class="form-control " > 
                            </div>
                            <br>
                            <br>
                        </div>
                        <div class="col-md-6">
                            <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_password1"/>
                                <input type="password"  placeholder="<?php print CommonLanguage::getMessage('enter_pass');?>" id="profile_User_Password1" name="User_Old_Password" class="form-control " > 
                            </div>
                        </div>
                    </div>
                    <br>
                    <br>
                    <br>
                    <br>
                    <div class="modal-footer " >
                        <div class="pull-right">
                            <button class="btn btn-default" id="profileCloseUser"   > <?php print CommonLanguage::getMessage('Close');?> </button>
                            <button class="btn btn-success disabled" id="profileSubmitUser"  > <?php print CommonLanguage::getMessage('Save');?>   </button>
                        </div>
                    </div>
                    </div>
            </div>
            </form>
    </div>
</div>
<script>
    var change_photo = <?php echo json_encode(CommonLanguage::getMessage('change_photo'));?>,
        Submitted_Successfully = <?php echo json_encode(CommonLanguage::getMessage('Submitted_Successfully'));?>,
        wrong_user_password = <?php echo json_encode(CommonLanguage::getMessage('wrong_user_password'));?>;
    $(function() {
        $("body").addClass("NoSearch")
        $("body .search-link[data-toggle='search']").html("<i class='pg-search'></i>Click here to <span class='bold'>search</span>")
        $("#profileSubmitUser").on("click",function(){
            if(ValidateForchangeprofilePhoto){
                var user_pass_temp = $("#profile_User_Password1").val();
                $("#changeprofileValidationAlert").remove();
                //     $("#changeprofileValidationAlert").remove();
                var AlertBox = $('<div id="changeprofileValidationAlert" class="WizardValidationAlert alert"></div>');
                AlertBox.insertAfter($("#profile_User").find(".modal-dialog .modal-header").find("h5"))
                $("#changeprofileisBusy").remove();
                //                $("#profile_User").append('<div id="changeprofileisBusy" 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>');
                $("#changeprofileValidationAlert").addClass("alert-danger").empty().html("<span>Change Profile Picture</span>");

                // $("#changeprofileisBusy").animate({opacity:1});
                var Data = new FormData(document.getElementById("changeprofilePostForm"));
                if($("#changeprofileUploadPhoto > input").prop("files").length > 0){
                    $.each(window.changeprofileCurrentFileUpload, function(key, value){
                        Data.append(key, value);
                    });
                }
                //            $("#profile_User_Password1").on("keyup",function(){
                //                DisableSubmit()
                //            })
                $.ajax({
                    url:"Views/HRM/changeprofile.php",
                    method:"POST",
                    data:Data,
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    complete:function(v1,v2){
                        var samePass = (v1.responseJSON);
                        if(v2 == 'success'){
                            if(samePass =="true"){
                                $("#changeprofileisBusy").remove();
                                $("#changeprofileValidationAlert").addClass("alert-success").empty().html(Submitted_Successfully);
                                setTimeout(function(){
                                    $("#profile_User").modal('hide')
                                    $("#changeprofileValidationAlert").hide()
                                },1000)
                            }
                            else{
                                $("#changeprofileisBusy").remove();
                                $("#changeprofileValidationAlert").addClass("alert-danger").empty().html(wrong_user_password);
                            }
                        }
                    }
                });
                //       $("#profile_User input").val("");
                //
            }
            else{
                $("#changeprofileisBusy").remove();
                $("#changeprofileValidationAlert").addClass("alert-danger").empty().html(change_photo);
            }
        });
    });
    $("#profile_User_Password1").on("keyup",function(){
        DisableChangeSubmit()
    })
    $("#profileCloseUser").on("click",function(){
        //        $("#profile_User input").val("");
        $("#profile_User ").modal("hide");
        // $("#changeprofileUploadImagePreview").attr("src","img/profiles/avatar.jpg");
    })
    function DisableChangeSubmit(){
        var IsValidated=true;
        if($("#profile_User_Password1").val() == ""){
            IsValidated = false;
        }
        if(!IsValidated){
            $("#profileSubmitUser").addClass("disabled")}
        else{$("#profileSubmitUser").removeClass("disabled")}
    }

    function ValidateForchangeprofilePhoto(input){
        var IsFileOK = false;
        if (input.files && input.files[0]){
            var FileName = $.trim(input.files[0].name)
            var FileType = FileName.substring(FileName.lastIndexOf("."));
            if(FileType){
                FileType = FileType.toLowerCase();
                if(FileType == ".png" || FileType == ".jpg" || FileType == ".jpeg"){IsFileOK = true;}
            }
            $("#changeprofileUploadImagePreview,#NewUserNoPreviewFileUploadIcon").hide()
            $("#NewUserFilewarning2").remove()
            if(IsFileOK == false){
                $("#NewUserNoPreviewFileUploadIcon").show()
                $("<span id='NewUserFilewarning2' style='color:#f00;display: block;text-align: center;'> [File not Supported]</span>").insertAfter($(input))
                return false;
            }else{
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("#changeprofileUploadImagePreview").attr('src', e.target.result).show();
                }
                reader.readAsDataURL(input.files[0]);
                return true;
            }
        }
        return false;
    }
</script>