/**
 * [OElove] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * $ Last update 2020/11/30 by OE $
*/
$(function(){
    
    //切换验证码...
    $(document).on("click", "#img_checkcode", function(){
        $(this).attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=70&height=30&num=6&"+Math.random());
    });
   
    //帐号登录...
    layui.use('form', function(){
        $form = layui.form;
        $form.on('submit(loginform)', function(data){
            $obj_id = data.elem.getAttribute("id");
            if (!$("#"+$obj_id).hasClass("forbid_submit")) {
                _loginPost($obj_id);
                return false;
            }
            return false;
        });
    });
    
    //回车键...
    $(document).keydown(function(event){
        if (event.keyCode == 13){
            _loginPost("but_submit_login");
        }
    });
    
});

//提交登录...
function _loginPost($obj_id) {
    
    $loginname = $("#loginname").val();
    $password = $("#password").val();
    $checkcode = $("#checkcode").val();
    if ($loginname.length == 0) {
        layer.msg("请输入登录帐号");
        return false;
    }
    if ($password.length == 0) {
        layer.msg("请输入登录密码");
        return false;
    }
    if ($checkcode.length == 0) {
        layer.msg("请输入验证码");
        return false;
    }
    
    $("#"+$obj_id).addClass("forbid_submit");
    $("#"+$obj_id).find("span").show();
    
    $.ajax({
        type: "POST",
        url: _ROOT_PATH + "index.php?m=admin&c=login",
        cache: false,
        data: {
            a:"loginpost", loginname:$loginname, password:$password, 
            checkcode:$checkcode, r:Math.random()
        },
        dataType: "json",
        success: function($data) {
            $json = eval($data);
            $response = $json.response;
            $result = $json.result;
            if ($response == "1") {
                layer.msg("登录成功");
                setTimeout(function(){
                    window.top.location.href = _ROOT_PATH+"index.php?m=admin&c=index";
                }, 500);                           
            }
            else {
                if ($result.length > 0) {
                    layer.msg($result);
                }
                else {
                    layer.msg("登录失败，请检查帐号密码是否正确");
                }
                //验证码重新更新...
                $("#img_checkcode").attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=70&height=30&num=6&"+Math.random());
                $("#"+$obj_id).removeClass("forbid_submit");
                $("#"+$obj_id).find("span").hide();
            }
        },
        error: function() {
            layer.msg("系统繁忙，请稍后再试");
            $("#"+$obj_id).removeClass("forbid_submit");
            $("#"+$obj_id).find("span").hide();
        }
    });   
    
}

