import { consumerSaleInvoicesNamedRoutes } from '@/domains/consumer/modules/saleInvoices/constants';
import { AbstractList } from '@/shared/abstractClasses';
import { PageDataListComponent } from '@/shared/components/pageDataList/page-data-list.component';
import { Component, inject, TemplateRef, ViewChild } from '@angular/core';
import { Router, RouterLink } from '@angular/router';
import { Badge } from 'primeng/badge';
import { ButtonDirective } from 'primeng/button';
import { IStatisticsSaleInvoicesResponse } from '../../../models';
import { CustomerStatisticsService } from '../../../services/main.service';

@Component({
  selector: 'consumer-statistics-latest-Invoices',
  templateUrl: './latest-Invoices.component.html',
  imports: [PageDataListComponent, ButtonDirective, RouterLink, Badge],
})
export class ConsumerStatisticsLatestInvoicesComponent extends AbstractList<IStatisticsSaleInvoicesResponse> {
  @ViewChild('status', { static: true }) status!: TemplateRef<any>;

  private readonly service = inject(CustomerStatisticsService);
  private readonly router = inject(Router);

  readonly invoicesPageRoute = consumerSaleInvoicesNamedRoutes.saleInvoices.meta.pagePath!();

  override setColumns(): void {
    this.columns = [
      { field: 'code', header: 'کد پیگیری' },
      { field: 'total_amount', header: 'قیمت نهایی', type: 'price' },
      {
        field: 'pos',
        header: 'پایانه',
        customDataModel(item) {
          return `${item.pos.name} - ${item.pos.complex.name} - ${item.pos.complex.business_activity.name}`;
        },
      },
      {
        field: 'consumer_account',
        header: 'ایجاد شده توسط',
        type: 'nested',
        nestedOption: { path: 'consumer_account.account.username' },
      },
      { field: 'status', header: 'وضعیت صدور', customDataModel: this.status },
    ];
  }

  override getDataRequest() {
    return this.service.getInvoices();
  }

  toSinglePage(invoice: IStatisticsSaleInvoicesResponse) {
    this.router.navigateByUrl(
      consumerSaleInvoicesNamedRoutes.saleInvoice.meta.pagePath!(invoice.id),
    );
  }
}
