<!--

    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.

-->
@if (!standalone) {
  <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, 'export')"
            [class.!hidden]="isEdit">
      {{'device-profile.export' | translate }}
    </button>
    <button mat-raised-button color="primary"
            [disabled]="(isLoading$ | async)"
            (click)="onEntityAction($event, 'setDefault')"
            [class.!hidden]="isEdit || entity?.default">
      {{'device-profile.set-default' | translate }}
    </button>
    <button mat-raised-button color="primary"
            [disabled]="(isLoading$ | async)"
            (click)="onEntityAction($event, 'delete')"
            [class.!hidden]="hideDelete() || isEdit">
      {{'device-profile.delete' | translate }}
    </button>
    <div class="flex flex-row xs:flex-col">
      <button mat-raised-button
              ngxClipboard
              (cbOnSuccess)="onDeviceProfileIdCopied($event)"
              [cbContent]="entity?.id?.id"
              [class.!hidden]="isEdit">
        <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
        <span translate>device-profile.copyId</span>
      </button>
    </div>
  </div>
}
<div [class.mat-padding]="!standalone" class="flex flex-col">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit" style="min-width: 0;" class="flex flex-col gap-2">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>device-profile.name</mat-label>
        <input matInput formControlName="name" required/>
        @if (entityForm.get('name').hasError('required')) {
          <mat-error>{{ 'device-profile.name-required' | translate }}</mat-error>
        }
        @if (entityForm.get('name').hasError('maxlength')) {
          <mat-error>{{ 'device-profile.name-max-length' | translate }}</mat-error>
        }
      </mat-form-field>
      <tb-rule-chain-autocomplete
        labelText="device-profile.default-rule-chain"
        appearance="outline"
        formControlName="defaultRuleChainId">
      </tb-rule-chain-autocomplete>
      <tb-dashboard-autocomplete
        label="{{'device-profile.mobile-dashboard' | translate}}"
        appearance="outline"
        formControlName="defaultDashboardId">
        @if (!entityForm.get('defaultDashboardId').disabled) {
          <span tb-hint>{{'device-profile.mobile-dashboard-hint' | translate}}</span>
        }
      </tb-dashboard-autocomplete>
      <tb-queue-autocomplete
        [queueType]="serviceType"
        appearance="outline"
        formControlName="defaultQueueName">
      </tb-queue-autocomplete>
      <tb-rule-chain-autocomplete
        labelText="device-profile.default-edge-rule-chain"
        appearance="outline"
        formControlName="defaultEdgeRuleChainId"
        [ruleChainType]="edgeRuleChainType">
        @if (!entityForm.get('defaultEdgeRuleChainId').disabled) {
          <span tb-hint>{{'device-profile.default-edge-rule-chain-hint' | translate}}</span>
        }
      </tb-rule-chain-autocomplete>
      <tb-ota-package-autocomplete
        [useFullEntityId]="true"
        [showDetailsPageLink]="true"
        [type]="otaUpdateType.FIRMWARE"
        [deviceProfileId]="deviceProfileId?.id"
        appearance="outline"
        formControlName="firmwareId">
      </tb-ota-package-autocomplete>
      <tb-ota-package-autocomplete
        [useFullEntityId]="true"
        [showDetailsPageLink]="true"
        [type]="otaUpdateType.SOFTWARE"
        [deviceProfileId]="deviceProfileId?.id"
        appearance="outline"
        formControlName="softwareId">
      </tb-ota-package-autocomplete>
      <mat-form-field class="mat-block !hidden" appearance="outline">
        <mat-label translate>device-profile.type</mat-label>
        <mat-select formControlName="type" required>
          @for (type of deviceProfileTypes; track type) {
            <mat-option [value]="type">
              {{ deviceProfileTypeTranslations.get(type) | translate }}
            </mat-option>
          }
        </mat-select>
        @if (entityForm.get('type').hasError('required')) {
          <mat-error>{{ 'device-profile.type-required' | translate }}</mat-error>
        }
      </mat-form-field>
      <tb-gallery-image-input class="flex-1"
                      label="{{'device-profile.image' | translate}}"
                      formControlName="image">
      </tb-gallery-image-input>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>device-profile.description</mat-label>
        <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
      </mat-form-field>
      @if (standalone) {
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>device-profile.transport-type</mat-label>
          <mat-select formControlName="transportType" required>
            @for (type of deviceTransportTypes; track type) {
              <mat-option [value]="type">
                {{ deviceTransportTypeTranslations.get(type) | translate }}
              </mat-option>
            }
          </mat-select>
          @if (entityForm.get('transportType').hasError('required')) {
            <mat-error>{{ 'device-profile.transport-type-required' | translate }}</mat-error>
          }
        </mat-form-field>
        <div formGroupName="profileData" style="padding-bottom: 16px;">
          <mat-accordion multi="true">
            @if (displayProfileConfiguration) {
              <mat-expansion-panel [expanded]="true">
                <mat-expansion-panel-header>
                  <mat-panel-title>
                    <div translate>device-profile.profile-configuration</div>
                  </mat-panel-title>
                </mat-expansion-panel-header>
                <ng-template matExpansionPanelContent>
                  <tb-device-profile-configuration
                    formControlName="configuration">
                  </tb-device-profile-configuration>
                </ng-template>
              </mat-expansion-panel>
            }
            @if (displayTransportConfiguration) {
              <mat-expansion-panel [expanded]="true">
                <mat-expansion-panel-header>
                  <mat-panel-title>
                    <div translate>device-profile.transport-configuration</div>
                  </mat-panel-title>
                </mat-expansion-panel-header>
                <ng-template matExpansionPanelContent>
                  <tb-device-profile-transport-configuration
                    formControlName="transportConfiguration"
                    [isAdd]="isTransportTypeChanged">
                  </tb-device-profile-transport-configuration>
                </ng-template>
              </mat-expansion-panel>
            }
            @if (entityForm.get('profileData.alarms').value?.length) {
              <mat-expansion-panel [expanded]="false">
                <mat-expansion-panel-header>
                  <mat-panel-title>
                    <div>{{'device-profile.alarm-rules-with-count' | translate:
                      {count: entityForm.get('profileData.alarms').value ?
                          entityForm.get('profileData.alarms').value.length : 0} }}</div>
                  </mat-panel-title>
                </mat-expansion-panel-header>
                <ng-template matExpansionPanelContent>
                  <tb-device-profile-alarms
                    formControlName="alarms"
                    [deviceProfileId]="deviceProfileId">
                  </tb-device-profile-alarms>
                </ng-template>
              </mat-expansion-panel>
            }
            <mat-expansion-panel [expanded]="true">
              <mat-expansion-panel-header>
                <mat-panel-title>
                  <div translate>device-profile.device-provisioning</div>
                </mat-panel-title>
              </mat-expansion-panel-header>
              <ng-template matExpansionPanelContent>
                <tb-device-profile-provision-configuration
                  formControlName="provisionConfiguration">
                </tb-device-profile-provision-configuration>
              </ng-template>
            </mat-expansion-panel>
          </mat-accordion>
        </div>
      }
    </fieldset>
  </form>
</div>
