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

<PageHeader as |p|>
  <p.top>
    <Hds::Breadcrumb>
      <Hds::Breadcrumb::Item
        @text={{@backendPath}}
        @route="vault.cluster.secrets.backend"
        @model={{@backendPath}}
        data-test-link="role-list"
      />
      <Hds::Breadcrumb::Item @text="Credentials" @route="vault.cluster.secrets.backend" @model={{@backendPath}} />
      <Hds::Breadcrumb::Item @text={{@roleName}} @route="vault.cluster.secrets.backend.show" @model={{@roleName}} />
      <Hds::Breadcrumb::Item @text={{this.options.title}} @current={{true}} />
    </Hds::Breadcrumb>
  </p.top>
  <p.levelLeft>
    <h1 data-test-title class="title is-3">
      {{this.options.title}}
    </h1>
  </p.levelLeft>
</PageHeader>

{{#if this.hasGenerated}}
  <div class="box is-fullwidth is-sideless is-paddingless is-marginless">
    <MessageError @model={{this.model}} />
    {{#unless this.model.isError}}
      <Hds::Alert @type="inline" @color="warning" class="has-top-bottom-margin" data-test-warning 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>
    {{/unless}}
    {{#each this.displayFields as |key|}}
      {{#let (get this.model.allByKey key) 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}}
          {{#if attr.options.masked}}
            {{#if (get this.model attr.name)}}
              <InfoTableRow
                @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
                @value={{get this.model attr.name}}
              >
                <MaskedInput
                  @value={{get this.model attr.name}}
                  @name={{attr.name}}
                  @displayOnly={{true}}
                  @allowCopy={{true}}
                />
              </InfoTableRow>
            {{/if}}
          {{else if (and (get this.model attr.name) (or (eq attr.name "issueDate") (eq attr.name "expiryDate")))}}
            <InfoTableRow
              data-test-table-row
              @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
              @value={{date-format (get this.model attr.name) "MMM dd, yyyy hh:mm:ss a"}}
            />
          {{else}}
            <InfoTableRow
              @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
              @value={{get this.model attr.name}}
            />
          {{/if}}
        {{/if}}
      {{/let}}
    {{/each}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Copy::Button
        @text="Copy credentials"
        @textToCopy={{this.model.toCreds}}
        @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">
      {{#if this.options.backIsListLink}}
        <Hds::Button
          @text="Back"
          @color="secondary"
          @route="vault.cluster.secrets.backend.list-root"
          @model={{@backendPath}}
          data-test-back-button
        />
      {{else}}
        <Hds::Button @text="Back" @color="secondary" {{on "click" this.reset}} data-test-back-button />
      {{/if}}
    </div>
  </div>
{{else}}
  <form {{on "submit" this.create}} data-test-secret-generate-form>
    <div class="box is-sideless no-padding-top is-fullwidth is-marginless">
      <NamespaceReminder @mode="generate" @noun="credential" />
      <MessageError @model={{this.model}} />
      {{#if this.helpText}}
        <p class="is-hint">{{this.helpText}}</p>
      {{/if}}
      {{#each this.formFields as |key|}}
        <FormField data-test-field @attr={{get this.model.allByKey key}} @model={{this.model}} />
      {{/each}}
    </div>
    <div class="field is-grouped box is-fullwidth is-bottomless">
      <Hds::ButtonSet>
        <Hds::Button
          @text="Generate"
          @icon={{if this.loading "loading"}}
          type="submit"
          disabled={{this.loading}}
          data-test-save
        />
        <Hds::Button
          @text="Cancel"
          @route="vault.cluster.secrets.backend.list-root"
          @color="secondary"
          @model={{@backendPath}}
          data-test-cancel
        />
      </Hds::ButtonSet>
    </div>
  </form>
{{/if}}