import { ToastService } from '@/core/services/toast.service';
import { IPosHeldOrderResponse } from '@/modules/pos/models';
import { ConfirmationDialogService } from '@/shared/components/confirmationDialog/confirmation-dialog.service';
import { PriceMaskDirective } from '@/shared/directives';
import { Component, EventEmitter, Input, Output, signal } from '@angular/core';
import { ButtonDirective } from 'primeng/button';
import { PosService } from '../../services/main.service';

@Component({
  selector: 'app-held-order-item',
  templateUrl: './held-order-item.component.html',
  imports: [PriceMaskDirective, ButtonDirective],
})
export class HeldOrderItemComponent {
  @Input() heldOrder!: IPosHeldOrderResponse;
  @Input() posId!: number;

  @Output() onCancel = new EventEmitter<number>();
  @Output() onLoad = new EventEmitter<number>();

  constructor(
    private readonly posService: PosService,
    private readonly confirmationService: ConfirmationDialogService,
    private readonly toastService: ToastService,
  ) {}

  actionLoading = signal<boolean>(false);

  cancelHeldOrder(heldOrderId: number) {
    this.confirmationService.confirm({
      message: `آیا از لغو سفارش شماره‌ی #${this.heldOrder.orderNumber} اطمینان دارید؟`,
      header: 'لغو سفارش موقت',
      acceptLabel: 'بله',
      rejectLabel: 'خیر',
      accept: () => {
        this.actionLoading.set(true);
        // this.posService.cancelOrder(this.posId, heldOrderId).subscribe({
        //   next: () => {
        //     this.onCancel.emit(heldOrderId);
        //     this.actionLoading.set(false);
        //     this.toastService.success({
        //       text: `سفارش شماره‌ی #${this.heldOrder.orderNumber} با موفقیت لغو شد.`,
        //     });
        //   },
        //   error: () => {
        //     this.actionLoading.set(false);
        //   },
        // });
      },
      reject: () => {},
    });
  }

  loadHeldOrder(heldOrderId: number) {
    this.onLoad.emit(heldOrderId);
  }
}
