if (window !== top) {
    top.location.href = location.href;
}
layui.use(['request', 'form', 'button', 'yaml'], function() {
    var $ = layui.jquery,
        request = layui.request,
        form = layui.form,
        button = layui.button,
        yaml = layui.yaml;

    var URL = yaml.load("config/cool.config.yml").admin.url;
    var PKI;

    //刷新验证码
    window.captcha = function() {
        $.get(request.getAdminServerUrl() +'/guest/captcha?' + Math.random(), function (res) {
            if (res.success) {
                $('[name="key"]').val(res.data.key);
                $("#captchaImage").attr('src', res.data.image);
            }
        });
    };

    //init
    layer.ready(function() {
        layui.data('server',
            null);
        layui.data('server', {
            key: 'url',
            value: URL
        });
        layui.data('server', {
            key: 'login',
            value: location.href
        });
        //初始化 RSA_PUBLIC_KEY
        $.get(request.getAdminServerUrl() +'/guest/pki', function (res) {
            if (res.success) {
                PKI = res.data;
            }
        });
        //始化化验证码初
        window.captcha();
    });

    //30秒自动刷新
    //setInterval(function () {window.captcha();}, 30 * 1000);

    //提交
    form.on('submit(login)', function (data) {
        var btn = button.load({elem: '.login'});
        data.field.captcha = $('#getRegisterCaptcha').val();
        data.field.password = encrypt(data.field.password);
        request.http({
            url: '/login',
            data: data.field,
            type: "post",
            dataType: 'json',
            sendToken: false,
            success: function (res) {
                btn.stop(function() {
                    if (res.success) {
                        layui.data('server', {
                            key: 'accessToken',
                            value: {name:'accessToken', value: res.data.accessToken}
                        });
                        layui.data('server', {
                            key: 'permissions',
                            value: res.data.permissions
                        });
                        button.load({elem: '.login', time: 200,
                            done: function () {
                                location.href = "index.html"
                            }
                        });
                    } else {
                        layer.msg(res.msg, {icon:2, time: 1500});
                        captcha();
                    }
                })
            }
        });
        return false;
    });

    //清空input
    window.empty = function(name) {
        $('[name="' + name + '"]').val('');
    };

    //RSA加密
    window.encrypt = function (data) {
        if (PKI) {
            var r = new JSEncrypt();
            r.setPublicKey('-----BEGIN PUBLIC KEY-----' + PKI + '-----END PUBLIC KEY-----');
            return r.encrypt(JSON.stringify(data));
        } else {
            layer.msg("password rsa error");
            return false;
        }
    };
});
