<!--

    Copyright © 2016-2022 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.

-->
<section fxLayout="column" [formGroup]="keyFilterListFormGroup">
  <mat-accordion [multi]="true">
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <div translate>filter.key-filters</div>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <div fxLayout="row">
        <span fxFlex="8"></span>
        <div fxLayout="row" fxLayoutAlign="start center" fxFlex="92">
          <label fxFlex translate class="tb-title no-padding">filter.key-name</label>
          <label fxFlex translate class="tb-title no-padding">filter.key-type.key-type</label>
          <span [fxShow]="!disabled" style="min-width: 80px;">&nbsp;</span>
          <span [fxShow]="disabled" style="min-width: 40px;">&nbsp;</span>
        </div>
      </div>
      <mat-divider></mat-divider>
      <div class="key-filter-list">
        <div fxLayout="row" fxLayoutAlign="start center" style="max-height: 40px;"
             formArrayName="keyFilters"
             *ngFor="let keyFilterControl of keyFiltersFormArray().controls; let $index = index">
          <div fxFlex="8" class="filters-operation">
            <span *ngIf="$index > 0" translate>filter.operation.and</span>
          </div>
          <div fxLayout="column" fxFlex="92">
            <div fxLayout="row" fxLayoutAlign="start center">
              <div fxFlex>{{ keyFilterControl.value.key.key }}</div>
              <div fxFlex>{{ entityKeyTypeTranslations.get(keyFilterControl.value.key.type) | translate }}</div>
              <button mat-icon-button color="primary"
                      type="button"
                      (click)="editKeyFilter($index)"
                      matTooltip="{{ (disabled ? 'filter.key-filter' : 'filter.edit-key-filter') | translate }}"
                      matTooltipPosition="above">
                <mat-icon>{{disabled ? 'more_vert' : 'edit'}}</mat-icon>
              </button>
              <button mat-icon-button color="primary"
                      [fxShow]="!disabled"
                      type="button"
                      (click)="removeKeyFilter($index)"
                      matTooltip="{{ 'filter.remove-key-filter' | translate }}"
                      matTooltipPosition="above">
                <mat-icon>close</mat-icon>
              </button>
            </div>
            <mat-divider></mat-divider>
          </div>
        </div>
        <span [fxShow]="!keyFiltersFormArray().length"
              fxLayoutAlign="center center" [ngClass]="{'disabled': disabled}"
              class="no-data-found" translate>filter.no-key-filters</span>
      </div>
      <div style="margin-top: 16px;">
        <button mat-button mat-raised-button color="primary"
                [fxShow]="!disabled"
                (click)="addKeyFilter()"
                type="button"
                matTooltip="{{ 'filter.add-key-filter' | translate }}"
                matTooltipPosition="above">
          {{ 'filter.add-key-filter' | translate }}
        </button>
      </div>
    </mat-expansion-panel>
    <mat-expansion-panel [expanded]="true">
      <mat-expansion-panel-header>
        <mat-panel-title>
          <div translate>filter.preview</div>
        </mat-panel-title>
      </mat-expansion-panel-header>
      <div class="tb-filter-preview">
        <tb-filter-text [formControl]="keyFiltersControl"></tb-filter-text>
      </div>
    </mat-expansion-panel>
  </mat-accordion>
</section>
