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 () {
    $("#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]).{4,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 register(_this) {
    //要求用户勾选用户协议
    var getCheckStatus = $("#agreementBtnre").is(":checked");
    if(getCheckStatus == true){
        $(_this).removeAttr("disabled")
    }else{
        alert("请勾选用户协议");
        return false;
    }
    let userName = $("#userName").val();
    let valCode = $("#valCode").val();
    let memMobile = $("#memMobile").val();
    let passwd = $("#passwd").val();
    let passwdRe = $("#passwdRe").val();
    var data = {
        userName: userName,
        memMobile:memMobile,
        valCode:valCode,
        passwd:md5(passwd),
        memRegisterSource: 1,
        /*wxId: $("#wxId").val(),
        memNickname: $("#memNickname").val(),
        openId: $("#openId").val(),
        memHeadimg: $("#memHeadimg").val(),
        subscribe: $("#subscribe").val()*/
    }
    if (userName == "") {
        alert('请输入用户名');
        return;
    } else {
        if (!isName(userName)) {
            alert('请输入2-11位数字、字母、汉字的用户名');
            return;
        }
    }
    if(memMobile == ""){
        alert("请输入手机号");
        return;
    }else{
        if(!isMobile(memMobile)){
            alert("手机号格式输入有误，请重新输入");
            return ;
        }
    }
    if(passwd == ""){
        alert("请输入密码");
        return;
    }else{
        if (!isPass(passwd)) {
            alert("密码格式有误，请输入8位包含数字、字母和符号的密码");
            return;
        }
    }
    if(passwdRe == ""){
        alert("请输入确认密码");
        return;
    }else{
        if(passwdRe !== passwd){
            alert("密码与确认密码输入不一致");
            return;
        }
    }

    if(valCode == ""){
        alert("请输入验证码");
        return;
    }
    data.passwd = md5(passwd);
    data.operationName = 'signInWithPassword';
    $.ajax({
        type : 'POST',
        url : serverHttp+"/register/mobilReg",
        data:data,
        success : function(res) {
            if (res.code == 200) {
                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 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 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;
    }
}

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