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

<app-page-header
  title="HR.LEAVE_TYPES.TITLE"
  subtitle="HR.LEAVE_TYPES.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.LEAVE_TYPES.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.LEAVE_TYPES.CODE' | translate }}</th>
      <th>{{ 'HR.LEAVE_TYPES.NAME_AR' | translate }}</th>
      <th>{{ 'HR.LEAVE_TYPES.NAME_EN' | translate }}</th>
      <th>{{ 'HR.LEAVE_TYPES.DAYS_ALLOWED' | translate }}</th>
      <th>{{ 'HR.LEAVE_TYPES.IS_PAID' | translate }}</th>
      <th>{{ 'HR.LEAVE_TYPES.REQUIRES_APPROVAL' | 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.days_allowed }}</td>
      <td>
        <p-tag
          [value]="item.is_paid ? ('COMMON.YES' | translate) : ('COMMON.NO' | translate)"
          [severity]="$any(item.is_paid ? 'success' : 'danger')"
        ></p-tag>
      </td>
      <td>
        <p-tag
          [value]="item.requires_approval ? ('COMMON.YES' | translate) : ('COMMON.NO' | translate)"
          [severity]="$any(item.requires_approval ? 'info' : 'secondary')"
        ></p-tag>
      </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="8" class="text-center p-4">{{ 'COMMON.NO_DATA' | translate }}</td>
    </tr>
  </ng-template>
</p-table>

<!-- Create/Edit Dialog -->
<p-dialog
  [header]="
    editingItem()
      ? ('HR.LEAVE_TYPES.EDIT' | translate)
      : ('HR.LEAVE_TYPES.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.LEAVE_TYPES.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.LEAVE_TYPES.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LEAVE_TYPES.DAYS_ALLOWED' | translate }} *</label>
          <p-inputNumber formControlName="days_allowed" [min]="0" [showButtons]="true"></p-inputNumber>
          @if (form.get('days_allowed')?.touched && form.get('days_allowed')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LEAVE_TYPES.GENDER' | translate }}</label>
          <p-select
            formControlName="gender"
            [options]="genderOptions"
            optionLabel="label"
            optionValue="value"
            [showClear]="true"
            [placeholder]="'COMMON.SELECT' | translate"
          ></p-select>
        </div>
      </div>
      <div class="flex gap-4 flex-wrap">
        <div class="flex align-items-center gap-2">
          <p-toggleSwitch formControlName="is_paid"></p-toggleSwitch>
          <label>{{ 'HR.LEAVE_TYPES.IS_PAID' | translate }}</label>
        </div>
        <div class="flex align-items-center gap-2">
          <p-toggleSwitch formControlName="requires_approval"></p-toggleSwitch>
          <label>{{ 'HR.LEAVE_TYPES.REQUIRES_APPROVAL' | translate }}</label>
        </div>
        <div class="flex align-items-center gap-2">
          <p-toggleSwitch formControlName="carry_forward"></p-toggleSwitch>
          <label>{{ 'HR.LEAVE_TYPES.CARRY_FORWARD' | translate }}</label>
        </div>
      </div>
      @if (showCarryForwardDays) {
        <div class="flex flex-column gap-1">
          <label>{{ 'HR.LEAVE_TYPES.MAX_CARRY_FORWARD_DAYS' | translate }}</label>
          <p-inputNumber formControlName="max_carry_forward_days" [min]="0" [showButtons]="true"></p-inputNumber>
        </div>
      }
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.LEAVE_TYPES.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>
