<!--

    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.

-->
<form [formGroup]="filterFormGroup" (ngSubmit)="save()" style="width: 700px;">
  <mat-toolbar color="primary" class="justify-between">
    <h2>{{ (isAdd ? 'filter.add' : 'filter.edit') | translate }}</h2>
    <button mat-icon-button
            (click)="cancel()"
            type="button">
      <mat-icon class="material-icons">close</mat-icon>
    </button>
  </mat-toolbar>
  <div mat-dialog-content class="flex flex-col">
    <div class="flex flex-row items-start gap-2">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>filter.name</mat-label>
        <input matInput formControlName="filter" required>
        @if (filterFormGroup.controls.filter.hasError('required')) {
          <mat-error>{{ 'filter.name-required' | translate }}</mat-error>
        }
        @if (filterFormGroup.controls.filter.hasError('duplicateFilterName')) {
          <mat-error>{{ 'filter.duplicate-filter' | translate }}</mat-error>
        }
      </mat-form-field>
      <button mat-stroked-button
              type="button"
              class="tb-rounded-btn mt-2"
              color="primary"
              [class.active]="filterFormGroup.get('editable').value"
              (click)="onEditableChange($event)">
        <mat-icon [class.invisible]="!filterFormGroup.get('editable').value">checkmark</mat-icon>
        <span translate>filter.editable</span>
      </button>
    </div>
    <tb-key-filter-list
        formControlName="keyFilters"
        [operation]="filterFormGroup.get('keyFiltersOperation').value">
      <tb-toggle-select keyFilterHeaderActions
                        formControlName="keyFiltersOperation"
                        [matTooltip]="!allowKeyFiltersOrConditions ? ('filter.or-conditions-disabled' | translate) : ''"
                        selectMediaBreakpoint="xs">
        <tb-toggle-option
            [value]="ComplexOperation.AND">{{ complexOperationTranslationMap.get(ComplexOperation.AND) | translate | titlecase }}
        </tb-toggle-option>
        <tb-toggle-option
            [value]="ComplexOperation.OR">{{ complexOperationTranslationMap.get(ComplexOperation.OR) | translate | titlecase }}
        </tb-toggle-option>
      </tb-toggle-select>
    </tb-key-filter-list>
  </div>
  <div mat-dialog-actions  class="flex items-center justify-end">
    <button mat-button color="primary"
            type="button"
            (click)="cancel()" cdkFocusInitial>
      {{ 'action.cancel' | translate }}
    </button>
    <button mat-raised-button color="primary"
            type="submit"
            [disabled]="filterFormGroup.invalid || !filterFormGroup.dirty">
      {{ (isAdd ? 'action.add' : 'action.update') | translate }}
    </button>
  </div>
</form>
