var trId = "";
 

function EditEmployee(Id) {    
    
    location.href = "/HR/EmployeeDetail/" + Id;
}

function SaveCopiedPermission() {

    ClearErrorMsg('divCopyPermission');

    if (checkRequired('divCopyPermission', 'error-msg') == false) {
        return;
    }

    if ($('#sUserId').val() == $('#destinationUserId').val()) {
        return SetErrorMsg("源用户和目标客户不能相同", 'divCopyPermission');
    }

    synConfirm("确定要复制授权吗吗？", function () {
        $.ajax({
            url: "/HR/CopyPermission",
            type: 'POST',
            cache: false,
            data: { sUserId: $('#sUserId').val(), dUserId:$('#destinationUserId').val()  },
            success: function (result) {
                if (result.code == 0) {
                    $('#divCopyPermission').dialog('close');
                }
                else {
                    alert(result.message);
                }

            },
            error: function (e) {
                alert('复制授权失败，请稍后再试。');
            }
        });
    });


}

function DeleteEmployee(Id) {

    synConfirm("确定删除吗？", function () {
        $.ajax({
            url: "/HR/DeleteEmployee",
            type: 'POST',
            cache: false,
            data: { Id: Id },
            success: function (result) {
                if (result.code == 0) {
                    location.href = '/HR/Employee';
                }
                else {
                    alert(result.message);
                }

            },
            error: function (e) {
                alert('该员工已产生业务数据，无法删除。');
            }
        });
    });

    return false;
}

function ResetUserPassword(UserId, EmployeeName) {
    $("#UserId").val(UserId);
    $('#newPassword').val("");

    $('#divPassword').dialog('open')

    return false;
}

function CopyPermission(UserId, EmployeeName) {
    $("#sUserId").val("");
    $('#destinationUserId').val(UserId);
    $('#destinationUserName').html(EmployeeName);

    $('#divCopyPermission').dialog('open');

    return false;
}

function SavePassword() {
    console.log(1);
    ClearErrorMsg("divPassword");

    if (checkRequired('divPassword', 'error-msg') == false) {
        return;
    }
    console.log(1);
    $.ajax({
        url: "/HR/ResetUserPassword",
        type: 'POST',
        cache: false,
        data: { UserId: $("#UserId").val(),Password:$("#newPassword").val() },
        success: function (result) {
            console.log(3);
            if (result.code == 0) {
                $('#divPassword').dialog('close');                
            }
            else {
                SetErrorMsg(result.message, "divPassword");
            }

        },
        error: function (e) {
            console.log(4);
            SetErrorMsg("密码重置失败", "divPassword"); 
        }
    });
}

$(function () {

    setOperationBtns();   

    TdRowOverOut("tEmployee");   

    $('#divPassword').dialog({
        width: 420,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });

    $('#divPermission').dialog({
        width: 920,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });

    $('#divCopyPermission').dialog({
        width: 480,
        autoOpen: false,
        modal: true,
        buttons: {
        }
    });
    
    $('#btnNew').click(function () {
        location.href = "/HR/EmployeeDetail/";
    });

    $("#divPermission .chkSingle").click(function () {
        determineCheckAll();
    }); 


    $("#divPermission #chkAll").click(function () {
        $("#divPermission .chkSingle").prop("checked", $(this).prop("checked"));    
        determineCheckAll();
    }); 


    $("#divPermission #chkGroupAll").click(function () {
        $(this).parent().parent().find(".chkSingle").prop("checked", $(this).prop("checked"));
    }); 

    $("#btnSavePermission").click(function () {

        synConfirm("确定要保存吗？", function () {

            var permissions = [];
            $("#divPermission .chkSingle").each(function () {
                if ($(this).prop("checked")) {                   
                    permissions.push(this.id);                                    
                }
            });

            $.ajax({
                url: "/HR/UpdateUserPermission",
                type: 'POST',
                cache: false,
                data: { UserId: $("#UserId").val(), Permission: JSON.stringify(permissions) },
                success: function (result) {
                    if (result.code == 0) {
                        closePermission();
                       // alert("用户权限修改成功！")
                    }
                    else {                       
                        SetErrorMsg(result.message, "divPermission");                         
                    }
                },
                error: function (e) {
                    SetErrorMsg("保存失败", "divPermission");                     
                }
            });
        });
   
    });
 
});

 
function showPermission() {
    $('#divPermission').dialog('open');
}

function closePermission() {
    $('#divPermission').dialog('close');
}

function EditPermission(UserId) {
    $("#UserId").val(UserId);
    $.ajax({
        url: "/HR/GetUserPermission",
        type: 'GET',
        cache: false,
        data: { UserId: UserId },
        success: function (result) {
            if (result.code == 0) {
               
                $("#divPermission .chkSingle").prop("checked", false);
                for (var i in result.data) {
                   
                    $("#divPermission").find("input[id=" + result.data[i] + "]").prop("checked", true);
                }
                determineCheckAll();
                showPermission();
            }
            else {
                alert(result.message);
            }

        },
        error: function (e) {
            alert("查找员工信息失败");
        }
    });
}

function determineCheckAll() {
    var allChecked = true;
    var groupChecked = true;

    //groupPermission
    $("#divPermission .groupPermission").each(function (i,v) {

        groupChecked = true;

        $(v).find(".chkSingle").each(function (x,y) {
            if (!$(y).prop("checked")) {
                allChecked = false;
                groupChecked = false;
                return false;
            }
        });

        $(v).find("#chkGroupAll").prop("checked", groupChecked);

    });


    $("#divPermission #chkAll").prop("checked", allChecked);
}
