<div id="layout-wrapper">
  <div class="inner-pg-sp">

    <div class="container-fluid">
      <div class="row">
        <div class="col-12">
          <div class="d-sm-flex align-items-center justify-content-between navbar-header p-0"></div>
        </div>
      </div>
    </div>
    
    <div class="container-fluid">
      <!-- SEARCH FORM -->
      <div class="col-xl-12 mt-4">
        <div class="card border">
          <div class="card-body">
            <div class="card-header font-edit-13-child mb-3">
              {{ "transactionLogs" | translate }}
            </div>

            <form [formGroup]="logsSearchForm">
              <div class="row g-3 mb-3">
                <div class="col-md-6">
                  <label>
                    {{ "fromDate" | translate }}
                    <span class="mandatory">*</span>
                  </label>
                  <div class="date-input-wrapper">
                    <input
                      type="date"
                      formControlName="fromDate"
                      class="form-control"
                      [max]="maxDate"
                    />
                    <i class="fas fa-calendar calendar-icon"></i>
                  </div>
                  <div class="text-danger"
                    *ngIf="logsSearchForm.get('fromDate')?.touched && logsSearchForm.get('fromDate')?.invalid">
                    {{ 'fieldRequired' | translate }}
                  </div>
                </div>

                <div class="col-md-6">
                  <label>
                    {{ "toDate" | translate }} <span class="mandatory">*</span>
                  </label>
                  <div class="date-input-wrapper">
                    <input
                      type="date"
                      formControlName="toDate"
                      class="form-control"
                      [max]="maxDate"
                    />
                    <i class="fas fa-calendar calendar-icon"></i>
                  </div>
                  <div class="text-danger"
                    *ngIf="logsSearchForm.get('toDate')?.touched && logsSearchForm.get('toDate')?.invalid">
                    {{ 'fieldRequired' | translate }}
                  </div>
                  <div class="text-danger" *ngIf="
                      logsSearchForm.touched &&
                      logsSearchForm.errors?.['fromDateGreaterThanOrEqualToToDate']
                    ">
                    {{ 'toDateInvalidError' | translate }}
                  </div>
                </div>
              </div>

              <div class="text-end">
                <button
                  class="btn btn-primary"
                  [disabled]="logsSearchForm.invalid"
                  (click)="getlogsData()"
                >
                  {{ "findLogs" | translate }}
                </button>
              </div>
            </form>
          </div>
        </div>
      </div>

      <!-- TABLE -->
      <div class="col-xl-12 mt-4">
        <div class="card border">
          <div class="card-body">
            <div class="card-header d-flex justify-content-between align-items-center font-edit-13-child">
              {{ "transactionLogDetails" | translate }}

              <div class="d-flex gap-2 align-items-center" *ngIf="allItems.length">
                <div class="search-box">
                  <input
                    type="text"
                    class="form-control form-control-sm"
                    placeholder="{{ 'search' | translate }}"
                    [(ngModel)]="searchText"
                    (ngModelChange)="applySearch()"
                  />
                  <i class="fas fa-search search-icon"></i>
                </div>

                <div class="d-flex align-items-center gap-2">
                  <button
                    class="btn btn-sm btn-outline-success"
                    (click)="exportDataInExcel()"
                    [disabled]="!filteredItems.length"
                    title="Export to Excel"
                  >
                    <i class="fa fa-download"></i>
                  </button>
                </div>
                
                <button
                  class="btn btn-sm btn-outline-secondary"
                  (click)="toggleTableCard()"
                  [title]="(logsDataExpanded ? 'collapse' : 'expand') | translate"
                >
                  <i *ngIf="logsDataExpanded" class="dripicons-chevron-up"></i>
                  <i *ngIf="!logsDataExpanded" class="dripicons-chevron-down"></i>
                </button>
              </div>
            </div>
          </div>
          
          <div class="card-body" *ngIf="logsDataExpanded">
            <!-- NO RECORDS -->
            <div *ngIf="!filteredItems.length" class="text-center text-muted py-4">
              <i class="fas fa-search fa-2x mb-2 opacity-50"></i>
              <p>{{ "noTransactionLogsFound" | translate }}</p>
            </div>

            <!-- TABLE -->
            <div *ngIf="filteredItems.length" class="table-responsive">
              <table class="table table-bordered mb-0">
                <thead class="table-light">
                  <tr>
                    <th scope="col">{{ "logID" | translate }}</th>
                                <th scope="col">{{ "organization" | translate }}</th>
                                <th scope="col">{{ "transactionID" | translate }}</th>
                                <th scope="col">{{ "drAccount" | translate }}</th>
                                <th scope="col">{{ "crAccount" | translate }}</th>
                                <th scope="col">{{ "drPcaGlacode" | translate }}</th>
                                <th scope="col">{{ "crPcaglacode" | translate }}</th>
                                <th scope="col">{{ "transactionUri" | translate }}</th>
                                <th scope="col">{{ "transactionCode" | translate }}</th>
                                <!-- <th scope="col">{{ "paymentMode" | translate }}</th> -->
                                <th scope="col">{{ "date" | translate }}</th>
                                <th scope="col">{{ "channel" | translate }}</th>
                                <th scope="col">{{ "createdAt" | translate }}</th>
                  </tr>
                </thead>

                <tbody>
                  <tr *ngFor="let log of pagedItems | tableFilter: searchText: [
                                    'logId',
                                    'porOrgacode',
                                    'transactionID',
                                    'drMbmbkmsnumber',
                                    'crMbmbkmsnumber',
                                    'drPcaGlacode',
                                    'crPcaglacode',
                                    'transactionUri',
                                    'transactionCode',
                                    
                                    'sgtGntrdate',
                                    'channelCode',
                                    'createdAt'
                                  ]">
                                <td><code>{{ log.logId || '-' }}</code></td>
                                <td>{{ log.porOrgacode || '-' }}</td>
                                <td><strong>{{ log.transactionID || '-' }}</strong></td>
                                <td>{{ log.drMbmbkmsnumber || '-' }}</td>
                                <td>{{ log.crMbmbkmsnumber || '-' }}</td>
                                <td>{{ log.drPcaGlacode || '-' }}</td>
                                <td>{{ log.crPcaglacode || '-' }}</td>
                                <td><small class="text-muted">{{ log.transactionUri || 'N/A' }}</small></td>
                                <td><span class="badge bg-secondary">{{ log.transactionCode || 'N/A' }}</span></td>
                                <!-- <td>{{ log.ppmPymdcode || '-' }}</td> -->
                                <td>{{ log.sgtGntrdate | date: 'MMM dd, yyyy' }}</td>
                                <td>{{ log.channelCode || '-' }}</td>
                                <td>{{ log.createdAt | date: 'MMM dd, yyyy HH:mm' }}</td>
                              </tr>
                </tbody>
              </table>

              <!-- PAGINATION FOOTER -->
              <div class="d-flex justify-content-between align-items-center mt-3 flex-wrap">
                <!-- Items per page -->
                <div class="d-flex align-items-center gap-2 mb-2 mb-md-0">
                  <span class="text-muted small">{{ "show" | translate }}</span>
                  <div style="width: 120px;">
                    <ng-select
                      class="form-select-sm"
                      [items]="pageSizeOptions"
                      bindValue="value"
                      [(ngModel)]="itemsPerPage"
                      (change)="itemsPerPageChanged()"
                      [searchable]="false"
                      [clearable]="false"
                      [dropdownPosition]="'top'"
                    >
                      <ng-template ng-option-tmp let-item="item">
                        {{ item.value }} {{ "entries" | translate }}
                      </ng-template>

                      <ng-template ng-label-tmp let-item="item">
                        {{ item.value }} {{ "entries" | translate }}
                      </ng-template>
                    </ng-select>
                  </div>
                </div>

                <!-- Page info -->
                <div class="text-muted mb-2 mb-md-0">
                  {{ "page" | translate }} {{ currentPage }}
                  {{ "of" | translate }} {{ totalPages() }}
                  <span class="d-none d-md-inline">
                    ({{ filteredItems.length }} {{ "totalItems" | translate }})
                  </span>
                </div>

                <!-- Pagination buttons -->
                <div class="btn-group">
                  <button
                    class="btn btn-outline-primary btn-sm"
                    (click)="previousPage()"
                    [disabled]="currentPage === 1"
                  >
                    <i class="fas fa-chevron-left me-1"></i>
                    {{ "previous" | translate }}
                  </button>
                  <button
                    class="btn btn-outline-primary btn-sm"
                    (click)="nextPage()"
                    [disabled]="currentPage >= totalPages()"
                  >
                    {{ "next" | translate }}
                    <i class="fas fa-chevron-right ms-1"></i>
                  </button>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>