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

<div class="cost-centers-page">
  <app-page-header title="COST_CENTERS.TITLE" subtitle="COST_CENTERS.SUBTITLE" (refresh)="retry()">
    <p-button
      [label]="'COST_CENTERS.NEW_COST_CENTER' | translate"
      icon="pi pi-plus"
      (onClick)="openNew()"
    />
  </app-page-header>

  <ng-template #bodyTpl let-cc>
    <tr>
      <td class="mono">{{ cc.code }}</td>
      <td>
        <span [style.padding-inline-start.rem]="(cc.level - 1) * 1.2">
          @if (cc.has_children) {
            <i class="pi pi-folder" style="margin-inline-end: 0.375rem; color: #ffc107;"></i>
          }
          {{ cc.name_ar || cc.name }}
        </span>
      </td>
      <td>{{ cc.level }}</td>
      <td>{{ cc.parent?.name_ar || cc.parent?.name || '—' }}</td>
      <td>
        <p-tag
          [value]="cc.status === 'active' ? ('COMMON.ACTIVE' | translate) : ('COMMON.INACTIVE' | translate)"
          [severity]="cc.status === 'active' ? 'success' : 'danger'"
        />
      </td>
      <td>
        <div class="action-buttons">
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editCostCenter(cc)"
          />
          <p-button
            icon="pi pi-trash"
            [rounded]="true"
            [text]="true"
            severity="danger"
            (onClick)="deleteCostCenter(cc)"
            [disabled]="cc.has_children"
          />
        </div>
      </td>
    </tr>
  </ng-template>
  <app-data-table [data]="(costCenters$ | async) || []" [loading]="!!(loading$ | async)" [error]="(error$ | async) ?? null" (retry)="retry()" [columns]="columns" [bodyTpl]="bodyTpl">
  </app-data-table>
</div>

<!-- Create/Edit Dialog -->
<app-form-dialog
  [(visible)]="dialogVisible"
  [header]="editingCostCenter() ? ('COST_CENTERS.EDIT_COST_CENTER' | translate) : ('COST_CENTERS.NEW_COST_CENTER' | translate)"
  [saving]="!!(saving$ | async)"
  (save)="onSave()"
>
  <form [formGroup]="costCenterForm" class="dialog-form">
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'COST_CENTERS.CODE' | translate }} *</label>
        <input pInputText formControlName="code" />
        @if (costCenterForm.get('code')?.touched && costCenterForm.get('code')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="form-field">
        <label>{{ 'COMMON.STATUS' | translate }}</label>
        <p-select formControlName="status" [options]="statusOptions" optionLabel="label" optionValue="value" />
      </div>
    </div>
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'COST_CENTERS.NAME_AR' | translate }} *</label>
        <input pInputText formControlName="name_ar" />
        @if (costCenterForm.get('name_ar')?.touched && costCenterForm.get('name_ar')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="form-field">
        <label>{{ 'COST_CENTERS.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
    </div>
    <div class="form-field">
      <label>{{ 'COST_CENTERS.PARENT' | translate }}</label>
      <p-select
        formControlName="parent_id"
        [options]="parentCostCenters"
        optionLabel="name_ar"
        optionValue="id"
        [filter]="true"
        filterBy="name_ar,name_en,code"
        [showClear]="true"
        [placeholder]="'COST_CENTERS.SELECT_PARENT' | translate"
        appendTo="body"
      />
    </div>
    <div class="form-field">
      <label>{{ 'COST_CENTERS.DESCRIPTION' | translate }}</label>
      <input pInputText formControlName="description" />
    </div>
  </form>
</app-form-dialog>
