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

<ModalDialog
  class="role-selector"
  data-test-role-form
  id="new-role"
  @onclose={{action (mut this.state) "role"}}
  @aria={{hash
    label=(if (eq this.state 'role') 'New Role' 'New Policy')
  }}
as |modal|>
  <Ref @target={{this}} @name="modal" @value={{modal}} />
  <BlockSlot @name="header">
{{#if (eq this.state 'role')}}
    <h2>New Role</h2>
{{else}}
    <h2>New Policy</h2>
{{/if}}
  </BlockSlot>
  <BlockSlot @name="body">

  <input id="{{this.name}}_state_role" type="radio" name="{{this.name}}[state]" value="role" checked={{if (eq this.state 'role') 'checked'}} onchange={{action 'change'}} />
    <RoleForm
      @form={{this.form}}
      @dc={{@dc}}
      @nspace={{@nspace}}
      @partition={{@partition}}
    >
      <BlockSlot @name="policy">

        <PolicySelector
          @source={{this.source}}
          @dc={{@dc}}
          @partition={{@partition}}
          @nspace={{@nspace}}
          @items={{this.item.Policies}}
        >
          <BlockSlot @name="trigger">
            <Hds::Button
              @text='Create new policy'
              @size='small'
              @color='tertiary'
              @icon='plus'
              class='type-dialog'
              data-test-create-policy
              {{action 'triggerStateCheckboxChange'}}
            />
          </BlockSlot>
        </PolicySelector>

      </BlockSlot>
    </RoleForm>

    <input id="{{this.name}}_state_policy" type="radio" name="{{this.name}}[state]" value="policy" checked={{if (eq this.state 'policy') 'checked'}} onchange={{action 'change'}} />
    <PolicyForm
      data-test-policy-form
      @name="role[policy]"
      @form={{this.policyForm}}
      @dc={{@dc}}
      @nspace={{@nspace}}
      @partition={{@partition}}
    />

  </BlockSlot>
  <BlockSlot @name="actions" as |close|>

    <Hds::ButtonSet>
      {{#if (eq this.state 'role')}}
        <Hds::Button
          type='submit'
          @text='Create and apply'
          @isLoading={{this.item.isSaving}}
          onclick={{perform this.save this.item @items (queue (action close) (action 'reset'))}}
          disabled={{if (or this.item.isSaving this.item.isPristine this.item.isInvalid) 'disabled'}}
        />
        <Hds::Button
          @text='Cancel'
          type='reset'
          @color='secondary'
          disabled={{if this.item.isSaving 'disabled'}}
          {{on 'click' (action (queue (action close) (action 'reset')))}}
        />

      {{else}}
        <Hds::Button
          type='submit'
          @text='Create and apply'
          @isLoading={{this.policy.isSaving}}
          {{action 'dispatch' 'save' (array this.policy this.item.Policies (action (mut this.state) 'role'))}}
          disabled={{if (or this.policy.isSaving this.policy.isPristine this.policy.isInvalid) 'disabled'}}
        />
        <Hds::Button
          @text='Cancel'
          type='reset'
          @color='secondary'
          disabled={{if this.policy.isSaving 'disabled'}}
          {{on 'click' (action (mut this.state) 'role')}}
        />
      {{/if}}
    </Hds::ButtonSet>
  </BlockSlot>
</ModalDialog>

<ChildSelector
  @disabled={{@disabled}}
  @repo={{this.repo}}
  @dc={{@dc}}
  @partition={{@partition}}
  @nspace={{@nspace}}
  @type="role"
  @placeholder="Search for role"
  @items={{@items}}
>
  <BlockSlot @name="label">
    Apply an existing role
  </BlockSlot>
  <BlockSlot @name="create">
    <Hds::Button
      @text='Create new role'
      @size='small'
      @color='tertiary'
      @icon='plus'
      class='type-dialog'
      data-test-role-create
      {{on "click" (optional this.modal.open)}}
    />
  </BlockSlot>
  <BlockSlot @name="option" as |option|>
    {{option.Name}}
  </BlockSlot>
  <BlockSlot @name="set" as |remove|>
    <TabularCollection
        data-test-roles
        @rows={{5}}
        @items={{sort-by 'CreateTime:desc' 'Name:asc' @items}} as |item index|
    >
      <BlockSlot @name="header">
        <th>Name</th>
        <th>Description</th>
      </BlockSlot>
      <BlockSlot @name="row">
        <td>
          <a href={{href-to 'dc.acls.roles.edit' item.ID}}>{{item.Name}}</a>
        </td>
        <td>
          {{item.Description}}
        </td>
      </BlockSlot>
      <BlockSlot @name="actions" as |index change checked|>
        <PopoverMenu @expanded={{if (eq checked index) true false}} @onchange={{action change index}} @keyboardAccess={{true}} as |components|>
          <BlockSlot @name="trigger">
            More
          </BlockSlot>
          <BlockSlot @name="menu" as |confirm keypressClick|>
              <components.MenuItem @href={{href-to 'dc.acls.roles.edit' item.ID}}>
                <BlockSlot @name="label">
                  {{#if (can "edit role" item=item)}}
                    Edit
                  {{else}}
                    View
                  {{/if}}
                </BlockSlot>
              </components.MenuItem>

            {{#if (not @disabled)}}
              <components.MenuItem class="dangerous" @onclick={{fn remove item @items}} data-test-delete>
                <BlockSlot @name="label">
                  Remove
                </BlockSlot>
                <BlockSlot @name="confirm-header">
                  Confirm Remove
                </BlockSlot>
                <BlockSlot @name="confirm-body">
                  Are you sure you want to remove this role?
                </BlockSlot>
                <BlockSlot @name="confirm-footer" as |Params|>
                  <Params.button @text='Remove' {{on 'click' Params.confirmAction}} />
                </BlockSlot>
              </components.MenuItem>
            {{/if}}
          </BlockSlot>
        </PopoverMenu>
      </BlockSlot>
    </TabularCollection>
  </BlockSlot>
</ChildSelector>
