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

<app-page-header
  title="HR.EOS.TITLE"
  subtitle="HR.EOS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.EOS.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.EOS.CODE' | translate }}</th>
      <th>{{ 'HR.EOS.EMPLOYEE' | translate }}</th>
      <th>{{ 'HR.EOS.TERMINATION_DATE' | translate }}</th>
      <th>{{ 'HR.EOS.TERMINATION_REASON' | translate }}</th>
      <th>{{ 'HR.EOS.YEARS_OF_SERVICE' | translate }}</th>
      <th>{{ 'HR.EOS.SETTLEMENT_AMOUNT' | translate }}</th>
      <th>{{ 'HR.EOS.NET_AMOUNT' | 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.termination_date }}</td>
      <td>{{ item.termination_reason }}</td>
      <td>{{ item.years_of_service }}</td>
      <td>{{ item.settlement_amount | number:'1.2-2' }}</td>
      <td>{{ item.net_amount | number:'1.2-2' }}</td>
      <td>
        <p-tag [value]="item.status" [severity]="$any(getStatusSeverity(item.status))"></p-tag>
      </td>
      <td>
        <div class="flex gap-2">
          @if (item.status === 'draft') {
            <p-button
              icon="pi pi-calculator"
              [rounded]="true"
              [text]="true"
              severity="info"
              [pTooltip]="'HR.EOS.CALCULATE' | translate"
              (onClick)="calculateEos(item)"
              [loading]="processing()"
            />
          }
          @if (item.status === 'calculated') {
            <p-button
              icon="pi pi-check-circle"
              [rounded]="true"
              [text]="true"
              severity="success"
              [pTooltip]="'HR.EOS.APPROVE' | translate"
              (onClick)="approveEos(item)"
              [loading]="processing()"
            />
          }
          @if (item.status === 'approved') {
            <p-button
              icon="pi pi-wallet"
              [rounded]="true"
              [text]="true"
              severity="warn"
              [pTooltip]="'HR.EOS.PAY' | translate"
              (onClick)="payEos(item)"
              [loading]="processing()"
            />
          }
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editItem(item)"
          />
          @if (item.status === 'draft') {
            <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.EOS.EDIT' | translate)
      : ('HR.EOS.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.EOS.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 gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.EOS.TERMINATION_DATE' | translate }} *</label>
          <p-datePicker formControlName="termination_date" dateFormat="yy-mm-dd" [showIcon]="true"></p-datePicker>
          @if (form.get('termination_date')?.touched && form.get('termination_date')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.EOS.TERMINATION_REASON' | translate }} *</label>
          <p-select
            formControlName="termination_reason"
            [options]="terminationReasonOptions"
            optionLabel="label"
            optionValue="value"
          ></p-select>
          @if (form.get('termination_reason')?.touched && form.get('termination_reason')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.EOS.LAST_WORKING_DAY' | translate }}</label>
        <p-datePicker formControlName="last_working_day" dateFormat="yy-mm-dd" [showIcon]="true"></p-datePicker>
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.EOS.OTHER_ENTITLEMENTS' | translate }}</label>
          <p-inputNumber formControlName="other_entitlements" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.EOS.DEDUCTIONS' | translate }}</label>
          <p-inputNumber formControlName="deductions" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.EOS.NOTES' | translate }}</label>
        <textarea pTextarea formControlName="notes" 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>
