<!--

    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, 'disableAccount')"
          [class.!hidden]="isEdit || !isUserActivated() || !isUserCredentialsEnabled()">
    {{'user.disable-account' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'enableAccount')"
          [class.!hidden]="isEdit || !isUserActivated() || isUserCredentialsEnabled()">
    {{'user.enable-account' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'displayActivationLink')"
          [class.!hidden]="isEdit || isUserActivated()">
    {{'user.display-activation-link' | translate }}
  </button>
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'resendActivation')"
          [class.!hidden]="isEdit || isUserActivated()">
    {{'user.resend-activation' | translate }}
  </button>
  @if (loginAsUserEnabled$ | async) {
    <button mat-raised-button color="primary"
            [disabled]="(isLoading$ | async)"
            (click)="onEntityAction($event, 'loginAsUser')"
            [class.!hidden]="isEdit">
      {{ (entity?.authority === authority.TENANT_ADMIN ? 'user.login-as-tenant-admin' : 'user.login-as-customer-user') | translate }}
    </button>
  }
  <button mat-raised-button color="primary"
          [disabled]="(isLoading$ | async)"
          (click)="onEntityAction($event, 'delete')"
          [class.!hidden]="hideDelete() || isEdit">
    {{'user.delete' | translate }}
  </button>
  <div class="flex flex-row xs:flex-col">
    <button mat-raised-button
            ngxClipboard
            (cbOnSuccess)="onUserIdCopied($event)"
            [cbContent]="entity?.id?.id"
            [disabled]="(isLoading$ | async)"
            [class.!hidden]="isEdit">
      <mat-icon svgIcon="mdi:clipboard-arrow-left"></mat-icon>
      <span translate>user.copyId</span>
    </button>
  </div>
</div>
<div class="mat-padding flex flex-col">
  <form [formGroup]="entityForm">
    <fieldset [disabled]="(isLoading$ | async) || !isEdit">
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>user.email</mat-label>
        <input matInput type="email" formControlName="email">
        @if (entityForm.get('email').hasError('email')) {
          <mat-error>
            {{ 'user.invalid-email-format' | translate }}
          </mat-error>
        }
        @if (entityForm.get('email').hasError('required')) {
          <mat-error>
            {{ 'user.email-required' | translate }}
          </mat-error>
        }
      </mat-form-field>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>user.first-name</mat-label>
        <input matInput formControlName="firstName">
      </mat-form-field>
      <mat-form-field class="mat-block" appearance="outline">
        <mat-label translate>user.last-name</mat-label>
        <input matInput formControlName="lastName">
      </mat-form-field>
      <tb-phone-input [required]="false"
                      label="{{ 'contact.phone' | translate }}"
                      [enableFlagsSelect]="true"
                      appearance="outline"
                      formControlName="phone">
      </tb-phone-input>
      <div formGroupName="additionalInfo" class="flex flex-col">
        <mat-form-field class="mat-block" floatLabel="always" appearance="outline">
          <mat-label translate>language.language</mat-label>
          <mat-select formControlName="lang"
                      [placeholder]="'language.auto' | translate">
            <mat-option [value]="null">{{ 'language.auto' | translate }}</mat-option>
            @for(lang of languageList; track lang) {
              <mat-option [value]="lang">{{ 'language.locales.' + lang | translate }}</mat-option>
            }
          </mat-select>
        </mat-form-field>
        <mat-form-field class="mat-block" floatLabel="always" appearance="outline">
          <mat-label translate>unit.unit-system</mat-label>
          <mat-select formControlName="unitSystem"
                      [placeholder]="'unit.unit-system-type.AUTO' | translate">
            <mat-option [value]="null">{{ 'unit.unit-system-type.AUTO' | translate }}</mat-option>
            @for(unit of UnitSystems; track unit) {
              <mat-option [value]="unit">{{ 'unit.unit-system-type.' + unit | translate }}</mat-option>
            }
          </mat-select>
        </mat-form-field>
        @if (entity?.id) {
          <section class="tb-default-dashboard flex flex-1 flex-col">
            <section class="flex flex-1 flex-col items-baseline gt-sm:flex-row">
              <tb-dashboard-autocomplete
                class="flex-1"
                label="{{ 'user.default-dashboard' | translate }}"
                formControlName="defaultDashboardId"
                appearance="outline"
                [dashboardsScope]="entity?.authority === authority.TENANT_ADMIN ? 'tenant' : 'customer'"
                [tenantId]="entity?.tenantId?.id"
                [customerId]="entity?.customerId?.id"
                [selectFirstDashboard]="false"
              ></tb-dashboard-autocomplete>
              <mat-checkbox class="flex-1" formControlName="defaultDashboardFullscreen">
                {{ 'user.always-fullscreen' | translate }}
              </mat-checkbox>
            </section>
            <section class="flex flex-1 flex-col items-baseline gt-sm:flex-row">
              <tb-dashboard-autocomplete
                class="flex-1"
                label="{{ 'dashboard.home-dashboard' | translate }}"
                formControlName="homeDashboardId"
                appearance="outline"
                [dashboardsScope]="entity?.authority === authority.TENANT_ADMIN ? 'tenant' : 'customer'"
                [tenantId]="entity?.tenantId?.id"
                [customerId]="entity?.customerId?.id"
                [selectFirstDashboard]="false"
              ></tb-dashboard-autocomplete>
              <mat-checkbox class="flex-1" formControlName="homeDashboardHideToolbar">
                {{ 'dashboard.home-dashboard-hide-toolbar' | translate }}
              </mat-checkbox>
            </section>
          </section>
        }
        <mat-form-field class="mat-block" appearance="outline">
          <mat-label translate>user.description</mat-label>
          <textarea matInput formControlName="description" cdkTextareaAutosize cdkAutosizeMinRows="1" rows="1"></textarea>
        </mat-form-field>
      </div>
    </fieldset>
  </form>
</div>
