<%
  # TODO remove when https://github.com/hotwired/turbo/issues/203 is fixed
  enable_turbo = !session[:user_return_to] || !session[:user_return_to].match?('/discourse/connect')
%>

<%= render HeaderCompactComponent.new(title: "Log in") %>

<%= render MainCompactComponent.new do %>
  <%= form_for(resource, as: resource_name, url: session_path(resource_name),
                         data: { turbo: enable_turbo.to_s }) do |f| %>
    <%= render "devise/shared/error_messages", resource: resource %>
    <div class="mb-6">
      <%= f.label :cn, 'User', class: 'block mb-2 font-bold' %>
      <p class="flex gap-2 items-center">
        <%= f.text_field :cn, value: h(params[:cn]),
              autofocus: params[:cn].blank?, autocomplete: "username",
              required: true, class: "relative grow", tabindex: "1" %>
        <span class="relative shrink-0 text-gray-500">@ <%= Setting.primary_domain %></span>
      </p>
    </div>
    <p class="mb-8">
      <%= f.label :password, class: 'block mb-2 font-bold' %>
      <%= f.password_field :password, autocomplete: "current-password",
            autofocus: params[:cn].present?, required: true,
            class: "w-full", tabindex: "2" %>
    </p>

    <%= tag.div class: "flex items-center mb-8 gap-x-3", data: {
          controller: "settings--toggle",
          :'settings--toggle-switch-enabled-value' => "false"
        } do %>
      <div class="relative inline-flex flex-shrink-0">
        <%= render FormElements::ToggleComponent.new(
              enabled: false, input_enabled: true, class_names: "hidden",
              tabindex: "3", data: {
                :'settings--toggle-target' => "button",
                action: "settings--toggle#toggleSwitch"
              }) %>
        <%= f.check_box :remember_me, {
              checked: false,
              data: { :'settings--toggle-target' => "checkbox" }
            }, "true", "false" %>
      </div>
      <%= f.label :remember_me,
            class: "text-gray-500 flex flex-col",
            data: { action: "click->settings--toggle#toggleSwitch" } %>
      <p class="grow text-sm text-right">
        <%= link_to "Forgot your password?", new_password_path(resource_name),
              class: "text-gray-500 underline" %><br />
      </p>
    <% end %>

    <p>
      <%= f.submit "Log in", class: 'btn-md btn-blue w-full', tabindex: "4" %>
    </p>
  <% end %>
<% end %>
