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

<PageHeader as |p|>
  <p.top>
    <Hds::Breadcrumb>
      {{#if @model.isNew}}
        <Hds::Breadcrumb::Item @text="Applications" @route="vault.cluster.access.oidc.clients" />
      {{else}}
        <Hds::Breadcrumb::Item
          @text="Details"
          @route="vault.cluster.access.oidc.clients.client.details"
          @model={{@model.name}}
        />
      {{/if}}
      <Hds::Breadcrumb::Item @text="{{if @model.isNew 'Create' 'Edit'}} Application" @current={{true}} />
    </Hds::Breadcrumb>
  </p.top>
  <p.levelLeft>
    <h1 class="title is-3" data-test-oidc-client-title>
      {{if @model.isNew "Create" "Edit"}}
      Application
    </h1>
  </p.levelLeft>
</PageHeader>
<form {{on "submit" (perform this.save)}}>
  <div class="box is-sideless is-fullwidth is-bottomless">
    <MessageError @errorMessage={{this.errorBanner}} />
    {{#each @model.formFields as |attr|}}
      <FormField @attr={{attr}} @model={{@model}} @modelValidations={{this.modelValidations}} />
    {{/each}}

    {{! MORE OPTIONS TOGGLE }}
    <FormFieldGroups @renderGroup="More options" @model={{@model}} @modelValidations={{this.modelValidations}} />
  </div>
  {{! RADIO CARD + SEARCH SELECT }}
  <div class="box is-sideless is-fullwidth is-marginless has-top-padding-xxl">
    <Hds::Text::Display @tag="h2" @size="400">Assign access</Hds::Text::Display>
    <div class="is-flex-row">
      <RadioCard
        data-test-oidc-radio="allow-all"
        @title="Allow everyone to access existing"
        @description="All Vault entities can authenticate through this application."
        @icon="org"
        @value="allow_all"
        @groupValue={{this.radioCardGroupValue}}
        @onChange={{this.handleAssignmentSelection}}
      />
      <RadioCard
        data-test-oidc-radio="limited"
        @title="Limit access to selected users"
        @description="Choose or create an assignment to give access to selected entities."
        @icon="users"
        @value="limited"
        @groupValue={{this.radioCardGroupValue}}
        @onChange={{this.handleAssignmentSelection}}
      />
    </div>
    {{#if (eq this.radioCardGroupValue "limited")}}
      <SearchSelectWithModal
        @id="assignments"
        @label="Assignment name"
        @subText="Search for an existing assignment, or type a new name to create it."
        @models={{array "oidc/assignment"}}
        @inputValue={{this.modelAssignments}}
        @onChange={{this.handleAssignmentSelection}}
        @excludeOptions={{array "allow_all"}}
        @fallbackComponent="string-list"
        @modalFormTemplate="modal-form/oidc-assignment-template"
        @modalSubtext="Use assignment to specify which Vault entities and groups are allowed to authenticate."
      />
    {{/if}}
  </div>
  <div class="field box is-fullwidth is-bottomless">
    <div class="control">
      <Hds::Button
        @text={{if @model.isNew "Create" "Update"}}
        @icon={{if this.save.isRunning "loading"}}
        type="submit"
        disabled={{this.save.isRunning}}
        data-test-oidc-client-save
      />
      <Hds::Button
        @text="Cancel"
        @color="secondary"
        class="has-left-margin-s"
        disabled={{this.save.isRunning}}
        {{on "click" this.cancel}}
        data-test-oidc-client-cancel
      />
    </div>
    {{#if this.invalidFormAlert}}
      <div class="control">
        <AlertInline @type="danger" class="has-top-padding-s" @message={{this.invalidFormAlert}} />
      </div>
    {{/if}}
  </div>
</form>