<SectionTitle @model="users" @onExport={{perform this.export}} />

<DataTable
  @modelName="user"
  @page={{this.page}}
  @search={{this.search}}
  @filter={{this.filters}}
  @defaultSort="last_name"
  @updatePage={{fn this.updateQueryParam "page"}}
  @updateSearch={{fn this.updateQueryParam "search"}}
  @updateSort={{fn this.updateQueryParam "sort"}}
  @include={{array "acls.role" "acls.scope"}}
  as |table|
>
  {{#if (evaluate-to-boolean this.filtersVisible)}}
    <table.filters as |Filter|>
      <Filter.radio-buttons
        @name="active"
        @label={{t "emeis.filters.active.title"}}
        @selected={{this.filter_active}}
        @options={{array
          (hash value=true label=(t "emeis.filters.active.on"))
          (hash value=false label=(t "emeis.filters.active.off"))
        }}
        @onChange={{fn this.updateFilter "active"}}
      />
    </table.filters>
  {{/if}}
  <table.head as |Column|>
    {{#unless this.emailAsUsername}}
      <Column @sort="username">
        {{t "emeis.users.headings.username"}}
      </Column>
    {{/unless}}
    <Column @sort="last_name">
      {{t "emeis.users.headings.lastName"}}
    </Column>
    <Column @sort="first_name">
      {{t "emeis.users.headings.firstName"}}
    </Column>
    <Column @sort="email">
      {{t "emeis.users.headings.email"}}
    </Column>
    {{#each this.customColumns as |extraColumn|}}
      <Column
        @sort={{if
          extraColumn.sortable
          (concat "metainfo__" extraColumn.slug)
          ""
        }}
        data-test-custom-column={{extraColumn.slug}}
      >
        {{optional-translate extraColumn.heading}}
      </Column>
    {{/each}}
    <Column>
      {{t "emeis.roles.title"}}
    </Column>
    <Column>
      {{t "emeis.scopes.title"}}
    </Column>
    <Column />
  </table.head>
  <table.body as |body|>
    <body.row>
      {{#let body.model as |user|}}
        {{#if this.emailAsUsername}}
          <td>
            <LinkTo
              @route="users.edit"
              @model={{user}}
              class="uk-link-text {{unless user.isActive 'text-line-through'}}"
            >
              {{user.lastName}}
            </LinkTo>
          </td>
          <td
            class="uk-link-text {{unless user.isActive 'text-line-through'}}"
          >{{user.firstName}}</td>
        {{else}}
          <td data-test-user-username={{user.id}}>
            <LinkTo
              @route="users.edit"
              @model={{user}}
              class="uk-link-text {{unless user.isActive 'text-line-through'}}"
            >
              {{user.username}}
            </LinkTo>
          </td>
          <td
            class="uk-link-text {{unless user.isActive 'text-line-through'}}"
          >{{user.lastName}}</td>
          <td
            class="uk-link-text {{unless user.isActive 'text-line-through'}}"
          >{{user.firstName}}</td>
        {{/if}}

        <td
          class="uk-link-text {{unless user.isActive 'text-line-through'}}"
          data-test-user-email={{user.id}}
        >
          {{user.email}}
        </td>
        {{#each this.customColumns as |extraColumn|}}
          <td
            class="uk-link-text {{unless user.isActive 'text-line-through'}}"
            data-test-custom-row={{extraColumn.slug}}
          >
            {{optional-localized-value
              (get user.metainfo extraColumn.slug)
              "user"
            }}
          </td>
        {{/each}}
        <td class="uk-link-text {{unless user.isActive 'text-line-through'}}">
          <ul class="uk-list uk-list-divider">
            {{#each user.acls as |acl|}}
              <li>
                {{#if this.linkToRole}}
                  <LinkTo
                    @route="roles.edit"
                    @model={{acl.role}}
                    class="uk-link-text"
                  >
                    {{acl.role.name}}
                  </LinkTo>
                {{else}}
                  {{acl.role.name}}
                {{/if}}
              </li>
            {{else}}
              -
            {{/each}}
          </ul>
        </td>
        <td class="uk-link-text {{unless user.isActive 'text-line-through'}}">
          <ul class="uk-list uk-list-divider">
            {{#each user.acls as |acl|}}
              <li>
                {{#if this.linkToScope}}
                  <LinkTo
                    @route="scopes.edit"
                    @model={{acl.scope}}
                    class="uk-link-text"
                  >
                    {{acl.scope.fullName}}
                  </LinkTo>
                {{else}}
                  {{acl.scope.fullName}}
                {{/if}}
              </li>
            {{else}}
              -
            {{/each}}
          </ul>
        </td>
        <td class="uk-text-right small-padding-right">
          <LinkTo
            @route="users.edit"
            @model={{user}}
            class="uk-link-reset uk-margin-small-right"
          >
            <UkIcon @icon="pencil" />
          </LinkTo>
          {{#if (can-delete "user" user)}}
            <UkButton @color="link" @onClick={{perform this.delete user}}>
              <UkIcon @icon="trash" />
            </UkButton>
          {{/if}}
        </td>
      {{/let}}
    </body.row>
  </table.body>
</DataTable>