{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<TabPageHeader @model={{@backendModel}} @breadcrumbs={{@breadcrumbs}}>
  <:toolbarFilters>
    {{#if (and (not @promptConfig) @roles.meta.total)}}
      <FilterInput
        @placeholder="Filter roles"
        @value={{@pageFilter}}
        @wait={{500}}
        @autofocus={{true}}
        @onInput={{this.onFilterChange}}
      />
    {{/if}}
  </:toolbarFilters>
  <:toolbarActions>
    {{#if @promptConfig}}
      <ToolbarLink @route="configure" data-test-toolbar-action="config">
        Configure LDAP
      </ToolbarLink>
    {{else}}
      <ToolbarLink @route="roles.create" @type="add" data-test-toolbar-action="role">
        Create role
      </ToolbarLink>
    {{/if}}
  </:toolbarActions>
</TabPageHeader>

{{#if @promptConfig}}
  <ConfigCta />
{{else if (not @roles.meta.filteredTotal)}}
  {{#if @pageFilter}}
    <EmptyState @title="There are no roles matching &quot;{{@pageFilter}}&quot;" />
  {{else}}
    <EmptyState
      data-test-config-cta
      @title="No roles created yet"
      @message="Roles in Vault will allow you to manage LDAP credentials. Create a role to get started."
    >
      <LinkTo class="has-top-margin-xs" @route="roles.create">
        Create role
      </LinkTo>
    </EmptyState>
  {{/if}}
{{else}}
  <div class="has-bottom-margin-s">
    {{#each @roles as |role|}}
      <ListItem @linkPrefix={{this.mountPoint}} @linkParams={{array "roles.role.details" role.type role.name}} as |Item|>
        <Item.content>
          <Icon @name="user" />
          <span data-test-role={{role.name}}>{{role.name}}</span>
          <Hds::Badge @text={{role.type}} data-test-role-type-badge={{role.name}} />
        </Item.content>
        <Item.menu as |Menu|>
          {{#if role.rolePath.isLoading}}
            <li class="action">
              <button disabled type="button" class="link button is-loading is-transparent">
                loading
              </button>
            </li>
          {{else}}
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-edit
                @route="roles.role.edit"
                @models={{array role.type role.name}}
                @disabled={{not role.canEdit}}
              >
                Edit
              </LinkTo>
            </li>
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-get-creds
                @route="roles.role.credentials"
                @models={{array role.type role.name}}
                @disabled={{not role.canReadCreds}}
              >
                Get credentials
              </LinkTo>
            </li>
            {{#if role.canRotateStaticCreds}}
              <li class="action">
                <Menu.Message
                  data-test-rotate-creds
                  @id={{concat "rotate-" role.id}}
                  @triggerText="Rotate credentials"
                  @title="Are you sure?"
                  @message="When manually rotating credentials, the rotation period will start over."
                  @confirmButtonText="Rotate"
                  @onConfirm={{fn this.onRotate role}}
                />
              </li>
            {{/if}}
            <li class="action">
              <LinkTo
                class="has-text-black has-text-weight-semibold"
                data-test-details
                @route="roles.role.details"
                {{! this will force the roles.role model hook to fire since we may only have a partial model loaded in the list view }}
                @models={{array role.type role.name}}
                @disabled={{not role.canRead}}
              >
                Details
              </LinkTo>
            </li>
            {{#if role.canDelete}}
              <li class="action">
                <Menu.Message
                  data-test-delete
                  @id={{concat "delete-" role.id}}
                  @triggerText="Delete"
                  @title="Are you sure?"
                  @message="Deleting this role means that you’ll need to recreate it in order to generate credentials again."
                  @onConfirm={{fn this.onDelete role}}
                />
              </li>
            {{/if}}
          {{/if}}
        </Item.menu>
      </ListItem>
    {{/each}}

    <Hds::Pagination::Numbered
      @currentPage={{@roles.meta.currentPage}}
      @currentPageSize={{@roles.meta.pageSize}}
      @route="roles"
      @showSizeSelector={{false}}
      @totalItems={{@roles.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{/if}}