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

<h3 class="title is-4">Intermediates</h3>
<div class="box is-fullwidth has-only-top-shadow">
  <p class="has-bottom-margin-l">
    Intermediates are identified by specifying the mount path and the issuer’s current name, as well as the new name it will
    have when it is imported to Vault.
  </p>
  {{#if this.signedIssuers}}
    <h2 data-test-cross-sign-status-count class="title is-5">Cross-signing complete ({{this.statusCount}})</h2>
    <p class="has-bottom-margin-xl">
      The next step is to
      <DocLink class="has-font-weight-normal" @path="/vault/docs/secrets/pki/rotation-primitives#notes-on-manual_chain">
        update the manual_chain<Icon @name="docs-link" />
      </DocLink>
      for each of your new certificates listed below, and the existing issuer.
    </p>
    <div class="is-flex-start">
      {{#each this.inputFields as |column|}}
        <div class="is-flex-grow-1 basis-0 has-text-grey has-bottom-margin-xs">
          <h2 data-test-info-table-label={{column.key}}>
            {{column.label}}
            {{#if column.helpText}}
              <InfoTooltip @verticalPosition="above" @horizontalPosition="center">{{column.helpText}}</InfoTooltip>
            {{/if}}
          </h2>
        </div>
      {{/each}}
    </div>
    <div class="box is-fullwidth is-sideless">
      {{#each this.signedIssuers as |crossSignRow idx|}}
        <div class="box is-marginless no-top-shadow has-slim-padding">
          <div data-test-info-table-row={{idx}} class="is-flex-start">
            <Icon
              @name={{if crossSignRow.hasError "alert-circle-fill" "check-circle"}}
              class={{if crossSignRow.hasError "has-text-danger" "has-text-success"}}
            />
            {{#each (map-by "key" this.inputFields) as |columnAttr|}}
              {{#let (get crossSignRow columnAttr) as |data|}}
                <div data-test-info-table-column={{columnAttr}} class="is-flex-grow-1 basis-0 has-bottom-margin-xs">
                  {{#if (eq columnAttr "intermediateMount")}}
                    <LinkTo class="has-text-black has-text-weight-semibold" @route="overview" @model={{data}}>
                      {{data}}
                    </LinkTo>
                  {{else if crossSignRow.hasError}}
                    <span class="has-text-grey has-text-weight-semibold">
                      {{data}}
                    </span>
                  {{else}}
                    <LinkToExternal
                      class="has-text-black has-text-weight-semibold"
                      @route="externalMountIssuer"
                      @models={{array (get crossSignRow "intermediateMount") data.issuerId}}
                    >
                      {{data.issuerRef}}
                    </LinkToExternal>
                  {{/if}}
                </div>
              {{/let}}
            {{/each}}
          </div>
          {{#if crossSignRow.hasError}}
            <Hds::Alert @type="inline" @color="critical" as |A|>
              <A.Title data-test-cross-sign-alert-title>
                {{if crossSignRow.hasUnsupportedParams crossSignRow.hasError "Cross-sign failed"}}
              </A.Title>
              <A.Description data-test-cross-sign-alert-message>
                {{if crossSignRow.hasUnsupportedParams crossSignRow.hasUnsupportedParams crossSignRow.hasError}}
              </A.Description>
            </Hds::Alert>
          {{/if}}
        </div>
      {{/each}}
    </div>
    <Hds::ButtonSet>
      <Hds::Button @text="Done" @route="issuers.issuer.details" data-test-cross-sign-done />
      <Hds::Button @text="Sign more issuers" @color="secondary" data-test-cross-sign-more {{on "click" this.reset}} />
    </Hds::ButtonSet>
  {{else}}
    <form {{on "submit" (perform this.submit)}} data-test-cross-sign-form>
      <ObjectListInput
        @objectKeys={{this.inputFields}}
        @onChange={{fn (mut this.formData)}}
        @validationErrors={{this.validationErrors}}
      />
      <hr class="has-background-gray-100" />
      <Hds::ButtonSet>
        <Hds::Button
          @text="Cross-sign"
          @icon={{if this.submit.isRunning "loading"}}
          type="submit"
          disabled={{this.submit.isRunning}}
          data-test-save
        />
        <Hds::Button
          @text="Cancel"
          @color="secondary"
          @route="issuers.issuer.details"
          disabled={{this.submit.isRunning}}
          data-test-cancel
        />
      </Hds::ButtonSet>
    </form>
  {{/if}}
</div>