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

<PageHeader as |p|>
  <p.top>
    <Hds::Breadcrumb>
      <Hds::Breadcrumb::Item @text="SSH" @route="vault.cluster.secrets.backend" @model={{this.backend.id}} />
      <Hds::Breadcrumb::Item @text="Sign" @route="vault.cluster.secrets.backend" @model={{this.backend.id}} />
      <Hds::Breadcrumb::Item
        @text={{this.model.role.name}}
        @route="vault.cluster.secrets.backend.show"
        @model={{this.model.role.name}}
      />
      <Hds::Breadcrumb::Item @text="Sign SSH Key" @current={{true}} />
    </Hds::Breadcrumb>
  </p.top>
  <p.levelLeft>
    <h1 class="title is-3">
      Sign SSH Key
    </h1>
  </p.levelLeft>
</PageHeader>

{{#if this.model.signedKey}}
  <div class="box is-fullwidth is-sideless is-paddingless is-marginless">
    <Hds::Alert @type="inline" @color="warning" class="has-top-margin-s has-bottom-margin-s" as |A|>
      <A.Title>Warning</A.Title>
      <A.Description>
        You will not be able to access this information later, so please copy the information below.
      </A.Description>
    </Hds::Alert>
    {{#each this.model.attrs as |attr|}}
      {{#if (eq attr.type "object")}}
        <InfoTableRow
          @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
          @value={{stringify (get this.model attr.name)}}
        />
      {{else}}
        <InfoTableRow
          @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
          @value={{get this.model attr.name}}
        />
      {{/if}}
    {{/each}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Copy::Button
        @text="Copy key"
        @textToCopy={{this.model.signedKey}}
        @onError={{(fn (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger"))}}
        class="primary"
      />
    </div>
    {{#if this.model.leaseId}}
      <div class="control">
        <Hds::Copy::Button
          @text="Copy lease ID"
          @textToCopy={{this.model.leaseId}}
          @onError={{(fn
            (set-flash-message "Clipboard copy failed. The Clipboard API requires a secure context." "danger")
          )}}
          class="secondary"
        />
      </div>
    {{/if}}
    <div class="control">
      <Hds::Button @text="Back" @color="secondary" {{on "click" (action "newModel")}} data-test-back-button />
    </div>
  </div>
{{else}}
  <form {{action "sign" on="submit"}} data-test-secret-generate-form="true">
    <div class="box is-sideless is-fullwidth is-marginless">
      <MessageError @model={{this.model}} />
      <NamespaceReminder @mode="sign" @noun="SSH key" />
      {{#if this.model.attrs}}
        {{#let (find-by "name" "publicKey" this.model.attrs) as |attr|}}
          <FormFieldFromModel @attr={{attr}} @model={{this.model}} />
        {{/let}}
        {{! valid_principals is required unless allow_empty_principals is true (not recommended) }}
        {{#let (find-by "name" "validPrincipals" this.model.attrs) as |attr|}}
          <FormFieldFromModel @attr={{attr}} @model={{this.model}} />
        {{/let}}
        <ToggleButton @isOpen={{this.showOptions}} @onClick={{fn (mut this.showOptions)}} data-test-toggle-button />
        {{#if this.showOptions}}
          <div class="box is-marginless">
            {{#each this.model.attrs as |attr|}}
              {{! These attrs render above, outside of the "More options" toggle }}
              {{#if (not (includes attr.name (array "publicKey" "validPrincipals")))}}
                <FormFieldFromModel
                  @attr={{attr}}
                  @model={{this.model}}
                  @updateTtl={{action "updateTtl" attr.name}}
                  @emptyData={{this.emptyData}}
                  @editorUpdated={{action "editorUpdated" attr.name}}
                />
              {{/if}}
            {{/each}}
          </div>
        {{/if}}
      {{/if}}
    </div>
    <Hds::ButtonSet class="has-top-bottom-margin">
      <Hds::Button
        @text="Sign"
        @icon={{if this.loading "loading"}}
        type="submit"
        disabled={{this.loading}}
        data-test-submit
      />
      <Hds::Button
        @text="Cancel"
        @color="secondary"
        @route="vault.cluster.secrets.backend.list-root"
        @model={{this.backend.id}}
        data-test-cancel
      />
    </Hds::ButtonSet>
  </form>
{{/if}}