<!--
  ~ 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>Create Process Group</h2>
<form class="create-process-group-form" [formGroup]="createProcessGroupForm">
    <context-error-banner [context]="ErrorContextKey.PROCESS_GROUP"></context-error-banner>
    <mat-dialog-content>
        <mat-form-field>
            <mat-label>Name</mat-label>
            <input matInput formControlName="newProcessGroupName" type="text" />
            <button
                matSuffix
                type="button"
                class="upload-flow-definition tertiary-color icon icon-template-import mr-2.5 pointer"
                title="Browse"
                (click)="flowUploadControl.click()">
                <input type="file" #flowUploadControl class="hidden" (change)="attachFlow($event)" />
            </button>
        </mat-form-field>
        @if (!flowDefinition) {
            <div class="flex flew-row w-full justify-start items-start">
                <mat-form-field>
                    <mat-label>Parameter Context</mat-label>
                    <mat-select formControlName="newProcessGroupParameterContext">
                        <mat-option
                            [value]="null"
                            nifiTooltip
                            [tooltipComponentType]="TextTip"
                            [tooltipInputData]="'No parameter context'"
                            [delayClose]="false"
                            >No parameter context
                        </mat-option>
                        @for (option of parameterContextsOptions | sortObjectByProperty: 'text'; track option.value) {
                            @if (option.description) {
                                <mat-option
                                    [value]="option.value"
                                    nifiTooltip
                                    [disabled]="option.disabled"
                                    [tooltipComponentType]="TextTip"
                                    [tooltipInputData]="option.description"
                                    [delayClose]="false"
                                    >{{ option.text }}</mat-option
                                >
                            } @else {
                                <mat-option [value]="option.value" [disabled]="option.disabled">{{
                                    option.text
                                }}</mat-option>
                            }
                        }
                    </mat-select>
                </mat-form-field>
                @if (currentUser$ | async; as currentUser) {
                    @if (currentUser.parameterContextPermissions.canWrite) {
                        <button
                            mat-icon-button
                            type="button"
                            class="primary-icon-button mt-1 ml-1"
                            (click)="openNewParameterContextDialog()"
                            title="Create parameter context">
                            <i class="fa fa-plus"></i>
                        </button>
                    }
                }
            </div>
        }
        @if (flowDefinition) {
            <div>
                <div class="flex flex-col gap-y-3 mb-3">
                    <div class="flex flex-col">
                        <div>Parameter Context</div>
                        <div class="unset neutral-color">
                            Parameters will be imported from the uploaded Flow Definition.
                        </div>
                    </div>
                    <div class="flex flex-col">
                        <div>File to upload</div>
                        <div class="flex justify-between items-center">
                            <div class="tertiary-color font-medium">{{ flowNameAttached }}</div>
                            <div (click)="removeAttachedFlow()">X</div>
                        </div>
                    </div>
                </div>
            </div>
        }
    </mat-dialog-content>
    @if ({ value: (saving$ | async)! }; as saving) {
        <mat-dialog-actions align="end">
            <button mat-button mat-dialog-close>Cancel</button>
            <button
                [disabled]="
                    !createProcessGroupForm.dirty ||
                    createProcessGroupForm.invalid ||
                    saving.value ||
                    createProcessGroupForm.pending
                "
                type="submit"
                (click)="createProcessGroup()"
                mat-flat-button>
                <span *nifiSpinner="saving.value">Add</span>
            </button>
        </mat-dialog-actions>
    }
</form>
