<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% js_bundle :mobile_login %>

<% extend Api::V1::Account %>

  <div class="mobileLogin-Header">
    <span class="screenreader-only">
      <%= t 'Canvas by Instructure' %>
      <script type="text/javascript">
        window.onload = function() {
          var msg
          var email = document.querySelector('[placeholder="Email"]')
          function showMessage() {
            var elt = document.querySelector('span#screenreader-errormsg')
            if (elt) elt.innerText = msg
          }
          <% if request.post? && flash[:error] %>
            msg = '<%= t "Error logging in: %{error_msg}", error_msg: flash[:error] %>'
            setTimeout(showMessage, 1500)
            if (email) email.focus()
          <% end %>
        }
      </script>
      <span aria-live="assertive" id="screenreader-errormsg"></span>
    </span>
  </div>
<%= form_for :pseudonym_session, :url => url_for({ action: :create }.merge(params.slice(:id).to_unsafe_h)), :html => {:id => "login_form", :class => "front face", :novalidate => 'novalidate', :autocomplete => "off" } do |f| %>

  <div class="ic-Form-control">
    <input
      class="ic-Input"
      autocomplete="off"
      autocorrect="off"
      placeholder="<%= @login_handle_name%>"
      type="<%= @login_handle_is_email ? 'email' : 'text' %>"
      name="pseudonym_session[unique_id]"
      value="<%= params[:pseudonym_session].try(:[], :unique_id) %>" />
  </div>
  <div class="ic-Form-control">
    <input
      class="ic-Input"
      type="password"
      name="pseudonym_session[password]"
      autocomplete="off"
      autocorrect="off"
      placeholder="<%= t :password, "Password" %>" />
  </div>
  <button
    type="submit"
    class="Button Button--primary Button--block"><%= t('log_in', 'Log In') %>
  </button>

  <% if request.post? && flash[:error] %>
    <div aria-hidden="true" class="error"><%= flash[:error] %></div>
  <% end %>

  <div class="forgotBlock">
    <% if url = params[:canvas_login].blank? && @domain_root_account.try(:forgot_password_external_url) %>
      <%= link_to t('dont_know_password', "Forgot Password?"), url, :class => 'forgot-password' %>
    <% else %>
      <a tabindex="0" role="link" class="forgot-password flip-to-back"><%= t('dont_know_password', "Forgot Password?") %></a>
    <% end %>
  </div>
<% end %>
<%= form_for :pseudonym_session, :url => forgot_password_path, :html => {:id => "forgot_password_form", :class => 'back face', :autocomplete => "off" } do |f| %>
  <div class="ic-Form-control">
    <label for="pseudonym_session_unique_id_forgot" class="screenreader-only"><%= t('login_handle', "Enter your %{login_handle_name} and we'll send you a link to change your password.", :login_handle_name => @login_handle_name) %></label>
    <input
      class="ic-Input"
      autocomplete="off"
      autocorrect="off"
      type="<%= @login_handle_is_email ? 'email' : 'text' %>"
      name="pseudonym_session[unique_id_forgot]"
      value=""
      placeholder="<%= @login_handle_name %>"
      id="pseudonym_session_unique_id_forgot" />
  </div>
  <button
    type="submit"
    class="Button Button--primary Button--block request-password-button"
    data-text-while-loading="<%= t(:sending, "Sending...") %>"
    data-text-when-loaded="<%= t(:sent, "Sent!") %>"
    data-text-on-error="<%= t(:error, "An error occurred, please try again later.") %>"
  >
    <%= t('buttons.request_password', 'Request Password') %>
  </button>
  <div class="forgotBlock">
    <a tabindex="0" role="link" class="flip-to-front"><%= t('back_to_login', "Back to Login") %></a>
  </div>
<% end %>

<% if @domain_root_account && @domain_root_account.self_registration? %>
  <% if @domain_root_account.self_registration_type == 'observer' %>
    <% js_env :ACCOUNT => account_json(@domain_root_account, nil, session, ['registration_settings']),
              :PASSWORD_POLICY => @domain_root_account.password_policy %>
    <div id='coenrollment_link' class='enrollment_link'>
      <div class="ic-Login__banner-title">
        <%= t('Parent of a Canvas User?') %>
      </div>
      <%= link_to '#',
          :class => "ic-Login__link ic-Login__register-banner ic-Login__banner-subtitle",
          data: reg_link_data(nil)  do %>
        <%= t('Create Account') %>
      <% end %>
    </div>
  <% else %>
    <div class='enrollment_link'>
      <%= link_to Setting.get("registration_link", "/register_from_website"),
                  :id => 'register_link',
                  :class => "ic-Login__link ic-Login__register-banner" do %>
        <div class="ic-Login__banner-title">
          <%= t('Need a Canvas Account?') %>
        </div>
        <div class="ic-Login__banner-subtitle">
          <%= t("Click Here, It's Free!") %>
        </div>
      <% end %>
    </div>
  <% end %>
<% end %>
