<!--

    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 || edgeScope !== 'tenant' || isAssignedToCustomer(entity) || entity?.customerIsPublic">
    {{'edge.make-public' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'assignToCustomer')"
          [class.!hidden]="isEdit || edgeScope !== 'tenant' || isAssignedToCustomer(entity)">
    {{'edge.assign-to-customer' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'unassignFromCustomer')"
          [class.!hidden]="isEdit || (edgeScope !== 'customer' && edgeScope !== 'tenant') || !isAssignedToCustomer(entity)">
    {{ (entity?.customerIsPublic ? 'edge.make-private' : 'edge.unassign-from-customer') | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'openEdgeAssets')"
          [class.!hidden]="isEdit || edgeScope === 'customer'">
    {{'edge.manage-assets' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'openEdgeDevices')"
          [class.!hidden]="isEdit || edgeScope === 'customer'">
    {{'edge.manage-devices' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'openEdgeEntityViews')"
          [class.!hidden]="isEdit || edgeScope === 'customer'">
    {{'edge.manage-entity-views' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'openEdgeDashboards')"
          [class.!hidden]="isEdit || edgeScope === 'customer'">
    {{'edge.manage-dashboards' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'openEdgeRuleChains')"
          [class.!hidden]="isEdit || edgeScope !== 'tenant'">
    {{'edge.manage-rulechains' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [class.!hidden]="hideDelete() || isEdit">
    {{'edge.delete' | translate }}
  </button>
  <div class="flex flex-row flex-wrap xs:flex-col">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onEdgeIdCopied($event)"
            [cbContent]="entity?.id?.id"
            [disabled]="(isLoading$ | async)"
            [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>edge.copy-id</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onEdgeInfoCopied('key')"
            [cbContent]="entity?.routingKey"
            [disabled]="(isLoading$ | async)"
            [class.!hidden]="isEdit || edgeScope === 'customer_user'">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>edge.copy-edge-key</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onEdgeInfoCopied('secret')"
            [cbContent]="entity?.secret"
            [disabled]="(isLoading$ | async)"
            [class.!hidden]="isEdit || edgeScope === 'customer_user'">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>edge.copy-edge-secret</span>
    </button>
    <button mat-raised-button
            ngxClipboard
            [disabled]="(isLoading$ | async)"
            (click)="onEntityAction($event, 'syncEdge')"
            [class.!hidden]="isEdit || edgeScope === 'customer_user'">
      <mat-icon svgIcon="mdi:sync"></mat-icon>
      <span translate>edge.sync</span>
    </button>
  </div>
  <div class="flex flex-row xs:flex-col">
    @if (!upgradeAvailable) {
      <button mat-raised-button color="primary"
              [disabled]="(isLoading$ | async)"
              (click)="onEntityAction($event, 'openInstallInstructions')"
              [class.!hidden]="isEdit || edgeScope === 'customer_user'">
        <mat-icon>info_outline</mat-icon>
        <span>{{ 'edge.install-connect-instructions' | translate }}</span>
      </button>
    } @else {
      <button mat-raised-button color="primary"
              [disabled]="(isLoading$ | async)"
              (click)="onEntityAction($event, 'openUpgradeInstructions')"
              [class.!hidden]="isEdit || edgeScope === 'customer_user'">
        <mat-icon>info_outline</mat-icon>
        <span>{{ 'edge.upgrade-instructions' | translate }}</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 || edgeScope !== 'tenant'">
    <mat-label translate>edge.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 || edgeScope !== 'tenant'">
    {{ 'edge.edge-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>edge.name</mat-label>
        <input matInput formControlName="name" required>
        @if (entityForm.get('name').hasError('required')) {
          <mat-error>{{ 'edge.name-required' | translate }}</mat-error>
        }
        @if (entityForm.get('name').hasError('maxlength')) {
          <mat-error>{{ 'edge.name-max-length' | translate }}</mat-error>
        }
      </mat-form-field>
      <tb-entity-subtype-autocomplete
        formControlName="type"
        appearance="outline"
        [required]="true"
        [entityType]="entityType.EDGE">
      </tb-entity-subtype-autocomplete>
    </fieldset>
    <div class="flex flex-row" [class.!hidden]="edgeScope === 'customer_user'">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>edge.edge-key</mat-label>
        <input matInput formControlName="routingKey">
      </mat-form-field>
      <button mat-icon-button type="button" ngxClipboard
              (cbOnSuccess)="onEdgeInfoCopied('key')"
              [cbContent]="entityForm?.get('routingKey').value"
              matTooltip="{{'edge.copy-edge-key' | translate}}">
        <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      </button>
    </div>
    <div class="flex flex-row" [class.!hidden]="edgeScope === 'customer_user'">
      <mat-form-field class="mat-block flex-1" appearance="outline">
        <mat-label translate>edge.edge-secret</mat-label>
        <input matInput formControlName="secret">
      </mat-form-field>
      <button mat-icon-button type="button" ngxClipboard
              (cbOnSuccess)="onEdgeInfoCopied('secret')"
              [cbContent]="entityForm?.get('secret').value"
              matTooltip="{{'edge.copy-edge-secret' | translate}}">
        <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      </button>
    </div>
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" style="min-width: 0;">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>edge.label</mat-label>
        <input matInput formControlName="label">
        @if (entityForm.get('label').hasError('maxlength')) {
          <mat-error>{{ 'edge.label-max-length' | translate }}</mat-error>
        }
      </mat-form-field>
      <div formGroupName="additionalInfo" class="flex flex-col">
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>edge.description</mat-label>
          <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
