<app-ui-card [title]="getCardTitle()">
   <a
    helper-text
    href="javascript:void(0);"
    class="icon-link icon-link-hover link-primary fw-semibold"
    >
    <ng-icon [name]="getHelperIcon()" class="me-1"></ng-icon>
    {{ getHelperText() }}
  </a>

  <div card-body>
  
    <!-- Barre d'actions supérieure -->
    <div class="row mb-3">
      <div class="col-md-6">
        <div class="d-flex align-items-center gap-2">
          <!-- Statistiques rapides -->
          <div class="btn-group btn-group-sm">
            <button 
              type="button" 
              class="btn btn-outline-primary"
              [class.active]="statusFilter === 'all'"
              (click)="filterByStatus('all')"
            >
              Tous ({{ getTotalPaymentsCount() }})
            </button>
            <button 
              type="button" 
              class="btn btn-outline-success"
              [class.active]="statusFilter === 'SUCCESS'"
              (click)="filterByStatus('SUCCESS')"
            >
              Réussis ({{ getSuccessfulPaymentsCount() }})
            </button>
            <button 
              type="button" 
              class="btn btn-outline-danger"
              [class.active]="statusFilter === 'FAILED'"
              (click)="filterByStatus('FAILED')"
            >
              Échoués ({{ getFailedPaymentsCount() }})
            </button>
            <button 
              type="button" 
              class="btn btn-outline-warning text-dark"
              [class.active]="statusFilter === 'PENDING'"
              (click)="filterByStatus('PENDING')"
            >
              En attente ({{ getPendingPaymentsCount() }})
            </button>
          </div>
        </div>
      </div>

      <div class="col-md-6">
        <div class="d-flex justify-content-end gap-2">
          <button 
            class="btn btn-outline-secondary" 
            (click)="refreshData()"
            [disabled]="loading"
          >
            <ng-icon name="lucideRefreshCw" class="me-1" [class.spin]="loading"></ng-icon>
            Actualiser
          </button>
        </div>
      </div>
    </div>

    <!-- Barre de recherche et filtres avancés -->
    <div class="row mb-3">
      <div class="col-md-4">
        <div class="input-group">
          <span class="input-group-text">
            <ng-icon name="lucideSearch"></ng-icon>
          </span>
          <input 
            type="text" 
            class="form-control" 
            placeholder="Rechercher par référence, description..."
            [(ngModel)]="searchTerm"
            (input)="onSearch()"
            [disabled]="loading"
          >
        </div>
      </div>
      
      <div class="col-md-3">
        <select class="form-select" [(ngModel)]="statusFilter" (change)="applyFiltersAndPagination()">
          @for (status of availableStatuses; track status.value) {
            <option [value]="status.value">{{ status.label }}</option>
          }
        </select>
      </div>
      
      <div class="col-md-3">
        <select class="form-select" [(ngModel)]="dateFilter" (change)="applyFiltersAndPagination()">
          @for (dateRange of availableDateRanges; track dateRange.value) {
            <option [value]="dateRange.value">{{ dateRange.label }}</option>
          }
        </select>
      </div>
      
      <div class="col-md-2">
        <button class="btn btn-outline-secondary w-100" (click)="onClearFilters()" [disabled]="loading">
          <ng-icon name="lucideX" class="me-1"></ng-icon>
          Effacer
        </button>
      </div>
    </div>

    <!-- Loading State -->
    @if (loading) {
      <div class="text-center py-4">
        <div class="spinner-border text-primary" role="status">
          <span class="visually-hidden">Chargement...</span>
        </div>
        <p class="mt-2 text-muted">{{ getLoadingText() }}</p>
      </div>
    }

    <!-- Error State -->
    @if (error && !loading) {
      <div class="alert alert-danger" role="alert">
        <div class="d-flex align-items-center">
          <ng-icon name="lucideAlertCircle" class="me-2"></ng-icon>
          <div>{{ error }}</div>
          <button class="btn-close ms-auto" (click)="error = ''"></button>
        </div>
      </div>
    }

    <!-- Payments Table -->
    @if (!loading && !error) {
      <div class="table-responsive">
        <table class="table table-hover table-striped">
          <thead class="table-light">
            <tr>
              <!-- Colonne Abonnement pour la vue globale -->
              @if (showSubscriptionColumn) {
                <th>Abonnement</th>
              }
              <th (click)="sort('reference')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Référence</span>
                  <ng-icon [name]="getSortIcon('reference')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th (click)="sort('createdAt')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Date</span>
                  <ng-icon [name]="getSortIcon('createdAt')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th (click)="sort('amount')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Montant</span>
                  <ng-icon [name]="getSortIcon('amount')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th (click)="sort('status')" class="cursor-pointer">
                <div class="d-flex align-items-center">
                  <span>Statut</span>
                  <ng-icon [name]="getSortIcon('status')" class="ms-1 fs-12"></ng-icon>
                </div>
              </th>
              <th width="120">Actions</th>
            </tr>
          </thead>
          <tbody>
            @for (payment of displayedPayments; track payment.id) {
              <tr>
                <!-- Colonne Abonnement pour la vue globale -->
                @if (showSubscriptionColumn) {
                  <td>
                    <div class="d-flex align-items-center">
                      <div class="avatar-sm bg-primary bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-2">
                        <ng-icon name="lucideRepeat" class="text-primary fs-12"></ng-icon>
                      </div>
                      <div>
                        <small class="text-muted">
                          #{{ payment.subscriptionId }}
                        </small>
                        <button 
                          class="btn btn-link btn-sm p-0 ms-1"
                          (click)="navigateToSubscription(payment.subscriptionId)"
                          title="Voir l'abonnement"
                        >
                          <ng-icon name="lucideExternalLink" size="12"></ng-icon>
                        </button>
                      </div>
                    </div>
                  </td>
                }
                <td>
                  <div class="d-flex align-items-center">
                    <div class="avatar-sm bg-info bg-opacity-10 rounded-circle d-flex align-items-center justify-content-center me-2">
                      <ng-icon name="lucideCreditCard" class="text-info fs-12"></ng-icon>
                    </div>
                    <div>
                      <strong class="d-block font-monospace small">
                        {{ truncateText(payment.reference, 15) }}
                      </strong>
                      <small class="text-muted">
                        Ref: {{ getInternalReference(payment) }}
                      </small>
                    </div>
                  </div>
                </td>
                <td>
                  <div>
                    <small class="text-muted d-block">
                      {{ formatDate(payment.createdAt) }}
                    </small>
                    <small class="text-muted">
                      {{ formatDateTime(payment.createdAt) }}
                    </small>
                  </div>
                </td>
                <td>
                  <strong class="d-block text-success">
                    {{ formatAmount(payment.amount, payment.currency) }}
                  </strong>
                  <small class="text-muted">{{ payment.currency }}</small>
                </td>
                <td>
                  <span class="badge d-flex align-items-center" [ngClass]="getPaymentStatusBadgeClass(payment.status)">
                    <ng-icon [name]="getPaymentStatusIcon(payment.status)" class="me-1" size="14"></ng-icon>
                    {{ getPaymentStatusDisplayName(payment.status) }}
                  </span>
                  @if (payment.description) {
                    <div class="mt-1">
                      <small class="text-muted">
                        {{ truncateText(payment.description, 30) }}
                      </small>
                    </div>
                  }
                </td>
                <td>
                  <div class="btn-group btn-group-sm" role="group">
                    <button 
                      class="btn btn-outline-primary btn-sm"
                      (click)="viewPaymentDetails(payment.id)"
                      title="Voir les détails"
                    >
                      <ng-icon name="lucideEye"></ng-icon>
                    </button>
                    <button 
                      class="btn btn-outline-info btn-sm"
                      (click)="navigateToSubscription(payment.subscriptionId)"
                      title="Voir l'abonnement"
                    >
                      <ng-icon name="lucideRepeat"></ng-icon>
                    </button>
                  </div>
                </td>
              </tr>
            }
            @empty {
              <tr>
                <td [attr.colspan]="getColumnCount()" class="text-center py-4">
                  <div class="text-muted">
                    <ng-icon name="lucideCreditCard" class="fs-1 mb-3 opacity-50"></ng-icon>
                    <h5 class="mb-2">{{ getEmptyStateTitle() }}</h5>
                    <p class="mb-3">{{ getEmptyStateDescription() }}</p>
                  </div>
                </td>
              </tr>
            }
          </tbody>
        </table>
      </div>

      <!-- Pagination -->
      @if (totalPages > 1) {
        <div class="d-flex justify-content-between align-items-center mt-3">
          <div class="text-muted">
            Affichage de {{ getStartIndex() }} à {{ getEndIndex() }} sur {{ totalItems }} paiements
          </div>
          <nav>
            <ngb-pagination
              [collectionSize]="totalItems"
              [page]="currentPage"
              [pageSize]="itemsPerPage"
              [maxSize]="5"
              [rotate]="true"
              [boundaryLinks]="true"
              (pageChange)="onPageChange($event)"
            />
          </nav>
        </div>
      }

      <!-- Résumé des résultats -->
      @if (displayedPayments.length > 0) {
        <div class="mt-3 pt-3 border-top">
          <div class="row text-center">
            <div class="col">
              <small class="text-muted">
                <strong>Total :</strong> {{ getPaymentsSummary().total }} paiements
              </small>
            </div>
            <div class="col">
              <small class="text-muted">
                <strong>Réussis :</strong> {{ getPaymentsSummary().success }}
              </small>
            </div>
            <div class="col">
              <small class="text-muted">
                <strong>Échoués :</strong> {{ getPaymentsSummary().failed }}
              </small>
            </div>
            <div class="col">
              <small class="text-muted">
                <strong>En attente :</strong> {{ getPaymentsSummary().pending }}
              </small>
            </div>
            <div class="col">
              <small class="text-muted">
                <strong>Montant total :</strong> {{ formatAmount(getPaymentsSummary().amount, Currency.XOF) }}
              </small>
            </div>
            <div class="col">
              <small class="text-muted">
                <strong>Taux de réussite :</strong> {{ getSuccessRate().toFixed(1) }}%
              </small>
            </div>
          </div>
        </div>
      }
    }
  </div>
</app-ui-card>