<h1 class="maintenance-title text-center">Log In to SonarQube</h1>

<% auth_providers = Api::Utils.java_facade.getIdentityProviders().to_a %>
<% if auth_providers.size > 0 %>
  <section class="oauth-providers">
    <ul>
      <% auth_providers.each do |provider| %>
        <li>
          <a href="<%= ApplicationController.root_context -%>/sessions/init/<%= provider.getKey().to_s %>"
             style="background-color: <%= provider.getDisplay().getBackgroundColor().to_s %>"
             title="Log in with <%= provider.getName().to_s -%>">
            <img alt="<%= provider.getName().to_s -%>" width="20" height="20"
                 src="<%= ApplicationController.root_context + provider.getDisplay().getIconPath().to_s -%>">
            <span>Log in with <%= provider.getName().to_s -%></span>
          </a>
        </li>
      <% end %>
    </ul>
  </section>
<% end %>

<form id="login_form" action="<%= ApplicationController.root_context -%>/api/authentication/login" method="post">
  <input type="hidden" name="return_to_anchor" value="<%= h @return_to_anchor %>">

  <div class="alert alert-danger alert-authentication-failed hidden"><%= message('session.flash_notice.authentication_failed') %></div>
  <div class="hidden" id="messages-panel">
    <div class="alert alert-danger alert-flash hidden" id="error">
      <span id="errormsg"></span>
    </div>
    <div class="alert alert-info alert-flash hidden" id="info">
      <span id="infomsg"></span>
    </div>
  </div>

  <div class="big-spacer-bottom">
    <label for="login" class="login-label"><%= message('login') %></label>
    <input type="text" id="login" name="login" class="login-input" maxlength="255" required
           placeholder="<%= message('login') %>">
    <% if Property.value('sonar.allowUsersToSignUp')=='true' %>
      <p class="note spacer-top spacer-left spacer-right">
        <%= message('sessions.new_account', :params => [url_for(:controller => 'users', :action => 'new')]) -%>
      </p>
    <% end %>
  </div>

  <p class="big-spacer-bottom">
    <label for="password" class="login-label"><%= message('password') %></label>
    <input type="password" id="password" name="password" class="login-input" required
           placeholder="<%= message('password') %>">
  </p>

  <div>
    <div class="text-right overflow-hidden">
      <button name="commit"><%= message('sessions.log_in') -%></button>
      <a class="spacer-left" href="<%= home_path -%>"><%= message('cancel') -%></a>
    </div>
  </div>
</form>

<script>
  (function ($) {
    $('#login').focus();
    var returnToAnchor = $('[name="return_to_anchor"]');
    if (!returnToAnchor.val()) {
      returnToAnchor.val(window.location.hash);
    }

    $("#login_form").modalForm({
      success: function () {
        window.location = '<%= escape_javascript @return_to -%>' + $('[name="return_to_anchor"]').val();
      },
      error: function () {
        $('.alert-flash').addClass('hidden');
        $('.alert-authentication-failed').removeClass('hidden');
        $('button').prop('disabled', false);
      },
      statusCode: {
        401: null
      }
    });

    <% if cookies['flash'] %>
      var data = JSON.parse(unescape('<%= escape_javascript(cookies['flash']) -%>'));
      if (data['loginerror']) {
        error(data['loginerror'].toString().replace(/\+/g, ' '));
      }
      if (data['info']) {
        info(data['info'].toString().replace(/\+/g, ' '));
      }
      <% cookies.delete 'flash' %>
    <% end %>

  })(window.jQuery);
</script>

