<!doctype html>
<html lang="<@t.page 'html_lang'/>">
<head>
    <meta charset="UTF-8">
    <title>${site.name} - <@t.page 'management_system'/> - <@t.page 'login_page'/></title>
    <link rel="stylesheet" href="${base}/resource/css/login.css">
    <link rel="stylesheet" href="${base}/resource/fonts/iconfont/iconfont.css">
</head>
<body>
<div class="login_body">
    <div class="login_body_left"><img src="<#if siteAttribute.square_logo?has_content>${getUrl(siteAttribute.square_logo)}<#else>${base}/resource/square_logo.png</#if>" alt="${site.name}"/></div>
    <div class="login_form clearfix">
        <form action="${base+adminContextPath}/login" method="post" onsubmit="encode()">
            <p class="logo">
                <#if site.multiple || parentSite?has_content && parentSite.multiple>
                    <select onchange="change(this)">
                        <option value="${(parentSite!site).id}">${(parentSite!site).name}</option>
                        <@sys.siteList parentId=(parentSite!site).id pageSize=100>
                        <optgroup label="<@t.page 'site.child_management'/>">
                            <#list page.list as a>
                        <option value="${a.id}"<#if a.id = site.id> selected="true"</#if>>${a.name}</option>
                            </#list>
                        </optgroup>
                        </@sys.siteList>
                    </select>
                <#else>
                    ${site.name}
                </#if>
            </p>
            <p>
                <i class="iconfont iconfont-user"></i>
                <input type="text" name="username" required="required" placeholder="<@t.page 'user.username'/>" title="<@t.page 'user.username'/>" value="${(admin.name)!}"/>
                <a class="btn-finger-login" href="javascript:void(0)"><@t.page 'login.fingerprint'/></a>
            </p>
            <p>
                <i class="iconfont iconfont-key"></i>
                <input type="password" name="password" placeholder="<@t.page 'password'/>" title="<@t.page 'password'/>"/>
            </p>
            <@sys.configData code="safe">
                <#if object.captcha?has_content && object.captcha?split(",")?seq_contains("management_system")>
            <p>
                <i class="iconfont iconfont-page"></i>
                <input type="text" name="captcha" placeholder="<@t.page 'captcha'/>" title="<@t.page 'captcha'/>" autocomplete="off"/>
                <img src="${base+adminContextPath}/getCaptchaImage?${.now?long}" alt="<@t.page 'captcha'/>" onclick="this.src=this.src+'&'+new Date().getTime()"/>
            </p>
                </#if>
            </@sys.configData>
            <p>
                <label><input type="checkbox" name="secureLogin" checked="checked"/><@t.page 'secure_login'/></label>
                <input type="submit" value="<@t.page 'login'/>"/>
                <input type="hidden" name="returnUrl" value="${(returnUrl!)}"/>
                <input type="hidden" name="encoding"/>
            </p>
            <p class="error_msg">
                <#if error?has_content>
                    <@t.message error/>
                </#if>
                <#if message?has_content>
                    <@t.message "message."+message/>
                </#if>
            </p>
        </form>
        <p>
            <@sys.configData code="site"><#assign excludeLangs=(object.site_exclude_lang!'')?split(',')/></@sys.configData>
            <#list langs as k,v>
                <#if !(excludeLangs?has_content && excludeLangs?seq_contains(k))>
            <a<#if t.pageMessage("lang")=k> class="current-lang"</#if> href="${base+adminContextPath}/changeLocale?lang=${k}&returnUrl=${(adminContextPath+'/')?url}">${v}</a>
                </#if>
            </#list>
        </p>
    </div>
</div>
<script src="${base}/resource/plugins/jquery/jquery.min.js"></script>
<script src="${base}/resource/plugins/encrypt/sha512.min.js"></script>
<script src="${base}/resource/plugins/webauthn/webauthn-simple-app.js"></script>
<script>
if($('input[name=username]').val()){
    $('input[name=password]').focus();
}else{
    $('input[name=username]').focus();
}
function encode(){
    $(".error_msg").text("");
    if($('input[name=secureLogin').is(':checked') ){
        $('input[name=password]').val(sha512($('input[name=password]').val()));
        $('input[name=encoding]').val('sha512');
    }
}
function change(obj){
    location.href=location.href+(0<location.href.indexOf('?')?'&':'?')+'currentSiteId='+obj.options[obj.options.selectedIndex].value;
}
$(document).on("webauthn-not-supported", () => {
    $(".btn-finger-login").hide();
});
var webAuthnApp=new WebAuthnApp({
    loginChallengeEndpoint : "${base+adminContextPath}/webauthn/assertion/options",
    loginResponseEndpoint : "${base+adminContextPath}/webauthn/assertion/result"
});
$(".btn-finger-login").on("click keydown",function(e){
    if(e.originalEvent.type !== "keydown" || "Space" === e.originalEvent.code || "Enter" === e.originalEvent.code){
        $(".error_msg").text("");
        if($('input[name=username]').val()){
            webAuthnApp.username = $("input[name=username]").val();
            webAuthnApp.login().then(()=>{
                window.location = "${base+adminContextPath}/webauthn/login?returnUrl="+$("input[name=returnUrl]").val();
            }).catch((err)=>{
                $(".error_msg").text(err.message);
            });
        } else {
            $('input[name=username]')[0].reportValidity();
        }
    }
});
</script>
</body>
</html>