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

<PageHeader as |p|>
  <p.top>
    <nav class="breadcrumb">
      <ul>
        <li>
          <span class="sep">&#x0002f;</span>
          <LinkTo @route="vault.cluster.secrets.backend" @model={{this.backendPath}} data-test-link="role-list">
            {{this.backendPath}}
          </LinkTo>
        </li>
        <li class="is-active">
          <span class="sep">&#x0002f;</span>
          <LinkTo @route="vault.cluster.secrets.backend" @model={{this.backendPath}}>
            creds
          </LinkTo>
        </li>
        <li>
          <span class="sep">&#x0002f;</span>
          <LinkTo @route="vault.cluster.secrets.backend.show" @model={{this.roleName}}>
            {{this.roleName}}
          </LinkTo>
        </li>
      </ul>
    </nav>
  </p.top>
  <p.levelLeft>
    <h1 data-test-title class="title is-3">
      {{this.options.title}}
    </h1>
  </p.levelLeft>
</PageHeader>

{{#if this.model.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.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}}
        {{#if
          (or
            (eq attr.name "key")
            (eq attr.name "secretKey")
            (eq attr.name "securityToken")
            (eq attr.name "privateKey")
            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" isFormatted=true}}
          />
        {{else}}
          <InfoTableRow
            @label={{capitalize (or attr.options.label (humanize (dasherize attr.name)))}}
            @value={{get this.model attr.name}}
          />
        {{/if}}
      {{/if}}
    {{/each}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <div class="control">
      <CopyButton
        @clipboardText={{this.model.toCreds}}
        @class="button is-primary"
        @buttonType="button"
        @success={{action (set-flash-message "Credentials copied!")}}
      >
        Copy credentials
      </CopyButton>
    </div>
    {{#if this.model.leaseId}}
      <div class="control">
        <CopyButton
          @clipboardText={{this.model.leaseId}}
          @class="button"
          @buttonType="button"
          @success={{action (set-flash-message "Lease ID copied!")}}
        >
          Copy Lease ID
        </CopyButton>
      </div>
    {{/if}}
    <div class="control">
      {{#if this.options.backIsListLink}}
        <LinkTo
          @route="vault.cluster.secrets.backend.list-root"
          @model={{this.backendPath}}
          data-test-secret-generate-back={{true}}
          class="button"
        >
          Back
        </LinkTo>
      {{else}}
        <button type="button" {{action "newModel"}} class="button" data-test-secret-generate-back="true">
          Back
        </button>
      {{/if}}
    </div>
  </div>
{{else}}
  <form {{action "create" on="submit"}} data-test-secret-generate-form="true">
    <div class="box is-sideless no-padding-top is-fullwidth is-marginless">
      <NamespaceReminder @mode="generate" @noun="credential" />
      <MessageError @model={{this.model}} />
      {{#if this.model.helpText}}
        <p class="is-hint">{{this.model.helpText}}</p>
      {{/if}}
      {{#if this.model.fieldGroups}}
        <FormFieldGroupsLoop @model={{this.model}} @mode={{this.mode}} />
      {{else}}
        {{#each this.model.attrs as |attr|}}
          <FormField data-test-field={{true}} @attr={{attr}} @model={{this.model}} />
        {{/each}}
      {{/if}}
    </div>
    <div class="field is-grouped box is-fullwidth is-bottomless">
      <div class="control">
        <button
          type="submit"
          disabled={{this.loading}}
          class="button is-primary {{if this.loading 'is-loading'}}"
          data-test-secret-generate={{true}}
        >
          Generate
        </button>
      </div>
      <div class="control">
        <LinkTo
          @route="vault.cluster.secrets.backend.list-root"
          @model={{this.backendPath}}
          class="button"
          data-test-secret-generate-cancel={{true}}
        >
          Cancel
        </LinkTo>
      </div>
    </div>
  </form>
{{/if}}