import {Component, OnInit, ChangeDetectionStrategy, Input, OnChanges, Output, EventEmitter} from '@angular/core';
import {TestingRegisterService} from '../../../../../../core/service/testing-register/testing-register.service';
import {TestingRecieptService} from '../../../../../../core/service/testing-register/testing-reciept.service';
import {BaseList} from '../../../../../../core/base/base-list';
import {
    ReceiptByPaymentCodeService
} from '../../../../../../core/service/report/receipt-by-payment-code/receipt-by-payment-code.service';
import {AppTokenService} from '../../../../../../core/service/security/app-token.service';
import {concatMap, filter, tap} from "rxjs/operators";
import {SweetalertService} from "../../../../../../core/service/sweetalert/sweetalert";
import {
    TestingRegisterEnrollmentService
} from "../../../../../../core/service/enrollment-api/testing-register-enrollment.service";
import {X} from '@angular/cdk/keycodes';

@Component({
    selector: 'app-manage-receipt-list',
    templateUrl: './manage-receipt-list.component.html',
    styleUrls: ['./manage-receipt-list.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ManageReceiptListComponent extends BaseList implements OnInit, OnChanges {
    @Input() recieptsList: any = [];
    @Output() OnCancelPay = new EventEmitter();

    user;
    column = ['1', '2', '3', '5', '4', '6'];
    test = ['1'];

    constructor(
        private reportPaymentcode: ReceiptByPaymentCodeService,
        private appTokenSV: AppTokenService,
        private swSV: SweetalertService,
    ) {
        super();
    }

    ngOnInit(): void {
        this.user = this.appTokenSV.getToken();

    }

    ngOnChanges() {
        console.log(this.recieptsList);
        this.recieptsList = this.updateMatTable(this.recieptsList ? this.recieptsList : []);
    }

    print(paymentCode, type: any) {
        // console.log(paymentCode);
        if (paymentCode !== null) {
            this.reportPaymentcode.getPdfReportReceiptForAll(paymentCode).subscribe(res => {
                const fileURL = URL.createObjectURL(res);
                window.open(fileURL, '_blank');
            });
        } else {
            this.swSV.error('กรุณากดรับเงินใหม่');
        }
    }

    printReal(paymentCode, type: any) {
        // console.log(paymentCode);
        if (paymentCode !== null) {
            this.reportPaymentcode.getPdfReportRealUat(paymentCode).subscribe(res => {
                const fileURL = URL.createObjectURL(res);
                window.open(fileURL, '_blank');
            });
        } else {
            this.swSV.error('กรุณากดรับเงินใหม่');
        }
    }

    printCopy(paymentCode, type: any) {
        // console.log(paymentCode);
        if (paymentCode !== null) {
            this.reportPaymentcode.getPdfReportCopy(paymentCode).subscribe(res => {
                const fileURL = URL.createObjectURL(res);
                window.open(fileURL, '_blank');
            });
        } else {
            this.swSV.error('กรุณากดรับเงินใหม่');
        }
    }

    printCopyRealUat(paymentCode, type: any) {
        // console.log(paymentCode);
        if (paymentCode !== null) {
            this.reportPaymentcode.getPdfReportCopyRealUat(paymentCode).subscribe(res => {
                const fileURL = URL.createObjectURL(res);
                window.open(fileURL, '_blank');
            });
        } else {
            this.swSV.error('กรุณากดรับเงินใหม่');
        }
    }

    cancelPay(registerUid) {
        this.OnCancelPay.emit(registerUid);
    }

    isDueDate(data) {
        let current_date = new Date()
        let payment_date = new Date(data.payment_datetime)
        current_date.setHours(0, 0, 0, 0);
        payment_date.setHours(0, 0, 0, 0);
        if (current_date.getTime() === payment_date.getTime()) {
            if (data.is_cancel) {
                return false
            }
            return true
        } else {
            return false
        }

    }


}
