<%
    local ver = require("xiaoqiang.XQVersion").webVersion
    local XQSysUtil = require "xiaoqiang.util.XQSysUtil"
    if not XQSysUtil.getInitInfo() then
        luci.http.redirect(luci.dispatcher.build_url("web", "init","guidetoapp"))
    end
    local XQFunction = require("xiaoqiang.common.XQFunction")
    local XQTopology = require("xiaoqiang.module.XQTopology")
    local httpHost = luci.http.getenv("HTTP_HOST")
    if string.find(httpHost, "miwifi.com") then
        local topo = XQTopology.topologicalGraph()
        local leafs = topo["leafs"]
        if leafs and #leafs > 0 then
            luci.http.redirect(luci.dispatcher.build_url("web", "topo"))
        end
    end
    local router_name = XQFunction.getRouterName()
    local isBinded = (XQSysUtil.getPassportBindInfo() and 'true') or 'false'
    local logType = 1
    if logtype then
        logType = tonumber(logtype) or 1
    end

    local remote_addr = luci.http.getenv("REMOTE_ADDR")
    local mac = luci.sys.net.ip4mac(remote_addr)

    local hardware = string.lower( XQSysUtil.getHardware() )
    local color = XQSysUtil.getColor()
    local routerSrc = resource.."/web/img/topograph/".."router_"..hardware.."_".. color..".png"
    local lang = XQSysUtil.getLang()
%>
<%include("web/inc/head")%>
    <title><%:小米路由器%></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <link href="<%=resource%>/web/css/bc.css?v=<%=ver%>" rel="stylesheet">
    <link href="<%=resource%>/web/css/login.css?v=<%=ver%>" rel="stylesheet">
    <link href="<%=resource%>/web/css/guide.css?v=<%=ver%>" rel="stylesheet">

</head>
<body>
<div id="doc">
    <div id="hd">
    </div>
    <div id="bd">
        <div class="mod-login" id="admin-PC"style="display:none;">
            <div class="title">
                欢迎使用小米路由器
            </div>
            <!-- <div class="pic">
                <img src="<%=routerSrc %>" >
            </div> -->
            <!-- <div class="rtname">
                <%=router_name%>
            </div> -->
            <div class="detail">
                下载小米WiFi APP管理路由器
            </div>
            <div class="download">
                <div class="code"></div>
            </div>
            <div class="tip">
                使用小米WiFi APP免除记密码烦恼
            </div>
            <form id="rtloginform" name="rtloginform" method="post" class="form-login">
                <div class="form-item">
                    <span class="v"><input id="password" class="ipt-text" type="password" name="router_password" autocomplete="off" placeholder="<%:请输入路由器管理密码%>" reqMsg="<%:请输入路由器管理密码%>"></span>
                    <em class="t"></em>
                </div>
                <div class="form-contral">
                    <a id="btnRtSubmit" class="btn-login"></a>
                </div>
            </form>
            <div class="mobile-ft">
                <p>
                    <a target="_blank" href="http://www1.miwifi.com"><%:官方网站%></a>
                </p>
            </div>
        </div>

        <div class="mod-guide mod-guide-admin mod-guide-phone" id="admin-mob" style="display:none;">
                    <p class="tit font18"><%:使用小米WiFi APP管理您的路由器%></p>
                    <div class="hr"></div>
                    <ul class="info pic-line clearfix">
                        <li>
                            <div class="ico wifi"></div>
                            <div>易用</div>
                            <div class="des">全新小米WiFi APP 极简操作，享受生活</div>
                        </li>
                        <li>
                            <div class="ico safe"></div>
                             <div>安全</div>
                            <div class="des">7x24小时安全防护 一键拉黑蹭网老王</div>
                        </li>
                        <li>
                            <div class="ico gamespeedup"></div>
                            <div>加速</div>
                            <div class="des">提高带宽，游戏加速 团战胜人一筹</div>
                        </li>
                        <li>
                            
                            <div class="ico money"></div>
                            <div>赚钱</div>
                            <div class="des">分享WiFi赚点钱 欢迎老王来蹭网</div>
                        </li>
                    </ul>

                    <a href="" id="install" class="btn btn-primary btn-block mgtp"><span><%:现在安装%></span></a>
                    <a href="miwifi://tab?tab=0&subtab=0" id="open" class="btn btn-primary btn-block"><span><%:我已安装，现在打开%></span></a>
                    <div class="notice"><span><i></i>不支持手机端网页管理路由器</span></div>
                </div>
    </div>
    <%include("web/inc/footermini")%>
</div>
<%include("web/inc/g.js")%>
<script>
 var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};

    if( isMobile.Android() || isMobile.iOS() )  {
         $('#admin-mob').show();

        if (isMobile.Android()) {
            $('#install').attr('href','http://bigota.miwifi.com/xiaoqiang/client/xqapp.apk');
         }else {
            $('#install').attr('href','https://itunes.apple.com/cn/app/id859962702?mt=8&ls=1');
         }
    }else {
        $('#admin-PC').show()
    }

$(function(){
    var pwdErrorCount = 0;
    $( '#password' ).focus();

    $( '#password' ).on( 'keypress', function( e ) {
        $('#rtloginform .form-item' ).removeClass( 'form-item-err' );
        $('#rtloginform .form-item .t' ).hide();
    });

    function buildUrl( s, token ){
        if (!window.location.origin){
            window.location.origin = window.location.protocol+"//"+window.location.host;
        }
        return window.location.origin + '/cgi-bin/luci/;stok=' + token+ '/web/setting/' + s;
    }

    function loginHandle ( e ) {
        e.preventDefault();
        var formObj = document.rtloginform;
        var pwd = $( '#password' ).val();
        if ( pwd == '') {
            return;
        }
        var nonce = Encrypt.init();
        var oldPwd = Encrypt.oldPwd( pwd );
        var param = {
            username: 'admin',
            password: oldPwd,
            logtype: 2,
            nonce: nonce
        };
        $.pub('loading:start');
        var url = '<%=luci.dispatcher.build_url("api", "xqsystem", "login")%>';
            $.post( url, param, function( rsp ) {
                $.pub('loading:stop');
                var rsp = $.parseJSON( rsp );
                if ( rsp.code == 0 ) {
                    var redirect,
                        token = rsp.token;
                    if ( /action=wan/.test(location.href) ) {
                        redirect = buildUrl('wan', token);
                    } else if ( /action=lannetset/.test(location.href) ) {
                        redirect = buildUrl('lannetset', token);
                    } else {
                        redirect = rsp.url;
                    }
                    window.location.href = redirect;
                } else if ( rsp.code == 403 ) {
                    window.location.reload();
                } else {
                    pwdErrorCount ++;
                    var errMsg = '<%:密码错误%>';
                    if (pwdErrorCount >= 4) {
                        errMsg = '<%:多次密码错误，将禁止继续尝试%>';
                    }
                    Valid.fail( document.getElementById('password'), errMsg, false);
                    $( formObj )
                    .addClass( 'shake animated' )
                    .one( 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function(){
                        $('#password').focus();
                        $( this ).removeClass('shake animated');
                    } );
                }
            });
    }
    $( '#rtloginform' ).on( 'submit', loginHandle);
    $( '#btnRtSubmit' ).on( 'click', loginHandle);
    $.placeholder();
});
</script>
