//编辑基本资料
function editUser(userId) {
    //清空数据
    $("#infoform")[0].reset();
    $.ajax({
        url: "user/editUser",
        data: {id: userId},
        dataType: "json",
        success: function (data) {
            data.userPass = "";
            $("#infoModal").modal('show');
            $("#infoform").setForm(data);

        }
    });
}

//更新基本资料
function updateUser() {
    var flag = $("#infoform").valid();
    var dataArr = $("#infoform").serializeArray();
    if (dataArr && flag) {
        var param = {};
        $.each(dataArr, function (i, obj) {
            param[obj.name] = obj.value;
        });

        $.ajax({
            url: "user/updateUser",
            type: "post",
            contentType:"application/json;charset=utf-8",
            data: JSON.stringify(param),
            success: function (data) {
                if (data.success) {
                    $("#infoModal").modal('hide');
                    swal({title: "修改成功", text: "数据已修改", type: "success", confirmButtonColor: "#1ab394"});
                } else {
                    swal({title: "修改失败", text: data.msg, type: "error", confirmButtonColor: "#1ab394"});
                }
            }
        });
    }
}

//编辑账户密码
function editAcount(userId) {
    $.validator.addMethod("checkPwd", function (value, element, params) {
        return this.optional(element) || /^(?:(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])).{8,16}$/.test(value);
    }, "密码至少包一个大写字母、一个小写字母及一个数字，长度至少8位");

    $("#accountform").validate({
        rules: {
            password: {
                required: true,
                checkPwd: true
            },
            repassword: {
                required: true,
                rangelength: [8, 15],
                equalTo: "#password"
            }
        },
        messages: {
            password: {
                required: '请输入新密码',
                regexPassword: "密码至少包一个大写字母、一个小写字母及一个数字，长度至少8位",
            },
            repassword: {
                required: "请输入确认密码",
                rangelength: "确认密码不能小于8个字符",
                equalTo: "两次输入密码不一致"
            }
        }
    });
    $("#accountform")[0].reset();
    $("#accountform").find("input[name='id']").val(userId);
    $("#accountModal").modal('show');
}

//更新账户密码
function updateAccount() {
    var flag = $("#accountform").valid();
    var dataArr = $("#accountform").serializeArray();
    if (flag && dataArr) {
        var param = {};
        $.each(dataArr, function (i, obj) {
            param[obj.name] = obj.value;
        });

        $.ajax({
            url: "user/updatePsw",
            type: "post",
            contentType:"application/json;charset=utf-8",
            data: JSON.stringify(param),
            success: function (data) {
                if (data.success) {
                    $("#accountModal").modal('hide');
                    swal({title: "修改成功", text: "数据已修改", type: "success", confirmButtonColor: "#1ab394"});
                } else {
                    swal({title: "修改失败", text: data.msg, type: "error", confirmButtonColor: "#1ab394"});
                }
            }
        });
    }
}
//渲染top中信息图标
window.onload=function() {
    var userId=$("#p_userId").html();
    /*getNoReadMessage(userId);*/
}