{{!
  Copyright IBM Corp. 2016, 2025
  SPDX-License-Identifier: BUSL-1.1
}}

<Page::Header @title={{@roleName}}>
  <:breadcrumbs>
    <KmipBreadcrumb @scope={{@scopeName}} @currentRoute={{@roleName}} />
  </:breadcrumbs>
</Page::Header>
<KmipTabs @role={{@roleName}} @scope={{@scopeName}} @isCredentials={{true}} />

<Toolbar>
  {{#if @credentials.meta.total}}
    <ToolbarFilters>
      <FilterInput
        aria-label="Filter credentials by serial number"
        placeholder="Filter credentials by serial number"
        @value={{@filterValue}}
        @wait={{200}}
        @onInput={{this.onFilterChange}}
      />
    </ToolbarFilters>
  {{/if}}

  <ToolbarActions>
    <ToolbarLink @route={{"credentials.generate"}} @type="add" data-test-generate-credentials>
      Generate credentials
    </ToolbarLink>
  </ToolbarActions>
</Toolbar>

{{#if @credentials}}
  <div class="has-bottom-margin-s">
    {{#each @credentials as |cred|}}
      <div data-test-list-item={{cred}}>
        <ListItem
          @linkPrefix={{this.mountPoint}}
          @linkParams={{array "credentials.show" @scopeName @roleName cred}}
          as |Item|
        >
          <Item.content>
            <Icon @name="file-text" class="has-text-grey-light" />{{cred}}
          </Item.content>
          <Item.menu>
            <Hds::Dropdown @isInline={{true}} @listPosition="bottom-right" as |dd|>
              <dd.ToggleIcon
                @icon="more-horizontal"
                @text="More options"
                @hasChevron={{false}}
                data-test-popup-menu-trigger
              />
              <dd.Interactive
                @route="credentials.show"
                @models={{array @scopeName @roleName cred}}
                data-test-popup-menu="View credentials"
              >
                View credentials
              </dd.Interactive>
              {{#if @capabilities.canDelete}}
                <dd.Interactive
                  @color="critical"
                  {{on "click" (fn (mut this.credToRevoke) cred)}}
                  data-test-confirm-action-trigger
                >
                  Revoke credentials
                </dd.Interactive>
              {{/if}}
            </Hds::Dropdown>

            {{#if (eq this.credToRevoke cred)}}
              <ConfirmModal
                @color="critical"
                @onClose={{fn (mut this.credToRevoke) null}}
                @confirmTitle="Revoke this?"
                @confirmMessage="Any client using these credentials will no longer be able to."
                @onConfirm={{this.revoke}}
              />
            {{/if}}
          </Item.menu>
        </ListItem>
      </div>
    {{/each}}

    <Hds::Pagination::Numbered
      @currentPage={{@credentials.meta.currentPage}}
      @currentPageSize={{@credentials.meta.pageSize}}
      @route="credentials.index"
      @models={{array @scope @role}}
      @showSizeSelector={{false}}
      @totalItems={{@credentials.meta.filteredTotal}}
      @queryFunction={{this.paginationQueryParams}}
      data-test-pagination
    />
  </div>
{{else}}
  {{#if @filterValue}}
    <EmptyState @title="There are no credentials matching &quot;{{@filterValue}}&quot;" />
  {{else}}
    <EmptyState
      @title="No credentials yet for this role"
      @message="You can generate new credentials that will be limited to this role's allowed operations, then you can distribute them to your KMIP clients."
    >
      <Hds::Link::Standalone @icon="plus" @text="Generate credentials" @route="credentials.generate" />
    </EmptyState>
  {{/if}}
{{/if}}