<PageHeader as |p|>
  <p.levelLeft>
    <h1 class="title is-3">
      Secrets Engines
    </h1>
  </p.levelLeft>
</PageHeader>

<Toolbar>
  <ToolbarActions>
    <ToolbarLink @route="vault.cluster.settings.mount-secret-backend" @type="add" data-test-enable-engine>
      Enable new engine
    </ToolbarLink>
  </ToolbarActions>
</Toolbar>

{{#each this.supportedBackends as |backend|}}
  {{#let
    (if
      (eq backend.engineType "kmip")
      "vault.cluster.secrets.backend.kmip.scopes"
      (if
        (eq backend.engineType "database") "vault.cluster.secrets.backend.overview" "vault.cluster.secrets.backend.list-root"
      )
    )
    as |backendLink|
  }}
    <LinkableItem data-test-secret-backend-row={{backend.id}} @link={{hash route=backendLink model=backend.id}} as |Li|>
      <Li.content
        @accessor={{if (eq backend.version 2) (concat "v2 " backend.accessor) backend.accessor}}
        @description={{backend.description}}
        @glyphText={{backend.engineType}}
        @glyph={{backend.icon}}
        @link={{hash route=backendLink model=backend.id}}
        @title={{backend.path}}
      />
      <Li.menu>
        <PopupMenu @name="engine-menu">
          <Confirm as |c|>
            <nav class="menu" aria-label="supported secrets engine menu">
              <ul class="menu-list">
                <li class="action">
                  <LinkTo @route="vault.cluster.secrets.backend.configuration" @model={{backend.id}}>
                    View configuration
                  </LinkTo>
                </li>
                {{#if (not-eq backend.type "cubbyhole")}}
                  <li class="action">
                    <c.Message
                      @id={{backend.id}}
                      @triggerText="Disable"
                      @message="Any data in this engine will be permanently deleted."
                      @title="Disable engine?"
                      @confirmButtonText="Disable"
                      @onConfirm={{perform this.disableEngine backend}}
                      data-test-engine-disable="true"
                    />
                  </li>
                {{/if}}
                {{#if this.item.updatePath.isPending}}
                  <li class="action">
                    <button disabled type="button" class="link button is-loading is-transparent">
                      loading
                    </button>
                  </li>
                {{/if}}
              </ul>
            </nav>
          </Confirm>
        </PopupMenu>
      </Li.menu>
    </LinkableItem>
  {{/let}}
{{/each}}

{{#each this.unsupportedBackends as |backend|}}
  <LinkableItem data-test-secret-backend-row={{backend.id}} @disabled={{true}} as |Li|>
    <Li.content
      @accessor={{if (eq backend.version 2) (concat "v2 " backend.accessor) backend.accessor}}
      @description={{backend.description}}
      @glyphText={{backend.engineType}}
      @glyph={{or (if (eq backend.engineType "kmip") "secrets" backend.engineType) "secrets"}}
      @title={{backend.path}}
    />
    <Li.menu>
      <PopupMenu name="engine-menu">
        <Confirm as |c|>
          <nav class="menu" aria-label="unsupported secrets engine menu">
            <ul class="menu-list">
              <li class="action">
                <LinkTo @route="vault.cluster.secrets.backend.configuration" @model={{backend.id}} data-test-engine-config>
                  View configuration
                </LinkTo>
              </li>
              <li>
                <c.Message
                  @id={{backend.id}}
                  @triggerText="Disable"
                  @message="Any data in this engine will be permanently deleted."
                  @title="Disable engine?"
                  @confirmButtonText="Disable"
                  @onConfirm={{perform this.disableEngine backend}}
                  data-test-engine-disable="true"
                />
              </li>
            </ul>
          </nav>
        </Confirm>
      </PopupMenu>
    </Li.menu>
  </LinkableItem>
{{/each}}