
$(function (){
    $.backstretch("../../thmz/images/backgrounds/back3.jpg");
    $('.login-form input[type="text"], .login-form input[type="password"], .login-form textarea').on('focus', function() {
    	$(this).removeClass('input-error');
    });
    
    $('.login-form').on('submit', function(e) {
    	$(this).find('input[type="text"], input[type="password"], textarea').each(function(){
    		if( $(this).val() == "" ) {
    			e.preventDefault();
    			$(this).addClass('input-error');
    		}
    		else {
    			$(this).removeClass('input-error');
    		}
    	});
    	
    });
    $('#loginButton').click(function(e) {
        login();
    });

    $(document).keyup(function(event){
        if(event.keyCode ==13){
            login();
        }
    });

    // //禁止滚动条
    // $(document.body).css({
    //     "overflow-x":"hidden",
    //     "overflow-y":"hidden"
    // });
});



function login() {
    $.ajax({
        type: "POST",
        contentType: "application/json;charset=UTF-8",
        url: "/thmz/login",
        data: JSON.stringify({"userCode":$("#form-username").val(),"password":$("#form-password").val()}),
        dataType: 'json',
        success: function(result) {
            if(result.code == 0) {
                $('.alert-danger').removeClass('in').addClass('hide');
                $('.alert-success').removeClass('hide').addClass('in');
                $("#successMessage").text(result.message);
                localStorage.setItem("token",result.token);
                localStorage.setItem("userID",result.userID);
                window.location.href='/thmz/menu/view'
            } else {
                if($("#errorMessage").length==0){
                    $(".form-bottom").append("<div class='alert alert-danger alert-dismissable hide' role='alert'>" +
                        "<button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>" +
                        "&times;</span></button><strong id='errorMessage' style='text-align-all: center'>这里是错误信息</strong></div>");

                }
                $("#errorMessage").text(result.message);
                $('.alert-danger').removeClass('hide').addClass('in');
            }
        }
    });
}