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

<app-page-header
  title="HR.LOANS.TITLE"
  subtitle="HR.LOANS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.LOANS.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': '65rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.LOANS.CODE' | translate }}</th>
      <th>{{ 'HR.LOANS.EMPLOYEE' | translate }}</th>
      <th>{{ 'HR.LOANS.AMOUNT' | translate }}</th>
      <th>{{ 'HR.LOANS.NUM_INSTALLMENTS' | translate }}</th>
      <th>{{ 'HR.LOANS.INSTALLMENT_AMOUNT' | translate }}</th>
      <th>{{ 'HR.LOANS.REMAINING_AMOUNT' | translate }}</th>
      <th>{{ 'HR.LOANS.START_DATE' | 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.amount | number:'1.2-2' }}</td>
      <td>{{ item.number_of_installments }}</td>
      <td>{{ item.installment_amount | number:'1.2-2' }}</td>
      <td>{{ item.remaining_amount | number:'1.2-2' }}</td>
      <td>{{ item.start_date }}</td>
      <td>
        <p-tag [value]="item.status" [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"
              [pTooltip]="'HR.LOANS.APPROVE' | translate"
              (onClick)="approveLoan(item)"
              [loading]="processing()"
            />
            <p-button
              icon="pi pi-times"
              [rounded]="true"
              [text]="true"
              severity="danger"
              [pTooltip]="'HR.LOANS.REJECT' | translate"
              (onClick)="rejectLoan(item)"
              [loading]="processing()"
            />
          }
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editItem(item)"
          />
          @if (item.status === 'pending') {
            <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.LOANS.EDIT' | translate)
      : ('HR.LOANS.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.LOANS.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.LOANS.LOAN_TYPE' | translate }}</label>
        <p-select
          formControlName="loan_type"
          [options]="loanTypeOptions"
          optionLabel="label"
          optionValue="value"
        ></p-select>
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LOANS.AMOUNT' | translate }} *</label>
          <p-inputNumber formControlName="amount" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
          @if (form.get('amount')?.touched && form.get('amount')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LOANS.INTEREST_RATE' | translate }}</label>
          <p-inputNumber formControlName="interest_rate" suffix="%" [min]="0" [max]="100"></p-inputNumber>
        </div>
      </div>
      <div class="flex gap-3">
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LOANS.NUM_INSTALLMENTS' | translate }} *</label>
          <p-inputNumber formControlName="number_of_installments" [min]="1" [showButtons]="true"></p-inputNumber>
          @if (form.get('number_of_installments')?.touched && form.get('number_of_installments')?.hasError('required')) {
            <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
          }
        </div>
        <div class="flex flex-column gap-1 flex-1">
          <label>{{ 'HR.LOANS.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>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.LOANS.REASON' | translate }}</label>
        <textarea pTextarea formControlName="reason" rows="2"></textarea>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.LOANS.NOTES' | translate }}</label>
        <textarea pTextarea formControlName="notes" rows="2"></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>
