<!--
~ 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>Save Flow Version</h2>
<form class="save-version-form" [formGroup]="saveVersionForm">
    <context-error-banner [context]="ErrorContextKey.FLOW_VERSION"></context-error-banner>
    <mat-dialog-content>
        @if (versionControlInformation) {
            <div class="flex flex-col gap-y-4 mb-6">
                <div>
                    <div>Registry</div>
                    <div class="tertiary-color font-medium">{{ versionControlInformation.registryName }}</div>
                </div>
                @if (versionControlInformation.branch) {
                    <div>
                        <div>Branch</div>
                        <div class="tertiary-color font-medium">{{ versionControlInformation.branch }}</div>
                    </div>
                }
                <div>
                    <div>Bucket</div>
                    <div class="tertiary-color font-medium">{{ versionControlInformation.bucketName }}</div>
                </div>
                <div>
                    <div>
                        <div>Flow Name</div>
                        <div class="tertiary-color font-medium">{{ versionControlInformation.flowName }}</div>
                    </div>
                </div>
                <div>
                    <div>Flow Description</div>
                    @if (!versionControlInformation.flowDescription) {
                        <div class="unset neutral-color">No value set</div>
                    } @else {
                        <div class="tertiary-color font-medium">{{ versionControlInformation.flowDescription }}</div>
                    }
                </div>
            </div>
        } @else {
            <mat-form-field>
                <mat-label>Registry</mat-label>
                <mat-select formControlName="registry" (selectionChange)="registryChanged($event.value)">
                    @for (option of registryClientOptions; track option) {
                        @if (option.description) {
                            <mat-option
                                [value]="option.value"
                                nifiTooltip
                                [tooltipComponentType]="TextTip"
                                [tooltipInputData]="option.description"
                                [delayClose]="false"
                                >{{ option.text }}
                            </mat-option>
                        } @else {
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        }
                    }
                </mat-select>
            </mat-form-field>

            @if (supportsBranching) {
                <mat-form-field>
                    <mat-label>Branch</mat-label>
                    <mat-select formControlName="branch" (selectionChange)="branchChanged($event.value)">
                        @for (option of branchOptions; track option) {
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        }
                    </mat-select>
                    @if (saveVersionForm.controls['branch'].hasError('required')) {
                        <mat-error>No branches available</mat-error>
                    }
                </mat-form-field>
            }

            <mat-form-field>
                <mat-label>Bucket</mat-label>
                <mat-select formControlName="bucket">
                    @for (option of bucketOptions; track option) {
                        @if (option.description) {
                            <mat-option
                                [value]="option.value"
                                nifiTooltip
                                [tooltipComponentType]="TextTip"
                                [tooltipInputData]="option.description"
                                [delayClose]="false"
                                >{{ option.text }}
                            </mat-option>
                        } @else {
                            <mat-option [value]="option.value">{{ option.text }}</mat-option>
                        }
                    }
                </mat-select>
                @if (saveVersionForm.controls['bucket'].hasError('required')) {
                    <mat-error>No buckets available</mat-error>
                }
            </mat-form-field>

            <div class="w-full">
                <mat-form-field class="flex-1">
                    <mat-label>Flow Name</mat-label>
                    <input matInput formControlName="flowName" type="text" />
                </mat-form-field>
            </div>

            <mat-form-field>
                <mat-label>Flow Description</mat-label>
                <textarea matInput formControlName="flowDescription" type="text"></textarea>
            </mat-form-field>
        }

        <mat-form-field>
            <mat-label>Version Comments</mat-label>
            <textarea matInput formControlName="comments" type="text"></textarea>
        </mat-form-field>
    </mat-dialog-content>

    <mat-dialog-actions align="end">
        <button mat-button mat-dialog-close>Cancel</button>
        <button
            [disabled]="saveVersionForm.invalid || saving() || saveVersionForm.pending"
            type="button"
            (click)="submitForm()"
            mat-flat-button>
            <span *nifiSpinner="saving()">Save</span>
        </button>
    </mat-dialog-actions>
</form>
