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

{{#if this.config}}
  {{! Model only has ID once form has been submitted and saved }}
  <main data-test-generate-csr-result>
    <div class="box is-sideless is-fullwidth is-shadowless">
      <Hds::Alert data-test-next-steps-csr @type="inline" @color="highlight" class="has-bottom-margin-s" as |A|>
        <A.Title>Next steps</A.Title>
        <A.Description>
          Copy the CSR below for a parent issuer to sign and then import the signed certificate back into this mount.
          {{#if this.config.private_key}}
            The
            <code>private_key</code>
            is only available once. Make sure you copy and save it now.
          {{/if}}
        </A.Description>
      </Hds::Alert>

      {{#each this.returnedFields as |fieldName|}}
        {{#let (get this.config fieldName) as |value|}}
          <InfoTableRow @label={{this.detailLabel fieldName}} @value={{value}} @addCopyButton={{eq fieldName "key_id"}}>
            {{#if value}}
              {{#if (includes fieldName (array "csr" "private_key"))}}
                <CertificateCard @data={{value}} />
              {{else if (eq fieldName "key_id")}}
                <LinkTo @route="keys.key.details" @models={{array this.secretMountPath.currentPath value}}>
                  {{value}}
                </LinkTo>
              {{else}}
                {{value}}
                {{! it's unlikely but if a value is returned for private_key and private_key_type we want to display it, otherwise we show the "internal" badge below }}
              {{/if}}
            {{else}}
              <Hds::Badge @text="internal" />
            {{/if}}
          </InfoTableRow>
        {{/let}}
      {{/each}}
    </div>
  </main>
  <footer>
    <div class="field is-grouped is-fullwidth has-top-margin-l">
      <Hds::Button @text="Done" {{on "click" @onComplete}} data-test-done />
    </div>
  </footer>
{{else}}
  <form {{on "submit" (perform this.save)}}>
    <MessageError @errorMessage={{this.error}} class="has-top-margin-s" />
    <h2 class="title is-size-5 has-border-bottom-light page-header">
      CSR parameters
    </h2>

    {{#each this.defaultFields as |fieldName|}}
      {{#let (find-by "name" fieldName this.form.formFields) as |field|}}
        <FormField @attr={{field}} @model={{this.form}} @modelValidations={{this.modelValidations}} />
      {{/let}}
    {{/each}}

    <PkiGenerateToggleGroups @form={{this.form}} @actionType="generate-csr" @modelValidations={{this.modelValidations}} />

    <hr class="has-background-gray-100" />

    <Hds::ButtonSet>
      <Hds::Button @text="Generate" type="submit" data-test-submit />
      <Hds::Button @text="Cancel" @color="secondary" {{on "click" @onCancel}} data-test-cancel />
    </Hds::ButtonSet>

    {{#if this.alert}}
      <div class="control">
        <AlertInline @type="danger" class="has-top-padding-s" @message={{this.alert}} data-test-alert />
      </div>
    {{/if}}
  </form>
{{/if}}