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

<app-page-header
  title="HR.POSITIONS.TITLE"
  subtitle="HR.POSITIONS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.POSITIONS.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': '60rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.POSITIONS.CODE' | translate }}</th>
      <th>{{ 'HR.POSITIONS.NAME_AR' | translate }}</th>
      <th>{{ 'HR.POSITIONS.NAME_EN' | translate }}</th>
      <th>{{ 'HR.POSITIONS.DEPARTMENT' | translate }}</th>
      <th>{{ 'HR.POSITIONS.GRADE' | translate }}</th>
      <th>{{ 'HR.POSITIONS.MIN_SALARY' | translate }}</th>
      <th>{{ 'HR.POSITIONS.MAX_SALARY' | 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.department?.name_ar || '-' }}</td>
      <td>{{ item.grade || '-' }}</td>
      <td>{{ item.min_salary != null ? (item.min_salary | number : '1.0-2') : '-' }}</td>
      <td>{{ item.max_salary != null ? (item.max_salary | number : '1.0-2') : '-' }}</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="9" class="text-center p-4">{{ 'COMMON.NO_DATA' | translate }}</td>
    </tr>
  </ng-template>
</p-table>

<p-dialog
  [header]="
    editingItem()
      ? ('HR.POSITIONS.EDIT' | translate)
      : ('HR.POSITIONS.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.POSITIONS.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.POSITIONS.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.POSITIONS.DEPARTMENT' | translate }}</label>
        <p-select
          formControlName="department_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.POSITIONS.GRADE' | translate }}</label>
        <input pInputText formControlName="grade" />
      </div>
      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.POSITIONS.MIN_SALARY' | translate }}</label>
            <p-inputNumber
              formControlName="min_salary"
              [minFractionDigits]="0"
              [maxFractionDigits]="2"
              mode="decimal"
            ></p-inputNumber>
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.POSITIONS.MAX_SALARY' | translate }}</label>
            <p-inputNumber
              formControlName="max_salary"
              [minFractionDigits]="0"
              [maxFractionDigits]="2"
              mode="decimal"
            ></p-inputNumber>
          </div>
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.POSITIONS.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>
