{% extends '_base_only_msg_content.html' %}
{% load static %}
{% load i18n %}

{% block content_title %}
    {% trans 'Login' %}
{% endblock %}

{% block content %}
    <form id="form" class="m-t" role="form" method="post" action="">
        {% csrf_token %}
        {% if form.non_field_errors %}
            <div style="line-height: 17px;">
                <p class="red-fonts">{{ form.non_field_errors.as_text }}</p>
            </div>
        {% elif form.errors.captcha %}
            <p class="red-fonts">{% trans 'Captcha invalid' %}</p>
        {% endif %}

        <div class="form-group">
            <input type="text" class="form-control" name="{{ form.username.html_name }}" placeholder="{% trans 'Username' %}" required="" value="{% if form.username.value %}{{ form.username.value }}{% endif %}">
            {% if form.errors.username %}
                <div class="help-block field-error">
                    <p class="red-fonts">{{ form.errors.username.as_text }}</p>
                </div>
            {% endif %}
        </div>
        <div class="form-group">
            <input type="password" class="form-control" id="password" placeholder="{% trans 'Password' %}" required="">
            <input id="password-hidden" type="text" style="display:none" name="{{ form.password.html_name }}">
            {% if form.errors.password %}
                <div class="help-block field-error">
                    <p class="red-fonts">{{ form.errors.password.as_text }}</p>
                </div>
            {% endif %}
        </div>
        {% if form.challenge %}
            <div class="form-group">
                <input type="challenge" class="form-control" id="challenge" name="{{ form.challenge.html_name }}" placeholder="{% trans 'MFA code' %}" >
                {% if form.errors.challenge %}
                    <div class="help-block field-error">
                        <p class="red-fonts">{{ form.errors.challenge.as_text }}</p>
                    </div>
                {% endif %}
            </div>
        {% endif %}
        <div>
            {{ form.captcha }}
        </div>
        <button type="submit" class="btn btn-primary block full-width m-b" onclick="doLogin();return false;">{% trans 'Login' %}</button>

        {% if demo_mode %}
            <p class="text-muted font-bold" style="color: red">
                Demo账号: admin 密码: admin
            </p>
        {% endif %}

        <div class="text-muted text-center">
            <div>
                <a href="{% url 'authentication:forgot-password' %}">
                    <small>{% trans 'Forgot password' %}?</small>
                </a>
            </div>
        </div>

        {% if AUTH_OPENID %}
            <div class="hr-line-dashed"></div>
            <p class="text-muted text-center">{% trans "More login options" %}</p>
            <div>
                <button type="button" class="btn btn-default btn-sm btn-block" onclick="location.href='{% url 'authentication:openid:login' %}'">
                    <i class="fa fa-openid"></i>
                    {% trans 'OpenID' %}
                </button>
            </div>
        {% endif %}

    </form>
    <script type="text/javascript" src="/static/js/plugins/jsencrypt/jsencrypt.min.js"></script>
    <script>
    function encryptLoginPassword(password, rsaPublicKey){
        var jsencrypt = new JSEncrypt(); //加密对象
        jsencrypt.setPublicKey(rsaPublicKey); // 设置密钥
        return jsencrypt.encrypt(password); //加密
    }
    function doLogin() {
        //公钥加密
        var rsaPublicKey = "{{ rsa_public_key }}"
        var password =$('#password').val(); //明文密码
        var passwordEncrypted = encryptLoginPassword(password, rsaPublicKey)
        $('#password-hidden').val(passwordEncrypted); //返回给密码输入input
        $('#form').submit();//post提交
    }
    </script>
{% endblock %}
