$(function (){
    /**
     * 查询用户信息
     */
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/user-info",
        dataType: 'json',
        headers:{'Accept': 'application/json','Authorization':'Bearer '+ localStorage.getItem("token")},
        success: function(result) {
            if(result=='401' || result==401){
                window.location.href='/thmz/login/view'
                return;
            }
            if(result.code == 0) {
                $("#userName").val(result.data.userName);
                $("#email").val(result.data.email);
                $("#email2").val(result.data.email);
                $("#phoneNumber").val(result.data.phoneNumber);
                //successMesage(result);
            } else {
                errorMesage(result);
            }
        }
    });

    /**
     * 提交按钮点击事件
     */
    $("#send").on("click",function (t) {
        var validatorResult = validator.checkAll($("#profile_form"));
        if(!validatorResult){
            return;
        }
        $.ajax({
            type: "POST",
            contentType: "application/json;charset=UTF-8",
            url: "/thmz/modify-user",
            dataType: 'json',
            data: JSON.stringify({"userName":$("#userName").val(),"email":$("#email").val(),"phoneNumber":$("#phoneNumber").val()}),
            headers:{'Accept': 'application/json','Authorization':'Bearer '+ localStorage.getItem("token")},
            success: function(result) {
                if(result=='401' || result==401){
                    window.location.href='/thmz/login/view'
                    return;
                }
                if(result.code == 0) {
                    successMesage(result);
                    profileInfo();
                } else {
                    errorMesage(result);
                }
            }
        });
    });

    /**
     * 取消按钮点击事件
     */
    $("#cancel").on("click",function (t) {
        profileInfo();
    });
    //数据校验
    init_validator();
});


/**
 * 修改按钮点击事件
 */
function profilePreview() {
    $("#titleInfo").removeClass('hide').addClass('in');
    $("#buttonDiv").removeClass('hide').addClass('in');
    $("#emailDiv").removeClass('hide').addClass('in');
    $("#userName").removeAttr("readonly").attr("required","true");
    $("#email").removeAttr("readonly").attr("required","true");
    $("#phoneNumber").removeAttr("readonly").attr("required","true");
}


/**
 * 用户信息查询
 */
function profileInfo() {
    $("#titleInfo").removeClass('in').addClass('hide');
    $("#buttonDiv").removeClass('in').addClass('hide');
    $("#emailDiv").removeClass('in').addClass('hide');
    $("#userName").attr("readonly","readonly").removeAttr("required");
    $("#email").attr("readonly","readonly").removeAttr("required");
    $("#phoneNumber").attr("readonly","readonly").removeAttr("required");
}








