$(function () {
    $('.img_code img')[0].onclick = function () {
        {
            $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
        }
    }

    var pwd = "";
    var len = 0;
    // type=tel input框
    var $inputs = $(".surface-ipt input");
    $("#secret_num").on("input", function () {
        if (!$(this).val()) {   //无值
        }
        if (/^[0-9]*$/g.test($(this).val())) {  //有值且只能是数字（正则）
            pwd = $(this).val().trim();
            len = pwd.length;
            for (var i in pwd) {
                $inputs.eq(i).val(pwd[i]);
            }
            $inputs.each(function () {  //将有值的当前input 后面的所有input清空
                var index = $(this).index();
                if (index >= len) {
                    $(this).val("");
                }
            })
            if (len === 6) {
                //执行确定操作
                $("#secret_num_auth").click()
            }

        } else {    //清除val中的非数字，返回纯number的value
            var arr = $(this).val().match(/\d/g);
            try {
                $(this).val($(this).val().slice(0, $(this).val().lastIndexOf(arr[arr.length - 1]) + 1));
            } catch (e) {
                //清空
                $(this).val("");
            }
        }
    })

    $(".oe_login_form").submit(function () {
        $(this).ajaxSubmit({
            success: function (data) { // data 保存提交后返回的数据，一般为 json 数据
                if (data) {
                    var jsonResult = JSON.parse(data)
                    if (jsonResult.code == 1) {
                        $('.alert_msg').html('')
                        $('.alert_msg').css({})
                        $('#secretNum').modal('show');
                        $('#secret_num_clear').click();
                    } else if (jsonResult.code == 2) {
                        $('.alert_msg').html('<p class="alert-danger">验证码不正确!</p>')
                        $('.alert_msg').css({'padding': '6px 0', 'text-align': 'center'})
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 3) {
                        $('.alert_msg').html('<p class="alert-danger">账号或者密码错误!</p>')
                        $('.alert_msg').css({'padding': '6px 0', 'text-align': 'center'})
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 4) {
                        window.location.href = window.location.origin + "/web";
                    } else if (jsonResult.code == 5) {
                        $('.field-img-code').css('display', '')
                        $('.alert_msg').html('<p class="alert-danger">你的账号超过登录失败限制次数，请输入验证码!</p>')
                        $('.alert_msg').css({'padding': '6px 0', 'text-align': 'center'})
                    } else if (jsonResult.code == 6) {
                        var show_msg = '<p class="alert alert-danger">密码输入错误累计已达' + jsonResult.failed_times + '次，' + jsonResult.locked_times + '次后将锁定!</p>'
                        $('.alert_msg').html(show_msg)
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 7) {
                        var show_msg = '<p class="alert alert-danger">你输入的账号不存在</p>'
                        $('.alert_msg').html(show_msg)
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 8) {
                        var show_msg = '<p class="alert alert-danger">你的账号已被锁定，请联系管理员解除！</p>'
                        $('.alert_msg').html(show_msg)
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 9) {
                        var show_msg = '<p class="alert alert-danger">参数配置错误，请联系系统管理员！</p>'
                        $('.alert_msg').html(show_msg)
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else if (jsonResult.code == 10) {
                        var show_msg = '<p class="alert alert-danger">' + jsonResult.msg + '</p>'
                        $('.alert_msg').html(show_msg)
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    } else {
                        $('.alert_msg').html('<p class="alert-danger">服务器错误，请稍后重试!</p>')
                        $('.alert_msg').css({'padding': '6px 0', 'text-align': 'center'})
                        $('.img_code img')[0].src = '/jdg/base/image/code?v=' + Math.ceil(Math.random() * 1000000)
                    }
                }
            }
        });
        return false;
    });
    $("#secretNum").on("shown.bs.modal", function () {
        var u = navigator.userAgent;
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if (isiOS) {
            $('.modal-body').html('<input type="text" name="secret_num" t-att-value="secret_num"\n' +
                'id="secret_num"\n' +
                'class="form-control" autocapitalize="off"/><div class="secret_alert_msg"\n' +
                'style="padding: 10px 45px;letter-spacing: 1px;text-align: center;"></div>')
            $('#secret_num_auth').css('display', 'inline-block')
        }
        $('#secret_num').focus();
    });
    $("#secret_num_auth").click(function () {
        $('.secret_alert_msg').html('<p class="alert alert-danger">令牌验证中</p>')
        var login = $("input[name='login']").val()
        var psd = $("input[name='password']").val()
        var csrf_token = $("input[name='csrf_token']").val()
        var secret_num = $('#secret_num').val()
        $.ajax({
            type: "POST",
            url: "/web/login/opt/auth",
            data: {
                login: login,
                password: psd,
                csrf_token: csrf_token,
                secret_num: secret_num,
            },
            dataType: "json",
            success: function (data) {
                if (data) {
                    if (data.code == 1) {
                        $('.secret_alert_msg').html('<p class="alert alert-danger">令牌验证成功</p>')
                        window.location.href = window.location.origin + "/web";
                    }
                    else if (data.code == 2) {
                        $('.secret_alert_msg').html('<p class="alert alert-danger">令牌错误!请重新输入</p>')
                    }
                }

            }
        });
    })
    $('#secret_num_clear').click(function () {
        var input_list = $('.surface-ipt').children()
        for (var i = 0; i < input_list.length; i++) {
            input_list[i].value = ''
        }
        $('#secret_num').val('')
        $('#secret_num').focus()
    })
})