<p-toast />
<p-confirmDialog />

<div class="unit-groups-page">
  <app-page-header title="UNIT_GROUPS.TITLE" subtitle="UNIT_GROUPS.SUBTITLE" (refresh)="retry()">
    <p-button
      [label]="'UNIT_GROUPS.NEW_UNIT_GROUP' | translate"
      icon="pi pi-plus"
      (onClick)="openNew()"
    />
  </app-page-header>

  <ng-template #bodyTpl let-unitGroup>
    <tr>
      <td>{{ unitGroup.name_ar }}</td>
      <td>{{ unitGroup.name_en }}</td>
      <td>
        <p-tag
          [value]="unitGroup.is_active ? ('COMMON.ACTIVE' | translate) : ('COMMON.INACTIVE' | translate)"
          [severity]="unitGroup.is_active ? 'success' : 'danger'"
        />
      </td>
      <td>
        <div class="action-buttons">
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editUnitGroup(unitGroup)"
          />
          <p-button
            icon="pi pi-trash"
            [rounded]="true"
            [text]="true"
            severity="danger"
            (onClick)="deleteUnitGroup(unitGroup)"
          />
        </div>
      </td>
    </tr>
  </ng-template>
  <app-data-table [data]="(unitGroups$ | async) || []" [loading]="!!(loading$ | async)" [error]="(error$ | async) ?? null" (retry)="retry()" [columns]="columns" [bodyTpl]="bodyTpl">
  </app-data-table>
</div>

<!-- Create/Edit Dialog -->
<app-form-dialog
  [(visible)]="dialogVisible"
  [header]="editingUnitGroup() ? ('UNIT_GROUPS.EDIT_UNIT_GROUP' | translate) : ('UNIT_GROUPS.NEW_UNIT_GROUP' | translate)"
  [saving]="!!(saving$ | async)"
  (save)="onSave()"
>
  <form [formGroup]="unitGroupForm" class="dialog-form">
    <div class="form-field">
      <label>{{ 'UNIT_GROUPS.NAME_AR' | translate }} *</label>
      <input pInputText formControlName="name_ar" />
      @if (unitGroupForm.get('name_ar')?.touched && unitGroupForm.get('name_ar')?.hasError('required')) {
        <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
      }
    </div>
    <div class="form-field">
      <label>{{ 'UNIT_GROUPS.NAME_EN' | translate }}</label>
      <input pInputText formControlName="name_en" />
    </div>
    <div class="form-field switch-field">
      <label>{{ 'COMMON.ACTIVE' | translate }}</label>
      <p-toggleSwitch formControlName="is_active" />
    </div>
  </form>
</app-form-dialog>
