var hasPicThumb;
var hasSign;
var userCode;
var i = 1;
var userSignFolderPath = $("#hfUserSignFolderPath").val();
var userImageFolderPath = $("#hfUserImageFolderPath").val();
$(document).ready(function () {
    var size = window.top.allowUploadFileSize;
    var errorUploadSize = window.top.errorExceptionsUploadSize;
    var userCodeL = $("#hfUserCodeL").val();
    $('.upload-btn').change(function (event) {
        if (this.files.length > 0) {
            var file = event.target.files[0];
            var reader = new FileReader();
            var parentDiv = $(this).closest(".parent");
            var id = Number(parentDiv.attr("data-id"));
            reader.readAsDataURL(file);
            reader.onload = function () {
                parentDiv.find(".uplaoded-img").fadeIn().attr('src', reader.result);
                parentDiv.find(".btn-delete").hide();
                parentDiv.find(".btn-save").show();
            };
        }
    });

    $(".no-image-upload").dropzone({
        url: "/file/post",
        init: function () {
            var objDropzone = this;
            objDropzone.on("complete", function (file, done) {
                var parentDiv = $(this.element).closest(".parent");
                var errorElement = parentDiv.find(".file-error");
                if (file.size > size) {
                    errorElement.text(errorUploadSize);
                    objDropzone.removeAllFiles();
                    return;
                }
                else if (!(file.type == "image/png" || file.type == "image/jpeg" || file.type == "image/jpg")) {
                    errorElement.text(window.top.msgExtension1 + " (.jpg , .jpeg , .png) " + window.top.msgExtension2);
                    objDropzone.removeAllFiles();
                    return;
                }
                parentDiv.find(".uplaoded-img").fadeIn().attr('src', file.dataURL);
                parentDiv.find(".btn-delete").hide();
                parentDiv.find(".btn-save").show();
                errorElement.text('');
                objDropzone.removeAllFiles();
                
            });
        },
        paramName: "file",
        uploadMultiple: false,
        maxFiles: 1
    });
    getUser(userCodeL);
});

