<!--

    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-details-buttons xs:flex xs:flex-col">
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'open')"
          [class.!hidden]="isEdit || isDetailsPage">
    {{'common.open-details-page' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'makePublic')"
          [class.!hidden]="isEdit || entityViewScope !== 'tenant' || isAssignedToCustomer(entity) || entity?.customerIsPublic">
    {{'entity-view.make-public' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'assignToCustomer')"
          [class.!hidden]="isEdit || entityViewScope !== 'tenant' || isAssignedToCustomer(entity)">
    {{'entity-view.assign-to-customer' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'unassignFromCustomer')"
          [class.!hidden]="isEdit || (entityViewScope !== 'customer' && entityViewScope !== 'tenant') || !isAssignedToCustomer(entity)">
    {{ (entity?.customerIsPublic ? 'entity-view.make-private' : 'entity-view.unassign-from-customer') | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'unassignFromEdge')"
          [class.!hidden]="isEdit || entityViewScope !== 'edge'">
    {{ 'edge.unassign-from-edge' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [class.!hidden]="hideDelete() || isEdit">
    {{'entity-view.delete' | translate }}
  </button>
  <div class="flex flex-row xs:flex-col">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onEntityViewIdCopied($event)"
            [cbContent]="entity?.id?.id"
            [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>entity-view.copyId</span>
    </button>
  </div>
</div>
<div class="mat-padding flex flex-col">
  <mat-form-field class="mat-block" appearance="outline"
                  [class.!hidden]="isEdit || !isAssignedToCustomer(entity)
                              || entity?.customerIsPublic || entityViewScope !== 'tenant'">
    <mat-label translate>entity-view.assignedToCustomer</mat-label>
    <input matInput disabled [ngModel]="entity?.customerTitle">
  </mat-form-field>
  <div class="tb-small" style="padding-bottom: 10px; padding-left: 2px;"
       [class.!hidden]="isEdit || !entity?.customerIsPublic || (entityViewScope !== 'customer' && entityViewScope !== 'tenant')">
    {{ 'entity-view.entity-view-public' | translate }}
  </div>
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" style="min-width: 0;">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>entity-view.name</mat-label>
        <input matInput formControlName="name" required>
        @if (entityForm.get('name').hasError('required')) {
          <mat-error>{{ 'entity-view.name-required' | translate }}</mat-error>
        }
        @if (entityForm.get('name').hasError('maxlength')) {
          <mat-error>{{ 'entity-view.name-max-length' | translate }}</mat-error>
        }
      </mat-form-field>
      <tb-entity-subtype-autocomplete
        formControlName="type"
        appearance="outline"
        [required]="true"
        [entityType]="entityType.ENTITY_VIEW">
      </tb-entity-subtype-autocomplete>
      <section class="flex flex-col">
        <label translate class="tb-title">entity-view.target-entity</label>
        <tb-entity-select class="flex-1"
                          [required]="true"
                          [allowedEntityTypes]="allowedEntityTypes"
                          formControlName="entityId"
                          appearance="outline">
        </tb-entity-select>
      </section>
      <div class="mat-accordion-container" formGroupName="keys">
        <mat-accordion [multi]="true">
          <mat-expansion-panel formGroupName="attributes" [expanded]="true">
            <mat-expansion-panel-header>
              <mat-panel-title>
                <div class="tb-panel-title" translate>entity-view.attributes-propagation</div>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <div translate class="tb-hint">entity-view.attributes-propagation-hint</div>
            <tb-entity-keys-list
              [entityId]="selectedEntityId | async"
              formControlName="cs"
              label="entity-view.client-attributes"
              [dataKeyType]="dataKeyType.attribute"
              appearance="outline">
            </tb-entity-keys-list>
            <tb-entity-keys-list
              [entityId]="selectedEntityId | async"
              formControlName="sh"
              label="entity-view.shared-attributes"
              [dataKeyType]="dataKeyType.attribute"
              appearance="outline">
            </tb-entity-keys-list>
            <tb-entity-keys-list
              [entityId]="selectedEntityId | async"
              formControlName="ss"
              label="entity-view.server-attributes"
              [dataKeyType]="dataKeyType.attribute"
              appearance="outline">
            </tb-entity-keys-list>
          </mat-expansion-panel>
          <mat-expansion-panel [expanded]="true">
            <mat-expansion-panel-header>
              <mat-panel-title>
                <div class="tb-panel-title" translate>entity-view.timeseries-data</div>
              </mat-panel-title>
            </mat-expansion-panel-header>
            <div translate class="tb-hint">entity-view.timeseries-data-hint</div>
            <tb-entity-keys-list
              [entityId]="selectedEntityId | async"
              formControlName="timeseries"
              label="entity-view.timeseries"
              [dataKeyType]="dataKeyType.timeseries"
              appearance="outline">
            </tb-entity-keys-list>
          </mat-expansion-panel>
        </mat-accordion>
      </div>
      <tb-datetime class="block"
                   dateText="{{ 'entity-view.start-ts' | translate }}"
                   [maxDate]="maxStartTimeMs | async"
                   formControlName="startTimeMs"
                   appearance="outline"
      ></tb-datetime>
      <tb-datetime class="block"
                   dateText="{{ 'entity-view.end-ts' | translate }}"
                   [minDate]="minEndTimeMs | async"
                   formControlName="endTimeMs"
                   appearance="outline"
      ></tb-datetime>
      <div formGroupName="additionalInfo">
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>entity-view.description</mat-label>
          <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
