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

<app-page-header
  title="HR.JOB_OPENINGS.TITLE"
  subtitle="HR.JOB_OPENINGS.SUBTITLE"
  (refresh)="loadPage()"
>
  <p-button
    [label]="'HR.JOB_OPENINGS.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.JOB_OPENINGS.CODE' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.TITLE_AR' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.DEPARTMENT' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.VACANCIES' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.OPENING_DATE' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.CLOSING_DATE' | translate }}</th>
      <th>{{ 'HR.JOB_OPENINGS.APPLICATIONS_COUNT' | 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.title_ar }}</td>
      <td>{{ item.department?.name_ar || '-' }}</td>
      <td>{{ item.vacancies }}</td>
      <td>{{ item.opening_date || '-' }}</td>
      <td>{{ item.closing_date || '-' }}</td>
      <td>{{ item.applications_count ?? 0 }}</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.JOB_OPENINGS.EDIT' | translate)
      : ('HR.JOB_OPENINGS.NEW' | translate)
  "
  [(visible)]="dialogVisible"
  [modal]="true"
  [style]="{ width: '700px' }"
  [closable]="true"
>
  <form [formGroup]="form" (ngSubmit)="onSave()">
    <div class="flex flex-column gap-3 pt-3">
      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.TITLE_AR' | translate }} *</label>
            <input pInputText formControlName="title_ar" />
            @if (form.get('title_ar')?.touched && form.get('title_ar')?.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.JOB_OPENINGS.TITLE_EN' | translate }}</label>
            <input pInputText formControlName="title_en" />
          </div>
        </div>
      </div>

      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.DEPARTMENT' | translate }}</label>
            <p-select
              formControlName="department_id"
              [options]="departments"
              optionLabel="name_ar"
              optionValue="id"
              [showClear]="true"
              [placeholder]="'COMMON.SELECT' | translate"
              [filter]="true"
            ></p-select>
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.POSITION' | translate }}</label>
            <p-select
              formControlName="position_id"
              [options]="positions"
              optionLabel="name_ar"
              optionValue="id"
              [showClear]="true"
              [placeholder]="'COMMON.SELECT' | translate"
              [filter]="true"
            ></p-select>
          </div>
        </div>
      </div>

      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.BRANCH' | translate }}</label>
            <p-select
              formControlName="branch_id"
              [options]="branches"
              optionLabel="name_ar"
              optionValue="id"
              [showClear]="true"
              [placeholder]="'COMMON.SELECT' | translate"
              [filter]="true"
            ></p-select>
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.VACANCIES' | translate }}</label>
            <p-inputNumber formControlName="vacancies" [min]="1" [showButtons]="true"></p-inputNumber>
          </div>
        </div>
      </div>

      <div class="flex flex-column gap-1">
        <label>{{ 'HR.JOB_OPENINGS.DESCRIPTION' | translate }}</label>
        <textarea pTextarea formControlName="description" rows="3"></textarea>
      </div>

      <div class="flex flex-column gap-1">
        <label>{{ 'HR.JOB_OPENINGS.REQUIREMENTS' | translate }}</label>
        <textarea pTextarea formControlName="requirements" rows="3"></textarea>
      </div>

      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.SALARY_FROM' | translate }}</label>
            <p-inputNumber formControlName="salary_from" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.SALARY_TO' | translate }}</label>
            <p-inputNumber formControlName="salary_to" mode="decimal" [minFractionDigits]="2" [maxFractionDigits]="2"></p-inputNumber>
          </div>
        </div>
      </div>

      <div class="grid">
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.OPENING_DATE' | translate }}</label>
            <p-datepicker formControlName="opening_date" dateFormat="yy-mm-dd" [showIcon]="true"></p-datepicker>
          </div>
        </div>
        <div class="col-6">
          <div class="flex flex-column gap-1">
            <label>{{ 'HR.JOB_OPENINGS.CLOSING_DATE' | translate }}</label>
            <p-datepicker formControlName="closing_date" dateFormat="yy-mm-dd" [showIcon]="true"></p-datepicker>
          </div>
        </div>
      </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>
