{% from "components/icon.html" import Icon %}
{% from "components/save_button.html" import SaveButton %}


{% for env_form in members_form.envs %}
  {% if env_form.env_id.data == env['id'] %}
    <div class='app-team-settings-link'>
      {{ 'fragments.edit_environment_team_form.add_new_member_text' | translate }}
      <a href='{{ url_for("applications.team", application_id=application.id) }}'>
        {{ 'fragments.edit_environment_team_form.add_new_member_link' | translate }}
      </a>
    </div>
    <form
      action="{{ url_for('applications.update_env_roles', environment_id=env['id']) }}"
      method="post">
      {{ members_form.csrf_token }}
      {{ env_form.env_id() }}
      <edit-environment-role
        inline-template
        v-bind:initial-role-categories='{{ env_form.team_roles.data | tojson }}'>
        <div>
          <div v-for='(roleCategory, roleindex) in roleCategories' class='environment-role'>
            <h4 v-if='checkNoAccess(roleCategory.role)'>
              {{ 'fragments.edit_environment_team_form.unassigned_title' | translate }}
            </h4>
            <h4 v-else v-html='roleCategory.role'></h4>
            <ul class='environment-role__users'>
              <div
                v-if="roleCategory.members && !roleCategory.members.length"
                class='environment-role__no-user'>
                {{ 'fragments.edit_environment_team_form.no_members' | translate }}
              </div>
              <li
                v-for='(member, memberindex) in roleCategory.members'
                class="environment-role__user"
                v-bind:class="{'unassigned': checkNoAccess(member.role_name)}">
                <span v-html='member.user_name'>
                </span>
                <span v-on:click="toggleSection(member.user_id)" class="icon-link right">
                  {{ Icon('edit', classes="icon--medium") }}
                </span>
                <div
                  v-show="selectedSection === member.user_id"
                  class='environment-role__user-field'>
                  <div class="usa-input">
                    <fieldset
                      data-ally-disabled="true"
                      class="usa-input__choices"
                      v-on:change="onInput">
                      <ul
                        v-for='(roleCategory, roleinputindex) in roleCategories'
                        v-bind:id="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-role_name'">
                        <li>
                          <input
                            v-bind:checked="member.role_name === roleCategory.role"
                            v-bind:name="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-role_name'"
                            v-bind:id="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-role_name-' + roleinputindex"
                            type="radio"
                            v-bind:user-id='member.user_id'
                            v-bind:value='roleCategory.role'>
                          <label
                            v-bind:for="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-role_name-' + roleinputindex">
                            <span v-if='checkNoAccess(roleCategory.role)'>
                              {{ 'fragments.edit_environment_team_form.no_access' | translate }}
                            </span>
                            <span v-else v-html='roleCategory.role'></span>
                          </label>
                        </li>
                      </ul>
                    </fieldset>
                  </div>
                </div>
                <input
                  v-bind:id="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-user_id'"
                  v-bind:name="'envs-{{ loop.index0 }}-team_roles-' + roleindex + '-members-' + memberindex + '-user_id'"
                  type="hidden"
                  v-bind:value='member.user_id'>
              </li>
            </ul>
          </div>
          <div class='action-group'>
            {{
              SaveButton(
                text=("portfolios.applications.update_button_text" | translate)
              )
            }}
          </div>
        </div>
      </edit-environment-role>
      <div class='action-group-cancel'>
        <a class='action-group-cancel__action icon-link icon-link--default' v-on:click="toggleSection('members')">
          {{ "common.cancel" | translate }}
        </a>
      </div>
    </form>
  {% endif %}
{% endfor %}
