import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { PaginationMeta } from '@msfa-models/pagination-meta.model';
import { Report } from '@msfa-models/reports.model';

@Component({
  selector: 'msfa-reports',
  templateUrl: './reports.component.html',
  styleUrls: ['./reports.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ReportsComponent {
  @Input() reports: Report[];
  @Input() paginationMeta: PaginationMeta;
  @Output() paginated = new EventEmitter<number>();

  columnHeaders: { label: string; key: keyof Report }[] = [
    {
      label: 'Typ av rapport',
      key: 'type'
    },
    {
      label: 'Inskickad datum',
      key: 'sendDate',
    },
    {
      label: 'Status',
      key: 'status',
    }
  ];

  get currentPage(): number {
    return this.paginationMeta.page;
  }

  get totalPage(): number {
    return this.paginationMeta?.totalPage;
  }

  get count(): number {
    return this.paginationMeta.count;
  }

  get currentResultStart(): number {
    return (this.currentPage - 1) * this.paginationMeta.limit + 1;
  }

  get currentResultEnd(): number {
    const end = this.currentResultStart + this.paginationMeta.limit - 1;
    return end < this.count ? end : this.count;
  }

  setNewPage(page: number): void {
    this.paginated.emit(page);
  }
}
