$(document).ready(function() {
    // var header = {
    //     "Content-Type": "application/x-www-form-urlencoded",
    //     "token": localStorage.getItem('token'),
    // }
    // ajax("/login/userInfo", "GET", header, function(res) {
    //     console.log(res);
    //     alert('保存成功!')
    //     window.location.href = 'index.html'
    // }, function(res) {
    //     console.log(res);
    // })

    var code = ''; //接受验证码
    var reg_phone = /^1[3|4|5|6|7|8][0-9]\d{8}$/;
    $("#code-btn").click(function() {
        console.log(111111111);
        var count = 60;
        var phone = $("#phone").val(); //获取手机号

        if (!reg_phone.test(phone)) { //验证手机是否符合格式
            alert("请填写正确的手机号");
            return false;
        }
        //开始计时  
        $("#code-btn").attr('disabled', 'disabled');
        $("#code-btn").html("倒计时" + count + "秒");
        var timer = setInterval(function() {
            count--;
            $("#code-btn").html("倒计时" + count + "秒");
            if (count == 0) {
                clearInterval(timer);
                $("#code-btn").attr("disabled", false); //启用按钮  
                $("#code-btn").html("重新发送验证码");
                code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效 
            }
        }, 1000);
        var header1 = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        var params1 = { phoneNumber: phone }

        //获取验证码接口
        ajax('/login/codeLogin', "GET", header1, params1, function(res) {
            console.log(res);
        }, function(res) {})
    });
    $("#code_btn").click(function() {
        console.log(111111111);
        var count = 60;
        var phone = $("#phone1").val(); //获取手机号
        console.log(phone);
        if (!reg_phone.test(phone)) { //验证手机是否符合格式
            alert("请填写正确的手机号");
            return false;
        }
        //开始计时  
        $("#code_btn").attr('disabled', 'disabled');
        $("#code_btn").html("倒计时" + count + "秒");
        var timer = setInterval(function() {
            count--;
            $("#code_btn").html("倒计时" + count + "秒");
            if (count == 0) {
                clearInterval(timer);
                $("#code_btn").attr("disabled", false); //启用按钮  
                $("#code_btn").html("重新发送验证码");
                code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效 
            }
        }, 1000);
        var header1 = {
            "Content-Type": "application/x-www-form-urlencoded"
        }
        var params1 = { phoneNumber: phone }

        //获取验证码接口
        ajax('/login/codeLogin', "GET", header1, params1, function(res) {
            console.log(res);
        }, function(res) {})
    });

});

//获取用户VIP信息判断是否是VIP
function setPersonal() {
    alert('请完善个人信息!')
}
$(".btnSave").click(function() {
    //姓
    var inputSurname = $("#surname").val();
    console.log(inputSurname);
    //名
    var inputUserName = $("#userName").val();
    console.log(inputUserName);
    //专业(null)
    var inputSpecialty = $("#specialty").val();
    console.log(inputSpecialty);
    //学校
    var inputSchool = $("#school").val();
    console.log(inputSchool);
    //手机号
    var inputPhone = $("#phone").val();
    console.log(inputPhone);
    //邮箱
    var inputEmailAddress = $("#emailAddress").val();
    console.log(inputEmailAddress);
    //验证码
    var verificationCode = $("#verificationCode").val();
    console.log(verificationCode);
    var reg_phone = /^1[3|4|5|6|7|8][0-9]\d{8}$/;
    if (inputSurname.length === 0) {
        alert("姓氏不能为空!")
    } else if (inputUserName.length === 0) {
        alert("名字不能为空!")
    } else if (inputSchool.length === 0) {
        alert("学校不能为空!")
    } else if (inputPhone.length === 0) {
        alert("手机号码不能为空!")
    } else if (!reg_phone.test(inputPhone)) {
        alert("请填写正确的手机号");
        return false;
    } else if (verificationCode.length === 0) {
        alert("验证码不能为空!")
    } else if (inputEmailAddress.length === 0) {
        alert("邮箱不能为空!")
    } else if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(inputEmailAddress) == false) {
        alert("邮箱格式不正确,请重新填写!")
    } else {
        var header = {
            "Content-Type": "application/json",
            "token": localStorage.getItem('token'),
        }
        var params = JSON.stringify({
            code: verificationCode,
            emailAddress: inputEmailAddress,
            id: localStorage.getItem("userId"),
            img: null,
            phoneNumber: inputPhone,
            specialty: inputSpecialty,
            school: inputSchool,
            surname: inputSurname,
            userName: inputUserName
        })

        ajax("/login/updateUsers", "PUT", header, params, function(res) {
            console.log(res);
            alert('保存成功!')
            window.location.href = 'index.html'
        }, function(res) {
            alert(res.responseJSON.msg)
            console.log(res);

        })
    }
})
$(".btnSave1").click(function() {
    //姓
    var inputSurname = $("#surname1").val();
    console.log(inputSurname);
    //名
    var inputUserName = $("#userName1").val();
    console.log(inputUserName);
    //专业(null)
    var inputSpecialty = $("#specialty1").val();
    console.log(inputSpecialty);
    //学校
    var inputSchool = $("#school1").val();
    console.log(inputSchool);
    //手机号
    var inputPhone = $("#phone1").val();
    console.log(inputPhone);
    //邮箱
    var inputEmailAddress = $("#emailAddress1").val();
    console.log(inputEmailAddress);
    //验证码
    var verificationCode = $("#verificationCode1").val();
    console.log(verificationCode);
    var reg_phone = /^1[3|4|5|6|7|8][0-9]\d{8}$/;
    if (inputSurname.length === 0) {
        alert("姓氏不能为空!")
    } else if (inputUserName.length === 0) {
        alert("名字不能为空!")
    } else if (inputSchool.length === 0) {
        alert("学校不能为空!")
    } else if (inputPhone.length === 0) {
        alert("手机号码不能为空!")
    } else if (!reg_phone.test(inputPhone)) {
        alert("请填写正确的手机号");
        return false;
    } else if (verificationCode.length === 0) {
        alert("验证码不能为空!")
    } else if (inputEmailAddress.length === 0) {
        alert("邮箱不能为空!")
    } else if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(inputEmailAddress) == false) {
        alert("邮箱格式不正确,请重新填写!")
    } else {
        var header = {
            "Content-Type": "application/json",
            "token": localStorage.getItem('token'),
        }
        var params = JSON.stringify({
            code: verificationCode,
            emailAddress: inputEmailAddress,
            id: localStorage.getItem("userId"),
            img: null,
            phoneNumber: inputPhone,
            specialty: inputSpecialty,
            school: inputSchool,
            surname: inputSurname,
            userName: inputUserName
        })

        ajax("/login/updateUsers", "PUT", header, params, function(res) {
            console.log(res);
            alert('保存成功!')
            window.location.href = 'index.html'
        }, function(res) {
            alert(res.responseJSON.msg)
            console.log(res);

        })
    }
})