<!--

    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.

-->
<div class="tb-form-panel no-border no-padding">
  <div class="tb-form-table" [formGroup]="relationFiltersFormGroup">
    <div class="tb-form-table-header">
      @if (enableNotOption) {
        <div class="tb-form-table-header-cell flex-18"></div>
      }
      <div class="tb-form-table-header-cell flex-50">{{ 'relation.relation-type' | translate }}</div>
      <div class="tb-form-table-header-cell flex-50">{{ 'entity.entity-types' | translate }}</div>
      <div class="tb-form-table-header-cell actions-header"></div>
    </div>
    <div class="tb-form-table-body" formArrayName="relationFilters">
      @for (relationFilterControl of relationFiltersFormArray.controls; track relationFilterControl) {
        <div class="tb-form-table-row align-start"
             >
          @if (enableNotOption) {
            <mat-chip-listbox class="flex-18 center-stretch" [formControl]="relationFilterControl.get('negate')">
              <mat-chip-option color="primary" [value]="true">{{ 'relation.not' | translate }}</mat-chip-option>
            </mat-chip-listbox>
          }
          <tb-relation-type-autocomplete subscriptSizing="dynamic"
                                         class="flex-50" showLabel="false"
                                         [additionalClasses]="['tb-inline-field']"
                                         appearance="outline"
                                         [formControl]="relationFilterControl.get('relationType')">
          </tb-relation-type-autocomplete>
          <tb-entity-type-list class="entity-type-list flex-50" subscriptSizing="dynamic" appearance="outline"
                               [additionalClasses]="['tb-inline-field', 'tb-chips', 'flex']"
                               filledInputPlaceholder="{{ 'entity.add-entity-type' | translate }}"
                               [allowedEntityTypes]="allowedEntityTypes"
                               [formControl]="relationFilterControl.get('entityTypes')">
          </tb-entity-type-list>
          <div class="tb-form-table-row-cell-buttons">
            <button type="button"
                    mat-icon-button
                    (click)="removeFilter($index)"
                    [disabled]="isLoading$ | async"
                    matTooltip="{{ 'relation.remove-filter' | translate }}"
                    matTooltipPosition="above">
              <mat-icon>delete</mat-icon>
            </button>
          </div>
        </div>
      }
      <div [class.!hidden]="relationFiltersFormArray.length">
        <span class="tb-prompt flex items-center justify-center" translate>relation.any-relation</span>
      </div>
    </div>
  </div>
  <div>
    <button type="button" mat-stroked-button color="primary" (click)="addFilter()">
      {{ 'filter.add' | translate }}
    </button>
  </div>
</div>
