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

<app-page-header
  title="HR.SHIFTS.TITLE"
  subtitle="HR.SHIFTS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.SHIFTS.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.SHIFTS.CODE' | translate }}</th>
      <th>{{ 'HR.SHIFTS.NAME_AR' | translate }}</th>
      <th>{{ 'HR.SHIFTS.NAME_EN' | translate }}</th>
      <th>{{ 'HR.SHIFTS.START_TIME' | translate }}</th>
      <th>{{ 'HR.SHIFTS.END_TIME' | translate }}</th>
      <th>{{ 'HR.SHIFTS.WORKING_HOURS' | translate }}</th>
      <th>{{ 'HR.SHIFTS.NIGHT_SHIFT' | 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>{{ item.name_en }}</td>
      <td>{{ item.start_time }}</td>
      <td>{{ item.end_time }}</td>
      <td>{{ item.working_hours }}</td>
      <td>
        <p-tag
          [value]="item.is_night_shift ? ('COMMON.YES' | translate) : ('COMMON.NO' | translate)"
          [severity]="$any(item.is_night_shift ? 'warn' : 'info')"
        ></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="9" class="text-center p-4">{{ 'COMMON.NO_DATA' | translate }}</td>
    </tr>
  </ng-template>
</p-table>

<p-dialog
  [header]="
    editingItem()
      ? ('HR.SHIFTS.EDIT' | translate)
      : ('HR.SHIFTS.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.SHIFTS.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.SHIFTS.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.SHIFTS.START_TIME' | translate }} *</label>
            <input pInputText formControlName="start_time" placeholder="HH:MM" />
            @if (form.get('start_time')?.touched && form.get('start_time')?.hasError('required')) {
              <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
            }
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.SHIFTS.END_TIME' | translate }} *</label>
            <input pInputText formControlName="end_time" placeholder="HH:MM" />
            @if (form.get('end_time')?.touched && form.get('end_time')?.hasError('required')) {
              <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
            }
          </div>
        </div>
      </div>
      <div class="grid">
        <div class="col-4">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.SHIFTS.WORKING_HOURS' | translate }}</label>
            <p-inputNumber formControlName="working_hours" [min]="0" [max]="24"></p-inputNumber>
          </div>
        </div>
        <div class="col-4">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.SHIFTS.BREAK_DURATION' | translate }}</label>
            <p-inputNumber
              formControlName="break_duration"
              [min]="0"
              suffix=" min"
            ></p-inputNumber>
          </div>
        </div>
        <div class="col-4">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.SHIFTS.GRACE_PERIOD' | translate }}</label>
            <p-inputNumber
              formControlName="grace_period"
              [min]="0"
              suffix=" min"
            ></p-inputNumber>
          </div>
        </div>
      </div>
      <div class="flex align-items-center gap-3">
        <label>{{ 'HR.SHIFTS.NIGHT_SHIFT' | translate }}</label>
        <p-toggleSwitch formControlName="is_night_shift"></p-toggleSwitch>
      </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>
