<!--

    Copyright © 2016-2023 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">
  <div class="tb-form-panel-title">{{ panelTitle }}</div>
  <div class="tb-form-table">
    <div class="tb-form-table-header no-padding-right">
      <div *ngIf="hasAdditionalLatestDataKeys" class="tb-form-table-header-cell tb-source-header" translate>datakey.source</div>
      <div class="tb-form-table-header-cell tb-key-header" translate>datakey.key</div>
      <div class="tb-form-table-header-cell tb-label-header" translate>datakey.label</div>
      <div *ngIf="!hideDataKeyColor" class="tb-form-table-header-cell tb-color-header" translate>datakey.color</div>
      <div *ngIf="!hideUnits" class="tb-form-table-header-cell tb-units-header" translate>widget-config.units-short</div>
      <div *ngIf="!hideDecimals" class="tb-form-table-header-cell tb-decimals-header" translate>widget-config.decimals-short</div>
      <div class="tb-form-table-header-cell tb-actions-header"></div>
    </div>
    <div *ngIf="keysFormArray().controls.length; else noKeys" class="tb-form-table-body tb-drop-list"
         cdkDropList cdkDropListOrientation="vertical"
         [cdkDropListDisabled]="!dragEnabled"
         (cdkDropListDropped)="keyDrop($event)">
      <div cdkDrag [cdkDragDisabled]="!dragEnabled"
           class="tb-draggable-form-table-row"
           *ngFor="let keyControl of keysFormArray().controls; trackBy: trackByKey; let $index = index;">
        <tb-data-key-row fxFlex
                         [formControl]="keyControl"
                         required
                         [datasourceType]="datasourceType"
                         [deviceId]="deviceId"
                         [entityAliasId]="entityAliasId"
                         [hasAdditionalLatestDataKeys]="hasAdditionalLatestDataKeys"
                         [hideDataKeyColor]="hideDataKeyColor"
                         [hideDecimals]="hideDecimals"
                         [hideUnits]="hideUnits"
                         [hideDataKeyDecimals]="hideDataKeyDecimals"
                         [hideDataKeyUnits]="hideDataKeyUnits"
                         [dataKeyType]="dataKeyType"
                         [singleRow]="false"
                         [keySettingsTitle]="keySettingsTitle"
                         [removeKeyTitle]="removeKeyTitle"
                         (keyRemoved)="removeKey($index)">
        </tb-data-key-row>
        <div class="tb-form-table-row-cell-buttons">
          <button fxHide.lt-lg
                  mat-icon-button
                  type="button"
                  cdkDragHandle
                  [ngClass]="{'tb-hidden': !dragEnabled}"
                  matTooltip="{{ 'action.drag' | translate }}"
                  matTooltipPosition="above">
            <mat-icon>drag_indicator</mat-icon>
          </button>
        </div>
      </div>
    </div>
    <tb-error *ngIf="errorText" noMargin [error]="errorText" style="padding-left: 12px;"></tb-error>
  </div>
  <div>
    <button type="button" mat-stroked-button color="primary" (click)="addKey()">
      {{ addKeyTitle }}
    </button>
  </div>
</div>
<ng-template #noKeys>
  <span fxLayoutAlign="center center"
        class="tb-prompt">{{ noKeysText }}</span>
</ng-template>
