{% extends "applications/base.html" %}

{% from "components/alert.html" import Alert %}
{% from "components/delete_confirmation.html" import DeleteConfirmation %}
{% from "applications/fragments/environments.html" import EnvironmentManagementTemplate with context %}
{% from "applications/fragments/members.html" import MemberManagementTemplate with context %}
{% from "components/modal.html" import Modal %}
{% from "components/pagination.html" import Pagination %}
{% from "components/save_button.html" import SaveButton %}
{% from "components/text_input.html" import TextInput %}

{% set secondary_breadcrumb = 'portfolios.applications.existing_application_title' | translate({ "application_name": application.name }) %}

{% block application_content %}

  <h3>{{ 'portfolios.applications.settings.name_description' | translate }}</h3>

  {% if user_can(permissions.EDIT_APPLICATION) %}
    <base-form inline-template>
      <form method="POST" action="{{ url_for('applications.update', application_id=application.id) }}" class="col col--half">
        {{ application_form.csrf_token }}
        {{ TextInput(application_form.name, optional=False) }}
        {{ TextInput(application_form.description, paragraph=True, optional=True, showOptional=False) }}
        <div class="action-group action-group--tight">
          {{ SaveButton(text='common.save_changes'|translate) }}
        </div>
      </form>
    </base-form>
  {% else %}
    <div>
      <p>
        {{ "fragments.edit_application_form.explain" | translate }}
      </p>
      <div class="usa-input usa-input__title__view-only">
        {{ application_form.name.label() }}
      </div>
      <p>
        {{ application_form.name.data }}
      </p>
      <div class="usa-input usa-input__title__view-only">
        {{ application_form.description.label() }}
      </div>
      <p>
        {{ application_form.description.data }}
      </p>
    </div>
  {% endif %}
  <hr>

  {{ MemberManagementTemplate(
    application,
    members,
    new_member_form,
    action_new="applications.create_member",
    action_update="applications.update_member") }}

  {{ EnvironmentManagementTemplate(
    application,
    environments_obj,
    new_env_form) }}

  {% if user_can(permissions.DELETE_APPLICATION) %}
    {% set env_count = application.environments | length %}
    {% if env_count == 1 %}
      {% set pluralized_env = "environment" %}
    {% else %}
      {% set pluralized_env = "environments" %}
    {% endif %}

    <h3>
      {{ "portfolios.applications.delete.subheading" | translate }}
    </h3>
    <div class="form-row">
      <div class="form-col form-col--two-thirds">
        {{ "portfolios.applications.delete.text" | translate({"application_name": application.name}) | safe }}
      </div>
      <div class="form-col form-col--third">
        <div class="usa-input">
          <input
            id="delete-application"
            type="button"
            v-on:click="openModal('delete-application')"
            class='usa-button--outline button-danger-outline'
            value="{{ 'portfolios.applications.delete.button' | translate }}"
            >
        </div>
      </div>
    </div>

    {% call Modal(name="delete-application") %}
      <h1>{{ "portfolios.applications.delete.header" | translate }}</h1>
      <hr>
      {{
        Alert(
          title=("components.modal.destructive_title" | translate),
          message=("portfolios.applications.delete.alert.message" | translate),
          level="warning"
        )
      }}

      {{
        DeleteConfirmation(
          modal_id="delete_application",
          delete_text=('portfolios.applications.delete.button' | translate),
          delete_action= url_for('applications.delete', application_id=application.id),
          form=application_form
        )
      }}
    {% endcall %}
  {% endif %}

  <hr>

  {% if user_can(permissions.VIEW_APPLICATION_ACTIVITY_LOG) and config.get("USE_AUDIT_LOG", False) %}
    {% include "fragments/audit_events_log.html" %}
    {{ Pagination(audit_events, url=url_for('applications.settings', application_id=application.id)) }}
  {% endif %}

{% endblock %}
