$(function () {
    // $('.aaa').on('click', function () {
    //     $('.login_p').show()
    // })
    $(window).on('load', function () {
        // $('.login_con_all_0>input').val('')
    })
    $('.login_close_span, .login_p0').on('click', function () {
        $('.inp_zddl_1').prop('checked', false)
        // $('.login_con_all_0>input').val('')
        $('.login_con_all:eq(0)').addClass('sel').siblings('.login_con_all').removeClass('sel')
        $('.login_p').hide()
    })

    $('.span_zddl').on('click', function () {
        $(this).find('input').click()
    })
    $('.span_zddl>input').on('click', function (e) {
        e.stopPropagation()
    })

    $('.more_login').on('click', function () {
        $('.login_con_all.sel').removeClass('sel').siblings('.login_con_all').addClass('sel')
    })

    var that = null
        that_p2 = null,
        user = '',
        pwd = '',
        remb = '',
        btnType = '',
        flag1 = true,
        setIvl = null,
        setTimes = 60,
        login_data = {},
        login_url = ''
    $('.obt_code_span_tag').on('click', function () {
        if (!flag1) return
        that = this
        user = $(that).parent().parent().parent().find('.inp_sjhm_1').val()
        if (user.length != 11 || !/1{1}[3-9]{1}[0-9]{9}/g.test(user)) return LayerAlert1('手机号码格式不正确')
        flag1 = false

        $.ajax({
            url: '/sms?act=smslogin&mobile=' + user,
            method: 'GET',
            dataType: 'json',
            success: function (res) {
                if (res[0] == true) {
                    setTimes = 60
                    $(that).text(setTimes + '秒')
                    setIvl = setInterval(function() {
                        if (setTimes > 1) {
                            setTimes--
                            $(that).text(setTimes + '秒')
                        } else {
                            $(that).text('获取验证码')
                            clearInterval(setIvl)
                            setIvl = null
                            flag1 = true
                        }
                    }, 1000);
                } else {
                    flag1 = true
                    LayerAlert1(res[1])
                }
            },
            error: function (err) {
                flag1 = true
                LayerAlert1('请求接口异常')
            }
        })


    })
    
    $('.login_con_all').on('keyup', function (e) {
        if (e.keyCode == 13) {
            $(this).find('.btn_login_0').click()
        }
    })

    $('.btn_login_0').on('click', function () {
        // that = this
        that_p2 = $(this).parent().parent()
        btnType = $(this).attr('data-type')
        if (btnType == 'mm') {
            if ($(that_p2).find('.inp_user_1').val() == '') {
                LayerAlert1('请输入账号')
                return false
            }
            if ($(that_p2).find('.inp_pwd_1').val() == '') {
                LayerAlert1('请输入密码')
                return false
            }
            login_url = '/login'
            login_data = {
                username: $(that_p2).find('.inp_user_1').val(),
                password: $(that_p2).find('.inp_pwd_1').val(),
                memory: $('.inp_zddl_1').prop('checked') == true ? 1 : 0
            }
        } else if (btnType == 'dx') {
            if ($(that_p2).find('.inp_sjhm_1').val() == '') {
                LayerAlert1('请输入手机号码')
                return false
            }
            if ($(that_p2).find('.inp_yzm_1').val() == '') {
                LayerAlert1('请输入验证码')
                return false
            }
            login_url = '/smslogin'
            login_data = {
                tel: $(that_p2).find('.inp_sjhm_1').val(),
                code: $(that_p2).find('.inp_yzm_1').val()
            }
        }
        $.ajax({
            url: login_url,
            method: 'POST',
            data: login_data,
            headers: {
            	"X-CSRF-TOKEN": csrf
            },
            dataType: 'json',
            success: function (res) {
                console.log(res);
                if (res[0] == true) {
                    history.go(0)
                } else {
                	csrf = res[2]
                    LayerAlert1(res[1])
                }
            },
            error: function (err) {
            	if (err.status == 419) {
            		LayerAlert1('缓存已过期请刷新页面')
            	} else {
                	LayerAlert1('请求接口异常')
            	}
            }
        })
    })

    function LayerAlert1(con) {
        layui.use('layer', function () {
            var layer = layui.layer
            layer.open({
                title: '提示',
                content: con ? con : '',
                area: '300px',
                offset: '200px',
                btn: '确定'
            })
        })
    }
})