{% extends "base.html" %}
{% load i18n %}

{% block sub_title %}{% trans "Log In" %} - {% endblock %}
{% block header_css_class %}hide{% endblock %}
{% block extra_base_style %}
<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}css/seafile-ui.css" />
{% endblock %}

{% block extra_style %}
<style type="text/css">
html, body, #wrapper { height:100%; }
#wrapper {
    background: url('{{ MEDIA_URL }}{{login_bg_image_path}}') center top no-repeat scroll;
    background-size: cover;
    padding-top:1px;
}
#lang {
    margin:0;
}
#lang-context {
    font-weight:normal;
}
#lang-context-selector {
    text-align:left;
}
</style>
{% endblock %}

{% block main_content %}
<div class="login-panel-outer-container vh">
<div class="login-panel" id="log-in-panel">
    <h1 class="login-panel-hd">{% trans "Log In" %}</h1>
    <form action="" method="post" id="login-form">{% csrf_token %}
        <input type="text" name="login" placeholder="{% trans "Email or Username" %}" aria-label="{% trans "Email or Username" %}" title="{% trans "Email or Username" %}" value="" class="input mb-3" />
        <div class="position-relative">
          <input id="password-input" type="password" name="password" placeholder="{% trans "Password" %}" aria-label="{% trans "Password" %}" title="{% trans "Password" %}" value="" class="input" autocomplete="off" />
          <i id="toggle-show-password" class="sf3-font sf3-font-eye-slash"></i>
        </div>

        {% if form.captcha %}
        <div class="ovhd mt-3">
            <span id="refresh-captcha" title="{% trans "Refresh" %}" class="icon-refresh op-icon fright"></span>
            {{ form.captcha }}
        </div>
        {% endif %}

        <input type="hidden" name="next" value="{% if next %}{{ next|escape }}{% else %}{{ SITE_ROOT }}{% endif %}" />
        {% if form.errors %}
        {% if form.captcha.errors %}
        {{ form.captcha.errors}}
        {% elif form.errors.freeze_account %}
        <p class="error mt-2">{{ form.errors.freeze_account }}</p>
        {% elif form.errors.inactive %}
        <p class="error mt-2">{{ form.errors.inactive }}</p>
        {% elif form.errors.invalid_input %}
        <p class="error mt-2">{{ form.errors.invalid_input }}</p>
        {% elif form.errors.not_found %}
        <p class="error mt-2">{{ form.errors.not_found }}</p>
        {% elif form.errors.disable_pwd_login %}
        <p class="error mt-2">{% trans "Please use Single Sign-On to login." %}</p>
        {% else %}
        <p class="error mt-2">{% trans "Incorrect email or password" %}</p>
        {% endif %}
        {% else %}
        <p class="error mt-2 hide"></p>
        {% endif %}

        <label class="checkbox-label mt-3 mb-0 remember-me">
            <input type="checkbox" name="remember_me" class="vam remember-input" />
            <span class="vam">{% blocktrans %}Remember me for {{remember_days}} days {% endblocktrans %}</span>
        </label>

        <button type="submit" class="submit btn btn-primary btn-block h-auto mb-3">{% trans "Log In" %}</button>
    </form>

    {% if enable_sso %}
    <button id="sso" class="btn btn-secondary btn-block">{% trans "Single Sign-On" %}</button>
    {% endif %}

    {% if enable_multi_adfs %}
    <button id="multi_adfs_sso" class="btn btn-secondary btn-block">{% trans "Single Sign-On" %}</button>
    {% endif %}

    {% if enable_weixin or enable_work_weixin or enable_dingtalk %}
    <div class="text-center">
      <div class="mt-5 mb-4 d-flex align-items-center">
        <span class="login-with-wechat-deco flex-fill"></span>
        <span id="login-with-wechat" class="mx-2">{% trans "Login with" %}</span>
        <span class="login-with-wechat-deco flex-fill"></span>
      </div>

      {% if enable_weixin %}
        <a href="{% url "weixin_oauth_login" %}">
          <img src="{{MEDIA_URL}}img/weixin.png" width="32" alt="" title="{% if LANGUAGE_CODE == 'zh-cn' %}微信{% else %}WeChat{% endif %}" />
        </a>
      {% endif %}

      {% if enable_work_weixin %}
        <a href="{% url "work_weixin_sso" %}">
          <img src="{{MEDIA_URL}}img/work-weixin.png" width="32" alt="" title="{% if LANGUAGE_CODE == 'zh-cn' %}企业微信{% else %}Work-WeChat{% endif %}" />
        </a>
      {% endif %}

      {% if enable_dingtalk %}
        <a href="{% url "dingtalk_sso" %}">
          <img src="{{MEDIA_URL}}img/dingtalk.png" width="32" alt="" title="{% if LANGUAGE_CODE == 'zh-cn' %}钉钉{% else %}DingTalk{% endif %}" />
        </a>
      {% endif %}
    </div>
    {% endif %}

    <div class="login-panel-bottom-container d-flex justify-content-center" id="login-bottom">
        {% if enable_change_password %}
        <a href="{{ SITE_ROOT }}accounts/password/reset/" class="normal px-2 bottom-item">{% trans "Forgot password?" %}</a>
        {% endif %}
        {% if enable_signup %}
        <a href="{{ signup_url }}" class="normal px-2 bottom-item" id="sign-up">{% trans "Signup" %}</a>
        {% endif %}
    </div>