function changeUserPassword() {
    var pass = $('#txtCurrentPass').val();
    var new_pass = $('#txtNewPass').val();
    var new_pass_confirm = $('#txtConfirmNewPass').val();
    var exceprionText = checkValidation('');
    if (exceprionText == '') {
        if (new_pass != new_pass_confirm) {
            showMsgModal(0, window.top.passwordNotSame);
            return;
        }
        if (!validatePass(new_pass)) {
            showMsgModal(0, window.top.passwordLeast4);
            return;
        }
        ajax({
            loading: true,
            url: "GeneralApi/ChangeUserPassword",
            data: { currentPassword: pass, newPassword: new_pass, confirmNewPassword: new_pass_confirm },
            success: function (result) {
                showMsgModal(2, window.top.savedSuccessfully);
                clearPassword();
            }
        });
    }
    else {
        showMsgModal(0, exceprionText);
    }
}
function getUser(code) {
    ajax({
        loading: true,
        url: "GeneralApi/GetUser",
        data: { code: code },
        success: function (result) {
            if (result != null) {
                hasPicThumb = result.HasPicThumb;
                hasSign = result.HasSign;
                userCode = result.UserCode;
                var userName = (result.UserCode.length > 15 ? result.UserCode.substring(0, 11) + " ... " : result.UserCode)
                    + "/" + (result.UserName.length > 20 ? result.UserName.substring(0, 16) + " ..." : result.UserName);
                $("#lblUserName").text(userName);
                $("#lblUserPost").text(result.IsAdminUser ? window.top.systemAdministrator : window.top.localUser);
                $("#lblTarafHesab").text(result.NameTarafH || "----");
                $("#lblEmail").text(result.UserEmail || "----");
                $("#lblMobile").text(result.UserMobile || "----");
                if (hasPicThumb) {
                    $(".img-user-pic").attr("src", "../" + userImageFolderPath + "/" + result.UserCode + ".jpg");
                    $("[data-id=2]").find(".btn-delete").show();
                    $("[data-id=2]").find(".btn-save").hide();
                }
                if (hasSign) {
                    $(".img-user-sign").attr("src", "../" + userSignFolderPath + "/" + result.UserCode + ".jpg");
                    $("[data-id=3]").find(".btn-delete").show();
                    $("[data-id=3]").find(".btn-save").hide();
                }
            }
        }
    });
}
function saveImage(sender, isDelete) {
    var parentDiv = $(sender).closest(".parent");
    var id = Number(parentDiv.attr("data-id"));
    var fileImage = isDelete ? null : parentDiv.find(".uplaoded-img").attr('src');
    if (isDelete && !confirm(window.top.deleteConfirm))
        return;
    if (id === 3 && isDelete && !hasSign) {
        parentDiv.find(".uplaoded-img").attr('src', "");
        return;
    }
    if (id === 2 && isDelete && !hasPicThumb) {
        parentDiv.find(".uplaoded-img").attr('src', "../img/pages/header/user.png");
        return;
    }
    ajax({
        loading: true,
        url: "GeneralApi/" + (id === 2 ? "SaveUserImage" : "SaveUserSign"),
        data: { fileImage: fileImage },
        success: function (result) {
            i++;
            showMsgModal(2, result.FinalMessage.join("<br />"));
            if (id === 3) {
                parentDiv.find(".uplaoded-img").attr("src", isDelete ? "../img/signature.svg" : "../" + userSignFolderPath + "/" + userCode + ".jpg?version="+i);
                hasSign = !isDelete
            }
            if (id === 2) {
                parentDiv.find(".uplaoded-img").attr("src", isDelete ? "../img/pages/header/user.png" : "../" + userImageFolderPath + "/" + userCode + ".jpg?version=" + i);
                $("#imgUser").attr("src", fileImage || "../img/pages/header/user.png");
                hasPicThumb = !isDelete;
                window.top.$("#imgPerson").attr("src", fileImage || "img/pages/header/user.png");
                window.top.$("#UserImg").attr("src", fileImage || "img/pages/header/user.png");
            }
            if (!isDelete) {
                parentDiv.find(".btn-save").hide();
                parentDiv.find(".btn-delete").show();
            }
            else {
                parentDiv.find(".btn-save").show();
                parentDiv.find(".btn-delete").hide();
            }
        }
    });
}
function changeSection(id) {
    var destinationDiv = $(".parent[data-id=" + id + "]");
    var currentId = Number($(".header-ul>li>label.active").attr("data-id"));
    var currentDiv = $(".parent[data-id=" + currentId + "]");
    if (currentDiv.find(".uplaoded-img").length > 0 && currentDiv.find(".uplaoded-img").attr('src').length > 60) {
        if (!confirm(window.top.ignoreChanges)) {
            return;
        }
        else {
            prevImage = "../" + $("#hfUser" + (currentId === 2 ? "Image" : "Sign") + "FolderPath").val() + "/" + userCode + ".jpg";
            if (currentId === 2) {
                currentDiv.find(".uplaoded-img").attr("src", hasPicThumb ? prevImage : "../img/pages/header/user.png");
                if (hasPicThumb) {
                    currentDiv.find(".btn-delete").show();
                    currentDiv.find(".btn-save").hide();
                }
            }
            else {
                currentDiv.find(".uplaoded-img").attr("src", hasSign ? prevImage : "../img/signature.svg");
                if (hasSign) {
                    currentDiv.find(".btn-delete").show();
                    currentDiv.find(".btn-save").hide();
                }
            }
        }
    }
    else if (id === 1) {
        clearPassword();
    }
    $(".header-ul>li>label").removeClass("active");
    $(".header-ul").find("[data-id=" + id + "]").addClass("active");
    currentDiv.hide();
    destinationDiv.show();
}
function clearPassword() {
    $('#txtCurrentPass').val('');
    $('#txtNewPass').val('');
    $('#txtConfirmNewPass').val('');
}



