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

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

  <ng-template #bodyTpl let-category>
    <tr>
      <td>{{ category.name_ar }}</td>
      <td>{{ category.name_en }}</td>
      <td>
        <p-tag
          [value]="category.scope_label || category.scope"
          [severity]="category.scope === 'customer' ? 'info' : category.scope === 'supplier' ? 'warn' : 'secondary'"
        />
      </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() ? ('PARTNER_CATEGORIES.EDIT_CATEGORY' | translate) : ('PARTNER_CATEGORIES.NEW_CATEGORY' | translate)"
  [saving]="!!(saving$ | async)"
  [width]="'500px'"
  (save)="onSave()"
>
  <form [formGroup]="categoryForm" class="dialog-form">
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'PARTNER_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 class="form-field">
        <label>{{ 'PARTNER_CATEGORIES.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
    </div>
    <div class="form-field">
      <label>{{ 'PARTNER_CATEGORIES.SCOPE' | translate }}</label>
      <p-select formControlName="scope" [options]="scopeOptions" optionLabel="label" optionValue="value" />
    </div>
    <div class="form-field">
      <label>{{ 'PARTNER_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>