</div>
</div>
{% include "snippets/policy_service_link.html" %}
{% endblock %}

{% block extra_script %}
<script type="text/javascript">
$('.login-panel-outer-container').prepend($($('#logo').html()).attr({'height': 40}).addClass('login-panel-logo'));
$('.login-panel-bottom-container').append($('#lang').removeClass('fright').addClass('px-2'));

var $el = $('.login-panel-outer-container');
var elHeight = $el.outerHeight();
var wdHeight = $(window).height();
if (wdHeight > elHeight) {
    $el.css({'margin-top': (wdHeight - elHeight)/2});
}
$el.removeClass('vh');

$('#lang-context').on('click', function() {
    var langTop = $('#lang').offset().top;
    var langSelectorTop;
    var langSelectorHeight = $('#lang-context-selector .sf-popover-con').outerHeight();
    if (langSelectorHeight > langTop) {
        langSelectorTop = '-' + (langTop - 5) + 'px';
    } else {
        langSelectorTop = '-' + (langSelectorHeight + 5) + 'px';
    }
    $('#lang-context-selector').css({
        'top': langSelectorTop,
        'right': 0
    });
    $('#lang-context-selector .sf-popover-con').css({
        'max-height': $('#lang').offset().top - 10
    });
});

$('[name="login"]').trigger('focus');

$('#toggle-show-password').click(function() {
  $(this).toggleClass('sf3-font-eye-slash sf3-font-eye');
  if ($(this).hasClass('sf3-font-eye-slash')) {
    $('#password-input').attr('type', 'password');
  } else {
    $('#password-input').attr('type', 'text');
  }
});

function setCaptchaInputWidth() {
    $('#id_captcha_1').outerWidth($('.input').outerWidth() - $('.captcha').width() - $('#refresh-captcha').outerWidth(true) - 10);
}
$(window).on('load', setCaptchaInputWidth);
$('.captcha').on('load', setCaptchaInputWidth);
$('#refresh-captcha').on('click', function() {
    $.ajax({
        url: '{% url 'captcha-refresh' %}',
        dataType:'json',
        cache:false,
        success: function(data) {
            $('.captcha').attr('src', data['image_url']);
            $('#id_captcha_0').val(data['key']);
        },
        error: function() {
            $('.error').removeClass('hide').html("{% trans "Failed to refresh the CAPTCHA, please try again later." %}");
        }
    });
    return false;
});

$('#login-form').on('submit', function(){
    if (!$('input[name="login"]').val().trim()) {
        $('.error').removeClass('hide').html("{% trans "Email or username cannot be blank" %}");
        return false;
    }
    if (!$('input[name="password"]').val().trim()) {
        $('.error').removeClass('hide').html("{% trans "Password cannot be blank" %}");
        return false;
    }
});

{% if enable_sso %}
$(function() {
    $('#sso').on('click', function() {
        window.location = "{% url 'sso' %}{% if next %}?next={{ next|escape }}{% endif %}" + encodeURIComponent(document.location.hash);
        return false;
    });
});
{% endif %}

{% if enable_multi_adfs %}
$(function() {
    $('#multi_adfs_sso').on('click', function() {
        window.location = "{% url 'multi_adfs_sso' %}{% if next %}?next={{ next|escape }}{% endif %}" + encodeURIComponent(document.location.hash);
        return false;
    });
});
{% endif %}

</script>
{% endblock %}
