import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { PrintReceiptsService, ReturnProcessStore } from '@isa/oms/data-access';
import { injectTabId } from '@isa/core/tabs';
import { ReturnTaskListComponent } from '@isa/oms/shared/task-list';
import { ReturnReviewHeaderComponent } from './return-review-header/return-review-header.component';

@Component({
  selector: 'oms-feature-return-review',
  templateUrl: './return-review.component.html',
  styleUrl: './return-review.component.scss',
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [ReturnTaskListComponent, ReturnReviewHeaderComponent],
})
export class ReturnReviewComponent {
  #printReceiptsService = inject(PrintReceiptsService);
  #returnProcessStore = inject(ReturnProcessStore);
  processId = injectTabId();

  async printReceipt() {
    const processId = this.processId();
    if (processId) {
      const receiptIds = this.#returnProcessStore
        .entities()
        .filter((p) => p.processId === processId && p.returnReceipt?.id)
        .map((p) => p.returnReceipt!.id);

      await this.#printReceiptsService.printReturnReceipts({
        returnReceiptIds: receiptIds,
      });
    }
  }
}
