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

<form {{on "submit" (perform this.save)}} data-test-policy-form>
  <div class="box is-shadowless is-fullwidth is-marginless">
    <MessageError @errorMessage={{this.errorBanner}} />
    <NamespaceReminder @mode={{if @model.isNew "create" "edit"}} @noun="policy" />
    {{#if @model.isNew}}
      <div class="field">
        <label for="policy-name" class="is-label">Name</label>
        <div class="control">
          <Input
            @type="text"
            @value={{lowercase @model.name}}
            id="policy-name"
            class="input"
            {{on "input" this.setModelName}}
            data-test-policy-input="name"
          />
        </div>
      </div>
    {{/if}}
    <div class="field">
      <Toolbar aria-label="toolbar for managing {{or @model.name 'new'}} policy">
        {{#if @renderPolicyExampleModal}}
          {{! only true in policy create and edit routes }}
          <ToolbarFilters aria-label="help tools for managing {{or @model.name 'new'}} policy">
            <Hds::Button
              @text="How to write a policy"
              @icon="bulb"
              @size="small"
              @color="tertiary"
              {{on "click" (fn (mut this.showTemplateModal))}}
              data-test-button="How to write a policy"
            />
          </ToolbarFilters>
        {{/if}}
        {{#if @model.isNew}}
          <ToolbarActions aria-label="actions for managing {{or @model.name 'new'}} policy">
            <div class="control is-flex">
              <Input
                id="fileUploadToggle"
                @type="checkbox"
                name="fileUploadToggle"
                class="toggle is-success is-small"
                @checked={{this.showFileUpload}}
                {{on "change" (fn (mut this.showFileUpload) (not this.showFileUpload))}}
                data-test-policy-edit-toggle
              />
              <label for="fileUploadToggle" class="has-text-weight-bold is-size-8">Upload file</label>
            </div>
          </ToolbarActions>
        {{/if}}
      </Toolbar>
      {{#if this.showFileUpload}}
        <div class="has-top-margin-xs">
          <TextFile @uploadOnly={{true}} @onChange={{this.setPolicyFromFile}} />
        </div>
      {{else}}
        <JsonEditor
          @title="Policy"
          @value={{@model.policy}}
          @valueUpdated={{action (mut @model.policy)}}
          @mode="ruby"
          @extraKeys={{hash Shift-Enter=(perform this.save)}}
          data-test-policy-editor
        />
      {{/if}}
    </div>
    {{#each @model.additionalAttrs as |attr|}}
      <FormField data-test-field={{true}} @attr={{attr}} @model={{@model}} />
    {{/each}}
  </div>
  <div class="field is-grouped box is-fullwidth is-bottomless">
    <Hds::ButtonSet>
      <Hds::Button
        @text={{if @model.isNew "Create policy" "Save"}}
        @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.cancel}}
        data-test-cancel
      />
    </Hds::ButtonSet>
  </div>
</form>

{{! SAMPLE POLICY MODAL. Only renders in policy create and edit routes }}
{{#if this.showTemplateModal}}
  <Hds::Modal
    id="policy-example-modal"
    @size="large"
    @onClose={{fn (mut this.showTemplateModal) false}}
    data-test-policy-example-modal
    as |M|
  >
    <M.Header data-test-modal-title>
      Example
      {{uppercase @model.policyType}}
      Policy
    </M.Header>
    <M.Body>
      <PolicyExample @policyType={{@model.policyType}} />
    </M.Body>
    <M.Footer as |F|>
      <Hds::Button @text="Close" {{on "click" F.close}} data-test-modal-close-button />
    </M.Footer>
  </Hds::Modal>
{{/if}}