var clock = '';
var nums = 60;
var btn;

function sendCode(thisBtn) {
    var email = document.getElementById("email").value;
    if (email.length == 0) {
        alert("邮箱不可为空！");
        return;
    }
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (!reg.test(email)) {
        alert("邮箱格式错误！");
        return;
    }
    btn = thisBtn;
    btn.disabled = true;
    btn.value = '重新发送(' + nums + ')';
    clock = setInterval(doLoop, 1000);

    $.ajax({
        type: "POST",
        url: "/user/get_verification_code",
        data: JSON.stringify({ email: email }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
}
function doLoop() {
    nums--;
    if (nums > 0) {
        btn.value = '重新发送(' + nums + ')';
    } else {
        clearInterval(clock);
        btn.disabled = false;
        btn.value = '发送验证码';
        nums = 60;
    }
}
function signIn() {
    var email = document.getElementById("email").value;
    if (email.length == 0) {
        alert("邮箱不可为空！");
        return;
    }
    var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    if (!reg.test(email)) {
        alert("邮箱格式错误！");
        return;
    }
    var code = document.getElementById("code").value;
    if (code.length != 6) {
        alert("验证码错误！");
        return;
    }
    $.ajax({
        type: "POST",
        url: "/user/login",
        data: JSON.stringify({ email: email, code: code }),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success : function(tokenData) {
            location.href="/manage/dashboard";
        },
        error: function (xhr, textStatus, error) {
            var msg = JSON.parse(xhr.responseText);
            alert(msg.error);
        }
    });
}