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

<app-page-header
  title="HR.DEPARTMENTS.TITLE"
  subtitle="HR.DEPARTMENTS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.DEPARTMENTS.NEW' | translate"
    icon="pi pi-plus"
    (onClick)="openNew()"
    severity="success"
  />
</app-page-header>

<p-table
  [value]="tableData()"
  [loading]="tableLoading()"
  [paginator]="true"
  [rows]="rows"
  [totalRecords]="totalRecords()"
  [lazy]="true"
  (onLazyLoad)="onPageChange($event)"
  [first]="first"
  [rowsPerPageOptions]="[10, 25, 50]"
  styleClass="p-datatable-sm p-datatable-striped"
  [tableStyle]="{ 'min-width': '50rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.DEPARTMENTS.CODE' | translate }}</th>
      <th>{{ 'HR.DEPARTMENTS.NAME_AR' | translate }}</th>
      <th>{{ 'HR.DEPARTMENTS.NAME_EN' | translate }}</th>
      <th>{{ 'HR.DEPARTMENTS.PARENT' | translate }}</th>
      <th>{{ 'COMMON.STATUS' | translate }}</th>
      <th>{{ 'COMMON.ACTIONS' | translate }}</th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-item>
    <tr>
      <td>{{ item.code }}</td>
      <td>{{ item.name_ar }}</td>
      <td>{{ item.name_en }}</td>
      <td>{{ item.parent?.name_ar || '-' }}</td>
      <td>
        <p-tag [value]="item.status" [severity]="$any(getStatusSeverity(item.status))"></p-tag>
      </td>
      <td>
        <div class="flex gap-2">
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editItem(item)"
          />
          <p-button
            icon="pi pi-trash"
            [rounded]="true"
            [text]="true"
            severity="danger"
            (onClick)="deleteItem(item)"
          />
        </div>
      </td>
    </tr>
  </ng-template>
  <ng-template pTemplate="emptymessage">
    <tr>
      <td colspan="6" class="text-center p-4">{{ 'COMMON.NO_DATA' | translate }}</td>
    </tr>
  </ng-template>
</p-table>

<p-dialog
  [header]="
    editingItem()
      ? ('HR.DEPARTMENTS.EDIT' | translate)
      : ('HR.DEPARTMENTS.NEW' | translate)
  "
  [(visible)]="dialogVisible"
  [modal]="true"
  [style]="{ width: '550px' }"
  [closable]="true"
>
  <form [formGroup]="form" (ngSubmit)="onSave()">
    <div class="flex flex-column gap-3 pt-3">
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.NAME_AR' | translate }} *</label>
        <input pInputText formControlName="name_ar" />
        @if (form.get('name_ar')?.touched && form.get('name_ar')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.PARENT' | translate }}</label>
        <p-select
          formControlName="parent_id"
          [options]="departments"
          optionLabel="name_ar"
          optionValue="id"
          [showClear]="true"
          [placeholder]="'COMMON.SELECT' | translate"
          [filter]="true"
        ></p-select>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.MANAGER' | translate }}</label>
        <p-select
          formControlName="manager_id"
          [options]="employees"
          optionLabel="full_name"
          optionValue="id"
          [showClear]="true"
          [placeholder]="'COMMON.SELECT' | translate"
          [filter]="true"
        ></p-select>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.BRANCH' | translate }}</label>
        <p-select
          formControlName="branch_id"
          [options]="branches"
          optionLabel="name_ar"
          optionValue="id"
          [showClear]="true"
          [placeholder]="'COMMON.SELECT' | translate"
          [filter]="true"
        ></p-select>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.DEPARTMENTS.DESCRIPTION' | translate }}</label>
        <textarea pTextarea formControlName="description" rows="3"></textarea>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'COMMON.STATUS' | translate }}</label>
        <p-select
          formControlName="status"
          [options]="statusOptions"
          optionLabel="label"
          optionValue="value"
        ></p-select>
      </div>
    </div>
  </form>
  <ng-template pTemplate="footer">
    <p-button
      [label]="'COMMON.CANCEL' | translate"
      icon="pi pi-times"
      [text]="true"
      (onClick)="dialogVisible.set(false)"
    />
    <p-button
      [label]="'COMMON.SAVE' | translate"
      icon="pi pi-check"
      (onClick)="onSave()"
      [loading]="saving()"
    />
  </ng-template>
</p-dialog>
