var mobileInt;

function timerMobileValCode(intDiff, sendBefoBtnId, sendafterBtnId,
                            waitsecondId) {
    mobileInt = window.setInterval(function () {
        var second = 60;
        if (intDiff > 0) {
            second = intDiff;
        } else if (intDiff == 0) {
            $("#" + sendafterBtnId).hide();
            $("#" + sendBefoBtnId).show();
            window.clearInterval(mobileInt);
        }
        if (second <= 9)
            second = '0' + second;
        $('#' + waitsecondId).html(second);
        intDiff--;
    }, 1000);
}

$(function () {
    var paramData = getHref();
    if (paramData.type == "pass") {
        $("#login-pass").trigger("click");
    }
    $("#sendcode").click(function () {
        var mobile = $("#memMobile").val();
        if (mobile == "") {
            alert("请填写手机号");
            return;
        }
        if (!(/^1[3456789]\d{9}$/.test(mobile))) {
            alert("请填写正确的手机号");
            return false;
        }
        sendYzm(mobile);
    });
})

function isMobile(mobile) {
    var matchrs = mobile.match(/^((13[0-9])|(16[0-9])|(14[5,7])|(15[^4,\D])|(17[0,6,7,8])|(18[0-9]))\d{8}$/);
    return matchrs;
}

function isPass(pass) {
    var reg = pass.match(/^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/);
    return reg;
}
function isName(userName) {
    var reg = /^[\u4E00-\u9FA5A-Za-z0-9]{2,11}$/;
    var isJudge = reg.test(userName);
    return isJudge;
}

function sendYzm(memUserId) {
    $.ajax({
        type: 'Get',
        url: serverHttp + "/login/sendMobileYzm",
        data: {
            phone: memUserId
        },
        success: function (res) {
            if (res.code == 200) {
                $("#sendcode").hide();
                $("#djstime").show();
                timerMobileValCode(59, "sendcode", "djstime", "waittime");
            }
        },
        error: function (data) {
            alert("验证码发送失败");
        }
    });
}

function errorWarn(text) {
    $(".commtk").text(text);
    $(".commtk").fadeIn(800);
    $(".commtk").fadeOut(500);
}

function login(_this) {
    //要求用户勾选用户协议
    var getCheckStatus = $("#agreementBtn").is(":checked");
    if(getCheckStatus == true){
        $(_this).removeAttr("disabled")
    }else{
        alert("请勾选用户协议");
        return false;
    }

    //获取白名单
    var getWhitelist = [];
    $.ajax({
        type: 'get',
        url: "/login/getWhitelist",
        async: false,
        success: (res) => {
            if (res.code == 200) {
                getWhitelist = res.data;
            }
        }
    });

    var type = $(_this).attr("statue");
    let valCode = $("#valCode").val();
    let memMobile = $("#memMobile").val();
    let passwd = $("#passwd").val();
    var data = {
        memMobile: memMobile,
        memRegisterSource: 1,
        wxId: $("#wxId").val(),
        memNickname: $("#memNickname").val(),
        openId: $("#openId").val(),
        memHeadimg: $("#memHeadimg").val(),
        subscribe: $("#subscribe").val()
    }
    // else {
        if (type == "code") {
            if (memMobile == "") {
                alert("请填写手机号");
                return;
            }
            if (!(/^1[3456789]\d{9}$/.test(memMobile))) {
                alert("请填写正确的手机号");
                return;
            }
            if(valCode == ''){
                alert("请填写验证码");
                return;
            }
            data.valCode = valCode;
            data.operationName = 'signInWithPhone';
        } else if (type == "pass") {
            if (memMobile == "") {
                alert("请填写用户名");
                return;
            }
            if (passwd == "") {
                alert("请填写密码");
                return;
            }
            if(getWhitelist.indexOf(memMobile) == -1){
                if (!isName(memMobile)) {
                    alert('请输入2-11位数字、字母、汉字的用户名');
                    return;
                }
                if (!isPass(passwd)) {
                    alert('密码格式有误，请输入8位包含数字、字母和符号的密码');
                    return;
                }
            }
            data.passwd = md5(passwd);
            data.operationName = 'signInWithPassword';
        }
        $.ajax({
            type: 'POST',
            url: serverHttp + "/login/mobileLogin",
            data: data,
            success: function (res) {
                if (res.code == 200) {
                    findUserBaseInfo() // 获取用户信息
                    var redirectUrl = $("#redirectUrl").val();
                    if (redirectUrl) {
                        window.location.href = redirectUrl;
                    } else {
                        window.location.href = serverHttp + "/index/index?from=H5";
                    }

                } else {
                    alert(res.message);
                }
            },
            error: function (data) {

            }
        });
    // }

}

function findUserBaseInfo() {
    $.ajax({
        type: 'POST',
        url: "/user/findUserBaseInfo",
        async: false,
        success: (res) => {
            if (res.code == 200) {
                localStorage.setItem('isVip', res.data.isVip)
                // console.log(res.data, 'fdsfdsfd')
            }
        },
        error: function (data) {

        }
    });
}

function gotoys() {
    window.location.href = serverHttp + "/user/yinsi?from=H5&memToken=" + this.memToken;
}

function gotofw() {
    window.location.href = serverHttp + "/user/xieyi?from=H5&memToken=" + this.memToken;
}

function gotouser() {
    window.location.href = serverHttp + "/user/yonghu?from=H5&memToken=" + this.memToken;
}


function toPass(_this) {
    $("#memMobile").attr("placeholder","用户名")
    $("#login-btn").attr("statue", "pass");
    $(_this).hide();
    $(".yanzhengma").hide();
    $("#passwd").show()
    $("#login-code").show();
    $("#register").show();
}

function toCode(_this) {
    $("#memMobile").attr("placeholder","手机号")
    $("#login-btn").attr("statue", "code");
    $(_this).hide();
    $("#passwd").hide();
    $("#register").hide();
    $(".yanzhengma").show()
    $("#login-pass").show();
}

function getHref() {
    var href = window.location.href;
    var arr = []
    var obj = {}
    if (/\?/g.test(href)) {
        var param = href.split("?")[1];
        arr = param.split("&");
        if (arr.length > 0) {
            for (var i = 0; i < arr.length; i++) {
                var item = arr[i];
                obj[item.split("=")[0]] = item.split("=")[1];
            }
        }
    }
    if (obj !== {}) {
        return obj;
    } else {
        return false;
    }
}

$("#register").on("click", function () {
    window.location.href = serverHttp + "/register?from=H5&redirectUrl=" + encodeURIComponent(location.href);
})
