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

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

  <ng-template #bodyTpl let-currency>
    <tr>
      <td class="mono">{{ currency.code }}</td>
      <td>{{ currency.name_ar || currency.name }}</td>
      <td>{{ currency.symbol }}</td>
      <td>{{ currency.decimals }}</td>
      <td>{{ currency.exchange_rate }}</td>
      <td>
        @if (currency.is_base) {
          <p-tag [value]="'CURRENCIES.BASE' | translate" severity="success" />
        }
      </td>
      <td>
        <p-tag
          [value]="currency.is_active ? ('COMMON.ACTIVE' | translate) : ('COMMON.INACTIVE' | translate)"
          [severity]="currency.is_active ? 'success' : 'danger'"
        />
      </td>
      <td>
        <div class="action-buttons">
          <p-button
            icon="pi pi-pencil"
            [rounded]="true"
            [text]="true"
            severity="info"
            (onClick)="editCurrency(currency)"
          />
          <p-button
            icon="pi pi-trash"
            [rounded]="true"
            [text]="true"
            severity="danger"
            (onClick)="deleteCurrency(currency)"
            [disabled]="currency.is_base"
          />
        </div>
      </td>
    </tr>
  </ng-template>
  <app-data-table [data]="(currencies$ | 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]="editingCurrency() ? ('CURRENCIES.EDIT_CURRENCY' | translate) : ('CURRENCIES.NEW_CURRENCY' | translate)"
  [saving]="!!(saving$ | async)"
  (save)="onSave()"
>
  <form [formGroup]="currencyForm" class="dialog-form">
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'CURRENCIES.CODE' | translate }} *</label>
        <input pInputText formControlName="code" placeholder="SAR" />
        @if (currencyForm.get('code')?.touched && currencyForm.get('code')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="form-field">
        <label>{{ 'CURRENCIES.SYMBOL' | translate }} *</label>
        <input pInputText formControlName="symbol" />
        @if (currencyForm.get('symbol')?.touched && currencyForm.get('symbol')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
    </div>
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'CURRENCIES.NAME_AR' | translate }} *</label>
        <input pInputText formControlName="name_ar" />
        @if (currencyForm.get('name_ar')?.touched && currencyForm.get('name_ar')?.hasError('required')) {
          <small class="p-error">{{ 'COMMON.REQUIRED' | translate }}</small>
        }
      </div>
      <div class="form-field">
        <label>{{ 'CURRENCIES.NAME_EN' | translate }}</label>
        <input pInputText formControlName="name_en" />
      </div>
    </div>
    <div class="form-row">
      <div class="form-field">
        <label>{{ 'CURRENCIES.DECIMALS' | translate }}</label>
        <p-inputNumber formControlName="decimals" [min]="0" [max]="6" />
      </div>
      <div class="form-field">
        <label>{{ 'CURRENCIES.EXCHANGE_RATE' | translate }}</label>
        <p-inputNumber formControlName="exchange_rate" [mode]="'decimal'" [minFractionDigits]="2" [maxFractionDigits]="6" />
      </div>
    </div>
    <div class="switch-row">
      <div class="switch-item">
        <p-toggleSwitch formControlName="is_base" />
        <label>{{ 'CURRENCIES.BASE' | translate }}</label>
      </div>
      <div class="switch-item">
        <p-toggleSwitch formControlName="is_active" />
        <label>{{ 'COMMON.ACTIVE' | translate }}</label>
      </div>
    </div>
  </form>
</app-form-dialog>
