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

<TabPageHeader @model={{@backendModel}} @breadcrumbs={{@breadcrumbs}}>
  <:toolbarActions>
    {{#if @promptConfig}}
      <ToolbarLink @route="configure" data-test-toolbar-action="config">
        Configure LDAP
      </ToolbarLink>
    {{/if}}
  </:toolbarActions>
</TabPageHeader>

{{#if @promptConfig}}
  <ConfigCta />
{{else}}
  <div class="is-grid has-top-margin-l grid-2-columns grid-gap-2">
    <OverviewCard
      @cardTitle="Roles"
      @subText="The total number of roles that have been set up in this secret engine in order to generate credentials."
    >
      <:action>
        <Hds::Link::Standalone @text="Create new" @route="roles.create" @icon="plus" @iconPosition="trailing" />
      </:action>
      <:content>
        <h2 class="title is-2 has-font-weight-normal has-top-margin-m" data-test-roles-count>
          {{or @roles.length "None"}}
        </h2>
      </:content>
    </OverviewCard>
    <OverviewCard
      @cardTitle="Libraries"
      @subText="The total number of libraries that have been created for service account management."
    >
      <:action>
        <Hds::Link::Standalone @text="Create new" @route="libraries.create" @icon="plus" @iconPosition="trailing" />
      </:action>
      <:content>
        {{#if this.librariesError}}
          <Hds::Alert @type="inline" @color="critical" class="has-top-margin-s" data-test-libraries-error as |A|>
            <A.Title>Error loading libraries</A.Title>
            <A.Description>{{this.librariesError}}</A.Description>
          </Hds::Alert>
        {{else}}
          <h2 class="title is-2 has-font-weight-normal has-top-margin-m" data-test-libraries-count>
            {{#if this.fetchLibraries.isRunning}}
              <Hds::Icon @name="loading" @size="24" @color="neutral" />
            {{else}}
              {{or this.allLibraries.length "None"}}
            {{/if}}
          </h2>
        {{/if}}
      </:content>
    </OverviewCard>
  </div>
  <div class="is-grid has-top-margin-l grid-2-columns grid-gap-2">
    <AccountsCheckedOut
      @libraries={{this.allLibraries}}
      @statuses={{this.librariesStatus}}
      @showLibraryColumn={{true}}
      @onCheckInSuccess={{transition-to "vault.cluster.secrets.backend.ldap.overview"}}
      @isLoadingStatuses={{or this.fetchLibraries.isRunning this.fetchLibrariesStatus.isRunning}}
      class="is-flex-half"
    />
    <div>
      <OverviewCard
        @cardTitle="Generate credentials"
        @subText="Quickly generate credentials by typing the role name. Only the engine's top-level roles are listed here."
      >
        <:content>
          <div class="has-top-margin-m is-flex">
            <SearchSelect
              class="is-flex-1"
              @ariaLabel="Role"
              @placeholder="Select a role"
              @disallowNewItems={{true}}
              @options={{this.roleOptions}}
              @selectLimit="1"
              @fallbackComponent="input-search"
              @onChange={{this.selectRole}}
              @passObject={{true}}
              @objectKeys={{array "id" "name" "type"}}
              @shouldRenderName={{true}}
            />
            <div>
              <Hds::Button
                @text="Get credentials"
                @color="secondary"
                class="has-left-margin-s"
                disabled={{not this.selectedRole}}
                {{on "click" this.generateCredentials}}
                data-test-generate-credential-button
              />
            </div>
          </div>
        </:content>
      </OverviewCard>
    </div>
  </div>
{{/if}}