import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { TranslateModule } from '@ngx-translate/core';
import { ExcelExportService } from '../shared/services/excel-export.service';
import { TRANSACTION_LOGS_FILE_NAME } from '../utils/app.constants';
import { pageSizeOptions } from '../utils/app.constants';
import { NgSelectModule } from '@ng-select/ng-select';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { TableFilterPipe } from '../shared/pipes/table-filter.pipe';
import {TransactionLog} from "../models/user"
import { URIKey } from '../utils/uri-enums';
import { HttpParams } from '@angular/common/http';
import { HttpURIService } from '../app.http.uri.service';

@Component({
  selector: 'app-transaction-logs',
  templateUrl: './transaction-logs.component.html',
  imports: [CommonModule, TranslateModule, NgSelectModule, FormsModule, ReactiveFormsModule, TableFilterPipe ]
})
export class TransactionLogsComponent implements OnInit {
onPageSizeChange(arg0: number) {
throw new Error('Method not implemented.');
}
  currentPage: number = 1;
  totalCount: number = 0;
  renewalDataExpanded: boolean = true;
  pageSizeOptions = pageSizeOptions;
  itemsPerPage: number = 10;
  transactionLog: TransactionLog[] = [];
  isLoading = false;
  pagedItems: any[] = [];
  errorMessage: string = '';
  searchText: string = '';
  allItems: TransactionLog[] = [];
  transactionDataExpanded: boolean = true

  constructor(
    private excelExportService: ExcelExportService,
    private httpService: HttpURIService,
  ) {}


  ngOnInit(): void {
    this.loadTransactionLogs();   
  }
 loadTransactionLogs(): void {
  this.isLoading = true;
  const params = new HttpParams();
  this.httpService
    .requestGET<any>(URIKey.TRANSACTION_LOGS, params)
    .subscribe({
      next: (res) => {
        const logs = Array.isArray(res) ? res : res?.data;
        this.transactionLog = logs ?? [];
        this.allItems = [...this.transactionLog];
        this.totalCount = this.transactionLog.length;

        this.updatePagedItems();
        this.isLoading = false;
      },
      error: (err) => {
        console.error('Error fetching logging details data:', err);
        this.transactionLog = [];
        this.isLoading = false;
      }
    });
}

  toggleTableCard(): void {
  this.transactionDataExpanded = !this.transactionDataExpanded;
}
  itemsPerPageChanged(): void {
    this.currentPage = 1;
    this.updatePagedItems();
  }
  onSearch(value: string): void {
  this.searchText = value;
}

   totalPages(): number {
    return Math.ceil(this.allItems.length / this.itemsPerPage);
  }

  previousPage(): void {
    if (this.currentPage > 1) {
      this.currentPage--;
      this.updatePagedItems();
    }
  }
  updatePagedItems(): void {
    const startIndex = (this.currentPage - 1) * this.itemsPerPage;
    const endIndex = startIndex + this.itemsPerPage;
    this.pagedItems = this.allItems.slice(startIndex, endIndex);
  }

  nextPage(): void {
    if (this.currentPage < this.totalPages()) {
      this.currentPage++;
      this.updatePagedItems();
    }
  }
  
  exportDataInExcel() {
    this.excelExportService.exportExcel(this.transactionLog, TRANSACTION_LOGS_FILE_NAME)
  }
}