<!--

    Copyright © 2016-2026 The Thingsboard Authors

    Licensed 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.

-->
<mat-toolbar color="primary">
  <h2>{{ dialogTitle | translate }}</h2>
  <span class="flex-1"></span>
  <div tb-help="templateNotifications"></div>
  <button mat-icon-button
          (click)="cancel()"
          type="button">
    <mat-icon class="material-icons">close</mat-icon>
  </button>
</mat-toolbar>
@if (isLoading$ | async) {
  <mat-progress-bar color="warn" mode="indeterminate"></mat-progress-bar>
} @else {
  <div style="height: 4px;"></div>
}
<div mat-dialog-content>
  <mat-horizontal-stepper linear #notificationTemplateStepper
                          labelPosition="end"
                          [orientation]="(stepperOrientation | async)"
                          (selectionChange)="changeStep($event)">
    <ng-template matStepperIcon="edit">
      <mat-icon>check</mat-icon>
    </ng-template>
    <mat-step [stepControl]="templateNotificationForm">
      <ng-template matStepLabel>{{ 'notification.setup' | translate }}</ng-template>
      <form [formGroup]="templateNotificationForm" style="padding-bottom: 16px;">
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>notification.name</mat-label>
          <input matInput formControlName="name" required>
          @if (templateNotificationForm.get('name').hasError('required')) {
            <mat-error>{{ 'notification.name-required' | translate }}</mat-error>
          }
        </mat-form-field>
        @if (!hideSelectType) {
          <mat-form-field class="mat-block flex-1" appearance="outline">
            <mat-label translate>notification.type</mat-label>
            <mat-select formControlName="notificationType">
              @for (notificationType of notificationTypes; track notificationType) {
                <mat-option [value]="notificationType">
                  {{ notificationTemplateTypeTranslateMap.get(notificationType).name | translate }}
                </mat-option>
              }
            </mat-select>
          </mat-form-field>
        }
        <section formGroupName="configuration">
          <label [class.tb-error]="templateNotificationForm.get('configuration.deliveryMethodsTemplates').hasError('atLeastOne')"
            class="tb-title tb-required">{{ "notification.delivery-methods" | translate }}</label>
          <div class="tb-hint" translate>notification.at-least-one-should-be-selected</div>
          <section formGroupName="deliveryMethodsTemplates" class="delivery-methods-container">
            @for (deliveryMethods of notificationDeliveryMethods; track deliveryMethods) {
              <section class="tb-form-panel stroked" [formGroupName]="deliveryMethods">
                <mat-slide-toggle formControlName="enabled">
                  {{ notificationDeliveryMethodInfoMap.get(deliveryMethods).name | translate }}
                </mat-slide-toggle>
              </section>
            }
          </section>
        </section>
      </form>
    </mat-step>
    <mat-step [stepControl]="notificationTemplateConfigurationForm" #composeStep=matStep>
      <ng-template matStepLabel>{{ 'notification.compose' | translate }}</ng-template>
      <ng-template matStepContent>
        <form [formGroup]="notificationTemplateConfigurationForm">
          <tb-template-configuration
            [notificationType]="templateNotificationForm.get('notificationType').value"
            [predefinedDeliveryMethodsTemplate]="deliveryConfiguration"
            [interacted]="composeStep.interacted"
            formControlName="deliveryMethodsTemplates">
          </tb-template-configuration>
        </form>
      </ng-template>
    </mat-step>
  </mat-horizontal-stepper>
</div>
<mat-divider></mat-divider>
<div mat-dialog-actions class="tb-dialog-actions">
  @if (selectedIndex > 0) {
    <button mat-stroked-button
            (click)="backStep()">{{ 'action.back' | translate }}</button>
  }
  <span class="flex-1"></span>
  <button mat-raised-button
          color="primary"
          (click)="nextStep()">{{ nextStepLabel() | translate }}</button>
</div>
