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

<app-page-header
  title="HR.LEAVE_REQUESTS.TITLE"
  subtitle="HR.LEAVE_REQUESTS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.LEAVE_REQUESTS.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': '55rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.LEAVE_REQUESTS.EMPLOYEE' | translate }}</th>
      <th>{{ 'HR.LEAVE_REQUESTS.LEAVE_TYPE' | translate }}</th>
      <th>{{ 'HR.LEAVE_REQUESTS.START_DATE' | translate }}</th>
      <th>{{ 'HR.LEAVE_REQUESTS.END_DATE' | translate }}</th>
      <th>{{ 'HR.LEAVE_REQUESTS.DAYS' | translate }}</th>
      <th>{{ 'COMMON.STATUS' | translate }}</th>
      <th>{{ 'COMMON.ACTIONS' | translate }}</th>
    </tr>
  </ng-template>
  <ng-template pTemplate="body" let-item>
    <tr>
      <td>{{ item.employee?.full_name || '-' }}</td>
      <td>{{ item.leave_type?.name_ar || '-' }}</td>
      <td>{{ item.start_date }}</td>
      <td>{{ item.end_date }}</td>
      <td>{{ item.days }}</td>
      <td>
        <p-tag
          [value]="'HR.LEAVE_REQUESTS.STATUS_' + (item.status || 'pending').toUpperCase() | translate"
          [severity]="$any(getStatusSeverity(item.status))"
        ></p-tag>
      </td>
      <td>
        <div class="flex gap-2">
          @if (item.status === 'pending') {
            <p-button
              icon="pi pi-check"
              [rounded]="true"
              [text]="true"
              severity="success"
              (onClick)="approveItem(item)"
              [loading]="approving()"
            />
            <p-button
              icon="pi pi-times"
              [rounded]="true"
              [text]="true"
              severity="danger"
              (onClick)="openRejectDialog(item)"
            />
            <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="7" class="text-center p-4">{{ 'COMMON.NO_DATA' | translate }}</td>
    </tr>
  </ng-template>
</p-table>

<!-- Create/Edit Dialog -->
<p-dialog
  [header]="
    editingItem()
      ? ('HR.LEAVE_REQUESTS.EDIT' | translate)
      : ('HR.LEAVE_REQUESTS.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_REQUESTS.EMPLOYEE' | translate }} *</label>
        <p-select
          formControlName="employee_id"
          [options]="employees"
          optionLabel="full_name"
          optionValue="id"
          [showClear]="true"
          [filter]="true"
          [placeholder]="'COMMON.SELECT' | translate"
        ></p-select>
        @if (form.get('employee_id')?.touched && form.get('employee_id')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.LEAVE_REQUESTS.LEAVE_TYPE' | translate }} *</label>
        <p-select
          formControlName="leave_type_id"
          [options]="leaveTypes"
          optionLabel="name_ar"
          optionValue="id"
          [showClear]="true"
          [filter]="true"
          [placeholder]="'COMMON.SELECT' | translate"
        ></p-select>
        @if (form.get('leave_type_id')?.touched && form.get('leave_type_id')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LEAVE_REQUESTS.START_DATE' | translate }} *</label>
          <p-datepicker formControlName="start_date" dateFormat="yy-mm-dd" [showIcon]="true"></p-datepicker>
          @if (form.get('start_date')?.touched && form.get('start_date')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LEAVE_REQUESTS.END_DATE' | translate }} *</label>
          <p-datepicker formControlName="end_date" dateFormat="yy-mm-dd" [showIcon]="true"></p-datepicker>
          @if (form.get('end_date')?.touched && form.get('end_date')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.LEAVE_REQUESTS.REASON' | translate }}</label>
        <textarea pTextarea formControlName="reason" rows="3"></textarea>
      </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>

<!-- Reject Dialog -->
<p-dialog
  [header]="'HR.LEAVE_REQUESTS.REJECT' | translate"
  [(visible)]="rejectDialogVisible"
  [modal]="true"
  [style]="{ width: '450px' }"
  [closable]="true"
>
  <div class="flex flex-column gap-3 pt-3">
    <div class="flex flex-column gap-1">
      <label>{{ 'HR.LEAVE_REQUESTS.REJECTION_REASON' | translate }}</label>
      <textarea pTextarea [(ngModel)]="rejectionReason" rows="4"
        [placeholder]="'HR.LEAVE_REQUESTS.REJECTION_REASON_PLACEHOLDER' | translate"
      ></textarea>
    </div>
  </div>
  <ng-template pTemplate="footer">
    <p-button
      [label]="'COMMON.CANCEL' | translate"
      icon="pi pi-times"
      [text]="true"
      (onClick)="rejectDialogVisible.set(false)"
    />
    <p-button
      [label]="'HR.LEAVE_REQUESTS.REJECT' | translate"
      icon="pi pi-times-circle"
      severity="danger"
      (onClick)="confirmReject()"
      [loading]="rejecting()"
    />
  </ng-template>
</p-dialog>
