{% extends "base_beer.html" %}
{% load static i18n %}
{% block title %}{% trans "Initial setup — Create admin" %}{% endblock %}

{% block content %}
<div class="large-space"></div>

<div class="large-space"></div>

<div class="grid">
  <div class="s12 m10 l8 xl6 middle-align center-align" style="margin: 0 auto;">
    <article class="round elevate">
      <div class="padding">
          <h4 class="center-align primary-text">{% trans "Create initial administrator" %}</h4>
          <p class="center-align">
            {% trans "This page is only available when there are no admin users in the database." %}
          </p>
          <p class="center-align">
            {% trans "After creating the account you will be logged in and redirected to the admin." %}
          </p>
      </div>

      <form method="post" novalidate>
        {% csrf_token %}

        {% if form.non_field_errors %}
          <div class="padding error surface round mb">
             {% for err in form.non_field_errors %}
                <div class="row align-center">
                    <i class="error-text">warning</i>
                    <span class="error-text">{{ err }}</span>
                </div>
             {% endfor %}
          </div>
        {% endif %}

        <div class="grid">
            <div class="s12 m6">
                <div class="field label border round {% if form.first_name.errors %}error{% endif %}">
                    <input type="text" name="{{ form.first_name.name }}" id="{{ form.first_name.id_for_label }}" value="{{ form.first_name.value|default:'' }}">
                    <label>{% trans "First name" %}</label>
                    {% if form.first_name.errors %}
                        <span class="helper error-text">{{ form.first_name.errors.0 }}</span>
                    {% endif %}
                </div>
            </div>
             <div class="s12 m6">
                <div class="field label border round {% if form.last_name.errors %}error{% endif %}">
                    <input type="text" name="{{ form.last_name.name }}" id="{{ form.last_name.id_for_label }}" value="{{ form.last_name.value|default:'' }}">
                    <label>{% trans "Last name" %}</label>
                     {% if form.last_name.errors %}
                        <span class="helper error-text">{{ form.last_name.errors.0 }}</span>
                    {% endif %}
                </div>
            </div>
        </div>

        <div class="field label border round {% if form.username.errors %}error{% endif %}">
            <input type="text" name="{{ form.username.name }}" id="{{ form.username.id_for_label }}" value="{{ form.username.value|default:'' }}">
            <label>{% trans "Username" %}</label>
            {% if form.username.errors %}
                <span class="helper error-text">{{ form.username.errors.0 }}</span>
            {% endif %}
        </div>

        <div class="field label border round {% if form.email.errors %}error{% endif %}">
            <input type="email" name="{{ form.email.name }}" id="{{ form.email.id_for_label }}" value="{{ form.email.value|default:'' }}">
            <label>{% trans "Email (optional)" %}</label>
            {% if form.email.errors %}
                <span class="helper error-text">{{ form.email.errors.0 }}</span>
            {% endif %}
        </div>

        <div class="space"></div>
        <div class="divider"></div>
        <div class="space"></div>

        <div class="field label border round {% if form.password1.errors %}error{% endif %}">
            <input type="password" name="{{ form.password1.name }}" id="{{ form.password1.id_for_label }}">
            <label>{% trans "Password" %}</label>
            {% if form.password1.errors %}
                <span class="helper error-text">{{ form.password1.errors.0 }}</span>
            {% endif %}
        </div>

        <div class="field label border round {% if form.password2.errors %}error{% endif %}">
             <input type="password" name="{{ form.password2.name }}" id="{{ form.password2.id_for_label }}">
            <label>{% trans "Confirm password" %}</label>
             {% if form.password2.errors %}
                <span class="helper error-text">{{ form.password2.errors.0 }}</span>
            {% endif %}
        </div>

        <div class="large-space"></div>

        <nav class="right-align">
            <a class="button transparent border round" href="{% url 'index' %}">{% trans "Cancel" %}</a>
            <button type="submit" id="setup-button" class="button primary round" style="display: none;">{% trans "Create admin" %}</button>
        </nav>

      </form>
    </article>
  </div>
</div>
{% endblock %}

{% block page_scripts %}
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const usernameInput = document.getElementById('{{ form.username.id_for_label }}');
        const passwordInput = document.getElementById('{{ form.password1.id_for_label }}');
        const setupButton = document.getElementById('setup-button');

        function toggleButton() {
            if (usernameInput.value.trim() !== '' && passwordInput.value.trim() !== '') {
                setupButton.style.display = 'inline-flex';
            } else {
                setupButton.style.display = 'none';
            }
        }

        if (usernameInput && passwordInput && setupButton) {
            usernameInput.addEventListener('input', toggleButton);
            passwordInput.addEventListener('input', toggleButton);
            usernameInput.addEventListener('change', toggleButton);
            passwordInput.addEventListener('change', toggleButton);

            toggleButton();
        }
    });
</script>
{% endblock %}
