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

<Toolbar>
  <ToolbarActions>
    {{#if @role.canDelete}}
      <ConfirmAction
        class="toolbar-button"
        @buttonColor="secondary"
        @onConfirmAction={{this.deleteRole}}
        @confirmTitle="Delete role?"
        @buttonText="Delete"
        data-test-pki-role-delete
      />
      <div class="toolbar-separator"></div>
    {{/if}}
    {{#if @role.canGenerateCert}}
      <LinkTo
        class="toolbar-link"
        @route="roles.role.generate"
        @models={{array @role.backend @role.id}}
        data-test-pki-role-generate-cert
      >
        Generate Certificate
        <Icon @name="chevron-right" />
      </LinkTo>
    {{/if}}
    {{#if @role.canSign}}
      <LinkTo
        class="toolbar-link"
        @route="roles.role.sign"
        @models={{array @role.backend @role.id}}
        data-test-pki-role-sign-cert
      >
        Sign Certificate
        <Icon @name="chevron-right" />
      </LinkTo>
    {{/if}}
    {{#if @role.canEdit}}
      <LinkTo
        class="toolbar-link"
        @route="roles.role.edit"
        @models={{array @role.backend @role.id}}
        data-test-pki-role-edit-link
      >
        Edit
        <Icon @name="chevron-right" />
      </LinkTo>
    {{/if}}
  </ToolbarActions>
</Toolbar>
{{#each @role.formFieldGroups as |fg|}}
  {{#each-in fg as |group fields|}}
    {{#if (not-eq group "default")}}
      <h3 class="is-size-4 has-text-weight-semibold has-top-margin-m">{{group}}</h3>
    {{/if}}
    {{#each fields as |attr|}}
      {{#let (get @role attr.name) as |val|}}
        {{#if (eq attr.name "issuerRef")}}
          <InfoTableRow
            @label={{capitalize (or attr.options.detailsLabel attr.options.label (humanize (dasherize attr.name)))}}
            @value={{val}}
            @alwaysRender={{true}}
          >
            <LinkTo @route="issuers.issuer.details" @models={{array @role.backend val}}>{{val}}</LinkTo>
          </InfoTableRow>
        {{else if (includes attr.name this.arrayAttrs)}}
          <InfoTableRow
            @label={{capitalize (or attr.options.detailsLabel attr.options.label (humanize (dasherize attr.name)))}}
            @value={{val}}
            @alwaysRender={{true}}
          />
        {{else if (includes attr.name (array "noStore" "noStoreMetadata"))}}
          <InfoTableRow
            @label={{capitalize (or attr.options.detailsLabel attr.options.label (humanize (dasherize attr.name)))}}
            @value={{not val}}
            @alwaysRender={{true}}
          />
        {{else if (eq attr.name "customTtl")}}
          {{! Show either notAfter or ttl }}
          <InfoTableRow
            @label={{capitalize (or attr.options.detailsLabel attr.options.label (humanize (dasherize attr.name)))}}
            @value={{or @role.notAfter @role.ttl}}
            @alwaysRender={{true}}
            @formatDate={{if @role.notAfter "MMM d yyyy HH:mm zzzz"}}
            @formatTtl={{@role.ttl}}
          />
        {{else}}
          <InfoTableRow
            @label={{capitalize (or attr.options.detailsLabel attr.options.label (humanize (dasherize attr.name)))}}
            @value={{val}}
            @alwaysRender={{true}}
            @type={{or attr.type attr.options.type}}
            @defaultShown={{attr.options.defaultShown}}
            @formatTtl={{eq attr.options.editType "ttl"}}
          />
        {{/if}}
      {{/let}}
    {{/each}}
  {{/each-in}}
{{/each}}