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

<app-page-header
  title="HR.SALARY_COMPONENTS.TITLE"
  subtitle="HR.SALARY_COMPONENTS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.SALARY_COMPONENTS.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': '60rem' }"
>
  <ng-template pTemplate="header">
    <tr>
      <th>{{ 'HR.SALARY_COMPONENTS.CODE' | translate }}</th>
      <th>{{ 'HR.SALARY_COMPONENTS.NAME_AR' | translate }}</th>
      <th>{{ 'HR.SALARY_COMPONENTS.TYPE' | translate }}</th>
      <th>{{ 'HR.SALARY_COMPONENTS.CALCULATION_TYPE' | translate }}</th>
      <th>{{ 'HR.SALARY_COMPONENTS.AMOUNT_PERCENTAGE' | translate }}</th>
      <th>{{ 'HR.SALARY_COMPONENTS.IS_TAXABLE' | 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>
        <p-tag
          [value]="item.type === 'earning' ? ('HR.SALARY_COMPONENTS.EARNING' | translate) : ('HR.SALARY_COMPONENTS.DEDUCTION' | translate)"
          [severity]="$any(getTypeSeverity(item.type))"
        ></p-tag>
      </td>
      <td>{{ item.calculation_type }}</td>
      <td>
        @if (item.calculation_type === 'percentage') {
          {{ item.percentage }}%
        } @else {
          {{ item.amount | number:'1.2-2' }}
        }
      </td>
      <td>
        <p-tag
          [value]="item.is_taxable ? ('COMMON.YES' | translate) : ('COMMON.NO' | translate)"
          [severity]="$any(item.is_taxable ? 'warn' : '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>

<p-dialog
  [header]="
    editingItem()
      ? ('HR.SALARY_COMPONENTS.EDIT' | translate)
      : ('HR.SALARY_COMPONENTS.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.SALARY_COMPONENTS.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.SALARY_COMPONENTS.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.SALARY_COMPONENTS.TYPE' | translate }}</label>
        <p-select
          formControlName="type"
          [options]="typeOptions"
          optionLabel="label"
          optionValue="value"
        ></p-select>
      </div>
      <div class="flex gap-4 align-items-center">
        <div class="flex align-items-center gap-2">
          <label>{{ 'HR.SALARY_COMPONENTS.IS_TAXABLE' | translate }}</label>
          <p-toggleSwitch formControlName="is_taxable" />
        </div>
        <div class="flex align-items-center gap-2">
          <label>{{ 'HR.SALARY_COMPONENTS.IS_FIXED' | translate }}</label>
          <p-toggleSwitch formControlName="is_fixed" />
        </div>
      </div>
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.SALARY_COMPONENTS.CALCULATION_TYPE' | translate }}</label>
        <p-select
          formControlName="calculation_type"
          [options]="calculationTypeOptions"
          optionLabel="label"
          optionValue="value"
        ></p-select>
      </div>
      @if (form.get('calculation_type')?.value === 'fixed') {
        <div class="flex flex-column gap-1">
          <label>{{ 'HR.SALARY_COMPONENTS.AMOUNT' | translate }}</label>
          <p-inputNumber formControlName="amount" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
        </div>
      }
      @if (form.get('calculation_type')?.value === 'percentage') {
        <div class="flex flex-column gap-1">
          <label>{{ 'HR.SALARY_COMPONENTS.PERCENTAGE' | translate }}</label>
          <p-inputNumber formControlName="percentage" suffix="%" [min]="0" [max]="100"></p-inputNumber>
        </div>
        <div class="flex flex-column gap-1">
          <label>{{ 'HR.SALARY_COMPONENTS.PERCENTAGE_OF' | translate }}</label>
          <input pInputText formControlName="percentage_of" />
        </div>
      }
      <div class="flex flex-column gap-1">
        <label>{{ 'HR.SALARY_COMPONENTS.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>
