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

{{! private_key is only available after initial save }}
{{#if this.generatedKey.private_key}}
  <Page::PkiKeyDetails
    @key={{this.generatedKey}}
    @canDelete={{this.generatedKeyCapabilities.canDelete}}
    @canEdit={{this.generatedKeyCapabilities.canUpdate}}
  />
{{else}}
  <form {{on "submit" (perform this.save)}}>
    <div class="box is-sideless is-fullwidth is-marginless">
      <MessageError @errorMessage={{this.errorBanner}} class="has-top-margin-s" />
      <NamespaceReminder @mode={{if @form.isNew "generate" "update"}} @noun="PKI key" />

      {{#if @form.isNew}}
        {{#each @form.formFieldGroups as |fieldGroup|}}
          {{#each-in fieldGroup as |group fields|}}
            {{#if (eq group "Key parameters")}}
              <PkiKeyParameters @form={{@form}} @fields={{fields}} @modelValidations={{this.modelValidations}} />
            {{else}}
              {{#each fields as |field|}}
                <FormField
                  data-test-field={{field}}
                  @attr={{field}}
                  @model={{@form}}
                  @modelValidations={{this.modelValidations}}
                  @showHelpText={{false}}
                />
              {{/each}}
            {{/if}}
          {{/each-in}}
        {{/each}}
      {{else}}
        {{#each @form.formFields as |field|}}
          {{! only key name is edit-able }}
          {{#if (eq field.name "key_name")}}
            <FormField
              data-test-field={{field}}
              @attr={{field}}
              @model={{@form}}
              @modelValidations={{this.modelValidations}}
              @showHelpText={{false}}
            />
          {{else}}
            <ReadonlyFormField @attr={{field}} @value={{get @form.data field.name}} />
          {{/if}}
        {{/each}}
      {{/if}}
    </div>
    <Hds::ButtonSet class="has-top-padding-s">
      <Hds::Button
        @text={{if @form.isNew "Generate key" "Edit key"}}
        @icon={{if this.save.isRunning "loading"}}
        type="submit"
        disabled={{this.save.isRunning}}
        data-test-submit
      />
      <Hds::Button
        @text="Cancel"
        @color="secondary"
        disabled={{this.save.isRunning}}
        {{on "click" this.onCancel}}
        data-test-cancel
      />
    </Hds::ButtonSet>
    {{#if this.invalidFormAlert}}
      <div class="control">
        <AlertInline
          @type="danger"
          class="has-top-padding-s"
          @message={{this.invalidFormAlert}}
          data-test-pki-key-validation-error
        />
      </div>
    {{/if}}
  </form>
{{/if}}