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

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

  <ng-template #bodyTpl let-category>
    <tr>
      <td class="mono">{{ category.code }}</td>
      <td>{{ category.name_ar }}</td>
      <td>{{ category.name_en }}</td>
      <td>{{ category.level ?? '—' }}</td>
      <td>
        <p-tag
          [value]="category.is_active ? ('COMMON.ACTIVE' | translate) : ('COMMON.INACTIVE' | translate)"
          [severity]="category.is_active ? 'success' : 'danger'"
        />
      </td>
      <td>
        <div class="action-buttons">
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editCategory(category)"
          />
          <p-button
            icon="pi pi-trash"
            [rounded]="true"
            [text]="true"
            severity="danger"
            (onClick)="deleteCategory(category)"
          />
        </div>
      </td>
    </tr>
  </ng-template>
  <app-data-table [data]="(categories$ | 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]="editingCategory() ? ('PRODUCT_CATEGORIES.EDIT_CATEGORY' | translate) : ('PRODUCT_CATEGORIES.NEW_CATEGORY' | translate)"
  [saving]="!!(saving$ | async)"
  (save)="onSave()"
>
  <form [formGroup]="categoryForm" class="dialog-form">
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'PRODUCT_CATEGORIES.CODE' | translate }}</label>
        <input pInputText formControlName="code" [placeholder]="'COMMON.AUTO_GENERATED' | translate" />
      </div>
      <div class="form-field">
        <label>{{ 'PRODUCT_CATEGORIES.NAME_AR' | translate }} *</label>
        <input pInputText formControlName="name_ar" />
        @if (categoryForm.get('name_ar')?.touched && categoryForm.get('name_ar')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
    </div>
    <div class="form-field">
      <label>{{ 'PRODUCT_CATEGORIES.NAME_EN' | translate }}</label>
      <input pInputText formControlName="name_en" />
    </div>
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'PRODUCT_CATEGORIES.PARENT' | translate }}</label>
        <p-select
          formControlName="parent_id"
          [options]="(categories$ | async) || []"
          optionLabel="name"
          optionValue="id"
          [filter]="true"
          filterBy="name,name_ar,name_en,code"
          [showClear]="true"
          [placeholder]="'PRODUCT_CATEGORIES.SELECT_PARENT' | translate"
        />
      </div>
      <div class="form-field">
        <label>{{ 'PRODUCT_CATEGORIES.DEFAULT_UNIT' | translate }}</label>
        <p-select
          formControlName="default_unit_id"
          [options]="(units$ | async) || []"
          optionLabel="name"
          optionValue="id"
          [filter]="true"
          filterBy="name,name_ar,name_en"
          [showClear]="true"
          [placeholder]="'PRODUCT_CATEGORIES.SELECT_UNIT' | translate"
        />
      </div>
    </div>
    <div class="form-field">
      <label>{{ 'PRODUCT_CATEGORIES.DESCRIPTION' | translate }}</label>
      <input pInputText formControlName="description" />
    </div>
    <div class="form-field switch-field">
      <label>{{ 'COMMON.ACTIVE' | translate }}</label>
      <p-toggleSwitch formControlName="is_active" />
    </div>
  </form>
</app-form-dialog>
