<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one or more
  ~ contributor license agreements.  See the NOTICE file distributed with
  ~ this work for additional information regarding copyright ownership.
  ~ The ASF licenses this file to You under the Apache License, Version 2.0
  ~ (the "License"); you may not use this file except in compliance with
  ~ the License.  You may obtain a copy of the License at
  ~
  ~     http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<h2 mat-dialog-title>{{ isNew ? 'Add' : 'Edit' }} {{ isUser ? 'User' : 'User Group' }}</h2>
<form class="edit-tenant-form" [formGroup]="editTenantForm">
    <context-error-banner [context]="ErrorContextKey.USERS"></context-error-banner>
    <mat-dialog-content>
        <div>
            <mat-radio-group formControlName="tenantType" (change)="tenantTypeChanged()">
                <mat-radio-button [value]="USER">Individual</mat-radio-button>
                <mat-radio-button [value]="USER_GROUP">Group</mat-radio-button>
            </mat-radio-group>
        </div>
        <div class="mt-6">
            <mat-form-field>
                <mat-label>Identity</mat-label>
                <input matInput formControlName="identity" type="text" />
                @if (identity.invalid) {
                    <mat-error>{{ getIdentityErrorMessage() }}</mat-error>
                }
            </mat-form-field>
        </div>
        @if (isUser) {
            @if (userGroups.length > 0) {
                <div class="mt-2">
                    <mat-label>Member of</mat-label>
                    <mat-selection-list formControlName="userGroups" class="border">
                        @for (userGroup of userGroups; track userGroup) {
                            <mat-list-option togglePosition="before" [value]="userGroup.id"
                                >{{ userGroup.component.identity }}
                            </mat-list-option>
                        }
                    </mat-selection-list>
                </div>
            }
        } @else {
            @if (users.length > 0) {
                <div class="mt-2">
                    <mat-label>Members</mat-label>
                    <mat-selection-list formControlName="users" class="border">
                        @for (user of users; track user) {
                            <mat-list-option togglePosition="before" [value]="user.id"
                                >{{ user.component.identity }}
                            </mat-list-option>
                        }
                    </mat-selection-list>
                </div>
            }
        }
    </mat-dialog-content>
    @if ({ value: (saving$ | async)! }; as saving) {
        <mat-dialog-actions align="end">
            <button mat-button mat-dialog-close>Cancel</button>
            <button mat-flat-button [disabled]="editTenantForm.invalid || saving.value" (click)="okClicked()">
                <span *nifiSpinner="saving.value">{{ isNew ? 'Add' : 'Apply' }}</span>
            </button>
        </mat-dialog-actions>
    }
</form>
