{% import 'macros/form.html' as form %}

{% block form %}
  <form id="user-edit-form" method="post" action="{{ action }}" enctype="multipart/form-data">
    {{ h.csrf_input() }}
    {% block form_errors %}{{ form.errors(error_summary) }}{% endblock %}

    {% block core_fields %}
      <fieldset>
        <legend>{{ _('Change details') }}</legend>
        {% if g.userobj.sysadmin %}
          {{ form.input('name', label=_('Username'), id='field-username', value=data.name, error=errors.name, classes=['control-medium'], is_required=true) }}
        {% else %}
          {{ form.input('name', label=_('Username'), id='field-username', value=data.name, error=errors.name, classes=['control-medium'], attrs={'readonly': '', 'class': 'form-control'}) }}
        {% endif %}

        {{ form.input('fullname', label=_('Full name'), id='field-fullname', value=data.fullname, error=errors.fullname, placeholder=_('eg. Joe Bloggs'), classes=['control-medium']) }}

        {{ form.input('email', label=_('Email'), id='field-email', type='email', value=data.email, error=errors.email, placeholder=_('eg. joe@example.com'), classes=['control-medium'], is_required=true) }}

        {{ form.markdown('about', label=_('About'), id='field-about', value=data.about, error=errors.about, placeholder=_('A little information about yourself')) }}

        {% set is_upload = data.image_url and not data.image_url.startswith('http') %}
        {% set is_url = data.image_url and data.image_url.startswith('http') %}

        {{ form.image_upload(data, errors, is_upload_enabled=h.uploads_enabled(), is_url=is_url, is_upload=is_upload, upload_label=_('Profile picture'), url_label=_('Profile picture URL') ) }}
      </fieldset>
    {% endblock %}

    {% block extra_fields %}
    {% if g.userobj.sysadmin and data.state == 'deleted' %}
      {% call form.checkbox('activate_user', label=_('Reactivate User'), id='activate_user', value=True, checked=false) %}
      {% set helper_text = _('This account is deactivated, if you want to reactivate it, please click on checkbox.') %}
      {{ form.info(helper_text, classes='info-help-tight') }}
      {% endcall %}
    {% endif %}
    {% endblock %}

    {% if is_sysadmin and current_user.name != data.name %}
    {% block sysadmin_password %}
      <fieldset>
      <legend>{{ _('Change ' + data.name|capitalize + "'s" + ' password') }}</legend>
      {{ form.input('password1', type='password', label=_('Password'), id='field-password', value=data.password1, error=errors.password1, classes=['control-medium'], attrs={'autocomplete': 'off', 'class': 'form-control'} ) }}
      {{ form.input('password2', type='password', label=_('Confirm Password'), id='field-password-confirm', value=data.password2, error=errors.password2, classes=['control-medium'], attrs={'autocomplete': 'off', 'class': 'form-control'}) }}
      </fieldset>
    {% endblock %}

    {% block sysadmin_old_password %}
      <fieldset>
        <legend>{{ _('Sysadmin password') }}</legend>
        {{ form.input('old_password',
                      type='password',
                      label=_('Sysadmin Password'),
                      id='field-password-old',
                      value=data.oldpassword,
                      error=errors.oldpassword,
                      classes=['control-medium'],
                      attrs={'autocomplete': 'off', 'class': 'form-control'}
                      ) }}

      </fieldset>
    {% endblock %}

    {% else %}
    {% block change_password %}
      <fieldset>
        <legend>{{ _('Change password') }}</legend>
        {{ form.input('old_password',
                      type='password',
                      label=_('Old Password'),
                      id='field-password-old',
                      value=data.oldpassword,
                      error=errors.oldpassword,
                      classes=['control-medium'],
                      attrs={'autocomplete': 'off', 'class': 'form-control'}
                      ) }}

        {{ form.input('password1', type='password', label=_('Password'), id='field-password', value=data.password1, error=errors.password1, classes=['control-medium'], attrs={'autocomplete': 'off', 'class': 'form-control'} ) }}

        {{ form.input('password2', type='password', label=_('Confirm Password'), id='field-password-confirm', value=data.password2, error=errors.password2, classes=['control-medium'], attrs={'autocomplete': 'off', 'class': 'form-control'}) }}
      </fieldset>
    {% endblock %}
    {% endif %}

    {{ form.required_message() }}

    <div class="form-actions">
      {% block form_actions %}
      {% set is_deleted = data.state == 'deleted' %}
      {% if not is_deleted %}
        {% block delete_button %}
          {% if h.check_access('user_delete', {'id': data.id})  %}
            <a class="btn btn-danger pull-left" href="{% url_for 'user_delete', id=data.id %}" data-module="confirm-action" data-module-content="{{ _('Are you sure you want to delete this User?') }}">{% block delete_button_text %}{{ _('Delete') }}{% endblock %}</a>
          {% endif %}
        {% endblock %}
        {% endif %}
        <button class="btn btn-primary" type="submit" name="save">{{ _('Reactivate Profile') if is_deleted else _('Update Profile') }}</button>
      {% endblock %}
    </div>
  </form>
{% endblock %}
