$('#loginCatcha').click(function () {
    $(this).attr('src', "/user/login/captchalogin.html" + '?t=' + Math.random());
});
$('#regCatcha').click(function () {
    $(this).attr('src', "/user/login/captcharegister.html" + '?t=' + Math.random());
});
// 显示登录窗口
function showLogin(type='login') {
    if(type=='login'){
        $('.login-model').show();
    }else{
        $('.reg-model').show();
    }
    layer.open({
        type: 1,
        shade: 0.8,
        title: false, //不显示标题
        content: $('.login-box'),
        end: function () {
            $('.login-model').hide();
            $('.reg-model').hide();
        }
    });
}
// 切换登录注册
function tabLogin(type='login') {
    if(type == 'login'){
        $('.login-model').show();
        $('.reg-model').hide();
    }else{
        $('.login-model').hide();
        $('.reg-model').show();
    }
}

function login(){
    let data = {
        phone: $("#phone_login").val(),
        password: $("#password_login").val(),
        captcha: $("#code_login").val()
    };
    // 校验表单数据 手机号码校验
    if (data.phone === '' || !checkPhone(data.phone)) {
        layer.alert('请输入正确的手机号码', {icon: 2});
        return false;
    }
    if (data.password === '') {
        layer.alert('请输入密码', {icon: 2});
        return false;
    }
    if (data.captcha === '') {
        layer.alert('请输入验证码', {icon: 2});
        return false;
    }
    $.post("/user/login/login.html", data, function (ret, status) {
        if (ret.code === 1) {
            location.reload()
        } else {
            layer.alert(ret.msg, {icon: 2});
            $("#loginCatcha").click();
        }
    });
}
function checkPhone(phone) {
    let reg = /^1[3456789]\d{9}$/;
    return reg.test(phone);
}
// 发送短信
function sendSms() {
    let phone = $("#phone_reg").val();
    let captcha = $("#code_reg").val();
    if (phone === '' || !checkPhone(phone)) {
        layer.alert('请输入正确的手机号码', {icon: 2});
        return false;
    }
    if (captcha === '') {
        layer.alert('请输入验证码', {icon: 2});
        return false;
    }
    var load_index = layer.load(1, {
        shade: [0.1,'#fff'] //0.1透明度的白色背景
    });
    $.post("/user/login/sendsms.html", {phone: phone, captcha: captcha}, function (ret, status) {
        layer.close(load_index);
        if (ret.code === 1) {
            $("#send_sms_btn").attr('disabled', true);
            countDown();
            //layer.alert(ret.msg, {icon: 1});
        } else {
            $("#regCatcha").click();
            layer.alert(ret.msg, {icon: 2});
        }
    });
}
// 发送短信后验证码倒计时
function countDown() {
    let count = 120;
    let timer = setInterval(function () {
        if (count > 0) {
            count--;
            $("#send_sms_btn").text(count + '秒后重发');
            $("#send_sms_btn").attr('disabled', true);
        } else {
            clearInterval(timer);
            $("#send_sms_btn").text('发送短信');
            $("#send_sms_btn").attr('disabled', false);
        }
    }, 1000);
}
// 注册
function register() {
    let data = {
        phone: $("#phone_reg").val(),
        password: $("#password_reg").val(),
        captcha: $("#code_reg").val(),
        smscode: $("#formSmsCode").val()
    };
    // 校验表单数据 手机号码校验
    if (data.phone === '' || !checkPhone(data.phone)) {
        layer.alert('请输入正确的手机号码', {icon: 2});
        return false;
    }
    if (data.password === '') {
        layer.alert('请输入密码', {icon: 2});
        return false;
    }
    if (data.captcha === '') {
        layer.alert('请输入验证码', {icon: 2});
        return false;
    }
    if (data.smscode === '') {
        layer.alert('请输入短信验证码', {icon: 2});
        return false;
    }
    $.post("/user/login/register.html", data, function (ret, status) {
        if (ret.code === 1) {
            layer.alert(ret.msg, {
                icon: 1,
                time: 1500,
                end: function () {
                    location.reload()
                }
            });
        } else {
            layer.alert(ret.msg, {icon: 2});
            $("#regCatcha").click();
        }
    });
}

function outlogin(){
    $.post("/user/login/logout.html", {}, function (ret, status) {
        location.reload()
    });
}