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

<app-page-header
  title="HR.PERFORMANCE_REVIEWS.TITLE"
  subtitle="HR.PERFORMANCE_REVIEWS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.PERFORMANCE_REVIEWS.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': '70rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.CODE' | translate }}</th>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.EMPLOYEE' | translate }}</th>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.REVIEWER' | translate }}</th>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.PERIOD_START' | translate }}</th>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.PERIOD_END' | translate }}</th>
      <th>{{ 'HR.PERFORMANCE_REVIEWS.OVERALL_RATING' | 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.employee?.full_name || '-' }}</td>
      <td>{{ item.reviewer?.full_name || '-' }}</td>
      <td>{{ item.review_period_start }}</td>
      <td>{{ item.review_period_end }}</td>
      <td>{{ item.overall_rating || '-' }}</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)"
          />
          @if (item.status === 'draft') {
            <p-button
              [label]="'HR.PERFORMANCE_REVIEWS.SUBMIT' | translate"
              icon="pi pi-send"
              severity="info"
              size="small"
              (onClick)="submitReview(item)"
            />
          }
          @if (item.status === 'submitted') {
            <p-button
              [label]="'HR.PERFORMANCE_REVIEWS.APPROVE' | translate"
              icon="pi pi-check-circle"
              severity="success"
              size="small"
              (onClick)="approveReview(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>

<!-- Review Dialog -->
<p-dialog
  [header]="
    editingItem()
      ? ('HR.PERFORMANCE_REVIEWS.EDIT' | translate)
      : ('HR.PERFORMANCE_REVIEWS.NEW' | translate)
  "
  [(visible)]="dialogVisible"
  [modal]="true"
  [style]="{ width: '600px' }"
  [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.PERFORMANCE_REVIEWS.EMPLOYEE' | translate }} *</label>
        <p-select
          formControlName="employee_id"
          [options]="employees"
          optionLabel="full_name"
          optionValue="id"
          [showClear]="true"
          [placeholder]="'COMMON.SELECT' | translate"
          [filter]="true"
        ></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.PERFORMANCE_REVIEWS.REVIEWER' | translate }}</label>
        <p-select
          formControlName="reviewer_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.PERFORMANCE_REVIEWS.TEMPLATE' | translate }}</label>
        <p-select
          formControlName="template_id"
          [options]="templates"
          optionLabel="name_ar"
          optionValue="id"
          [showClear]="true"
          [placeholder]="'COMMON.SELECT' | translate"
          [filter]="true"
        ></p-select>
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.PERFORMANCE_REVIEWS.PERIOD_START' | translate }}</label>
          <p-datePicker
            formControlName="review_period_start"
            dateFormat="yy-mm-dd"
            [showIcon]="true"
            [placeholder]="'HR.PERFORMANCE_REVIEWS.PERIOD_START' | translate"
          ></p-datePicker>
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.PERFORMANCE_REVIEWS.PERIOD_END' | translate }}</label>
          <p-datePicker
            formControlName="review_period_end"
            dateFormat="yy-mm-dd"
            [showIcon]="true"
            [placeholder]="'HR.PERFORMANCE_REVIEWS.PERIOD_END' | translate"
          ></p-datePicker>
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.PERFORMANCE_REVIEWS.COMMENTS' | translate }}</label>
        <textarea pTextarea formControlName="comments" 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>
