(function () {
    var page = {
        headColor: '#ffffff',
        init: function ($page) {
            var path = config.webapi;
            learun.http.get(path + "Learun/EducationalAdministration/EmpRegister/registerbutton",
                {}, (res) => {
                    console.log(res);
                    if (res.info == "True") {
                        var button = $page.find('#RegisterBtn');
                        button.show();
                    }
                });
            //获取ACIp
            learun.http.get(path + "weixinapi/GetACIp",
                {}, (res) => {
                    learun.storage.set("Ip", res.data.Ip);
                    learun.storage.set("ACIp", res.data.ACIp);
                    learun.storage.set("ACIp2", res.data.ACIp2);
                });
            $page.find('img').attr('src', config.web + "Login/GetImg?code=applogo");
            $page.find('#loginBtn').on('tap', function () {
                var account = $('#account').val();
                var password = $('#password').val();

                if (account === "") {
                    learun.layer.warning('用户名不能为空！', function () { }, '提示', '关闭');
                } else if (password === "") {
                    learun.layer.warning('密码不能为空！', function () { }, '提示', '关闭');
                } else {
                    var data = {
                        username: account,
                        password: $.md5(password)
                    };
                    var postdata = {
                        token: '',
                        loginMark: learun.deviceId(),// 正式请换用设备号
                        data: JSON.stringify(data)
                    };

                    learun.layer.loading(true, "正在登录，请稍后");
                    learun.http.post(path + "learun/adms/user/login", postdata, (res) => {
                        learun.layer.loading(false);
                        if (res === null) {
                            learun.layer.warning('无法连接服务器,请检测网络！', function () { }, '提示', '关闭');
                            return;
                        }

                        if (res.code === 200) {
                            var logininfo = {
                                account: account,
                                token: res.data.baseinfo.token,
                                date: learun.date.format(new Date(), 'yyyy-MM-dd hh:mm:ss')
                            };
                            learun.storage.set('logininfo', logininfo);
                            learun.storage.set('userinfo', res.data);
                            $('#account').val('');
                            $('#password').val('');
                            learun.tab.go('workspace');
                            if (learun.storage.get("Ip") != null && learun.storage.get("ACIp") != "") {
                                //Ip上网
                                sendToAc(learun.storage.get("ACIp"), 'logon', learun.storage.get("Ip"), account, '');
                            }
                            if (learun.storage.get("Ip") != null && learun.storage.get("ACIp2") != "") {
                                //Ip上网
                                sendToAc(learun.storage.get("ACIp2"), 'logon', learun.storage.get("Ip"), account, '');
                            }
                        } else {
                            learun.layer.warning(res.info, function () { }, '提示', '关闭');
                        }
                    });
                }

            });
            $page.find('#RegisterBtn').on('tap',
                function () {
                    learun.nav.go({ path: 'EducationalAdministration/EmpRegister', title: '教师注册', type: 'right' });
                });
            $page.find('#bindweixinBtn').on('tap',
                function () {
                    location.href = "/weixinlogin.html";
                });

        }
    };
    return page;
})();
