import {formatDate} from '@angular/common';
import {Component, OnInit, ChangeDetectionStrategy, EventEmitter, Input, Output, ViewChild} from '@angular/core';
import {MatDialog} from '@angular/material/dialog';
import {filter, switchMap, tap} from 'rxjs/operators';
import {BaseList} from 'src/app/core/base/base-list';
import {
    DailyRegisterPaymentTotalService
} from 'src/app/core/service/report/daily-register/daily-register-payment-total.service';
import {DailyRegisterPaymentService} from 'src/app/core/service/report/daily-register/daily-register-payment.service';
import {
    PeriodRegisterPaymentOnlineService
} from 'src/app/core/service/report/period-register/period-register-payment-online.service';
import {StudentPaymentService} from 'src/app/core/service/student-payment/student-payment.service';
import {SweetalertService} from 'src/app/core/service/sweetalert/sweetalert';
import {DialogComponent} from '../dialog/dialog.component';

@Component({
    selector: 'app-list',
    templateUrl: './list.component.html',
    styleUrls: ['./list.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListComponent extends BaseList implements OnInit {
    paymentselect: [] = [];
    @Input() paymentList: any = [];
    @Output() confirm = new EventEmitter()
    @Output() search = new EventEmitter()
    query = {
        date: null,
        is_cash: false,
        is_payin: false,
        is_qrcode: false,
        is_credit_card: false,
    };

    constructor(
        private studentPaymentSV: StudentPaymentService,
        private swSV: SweetalertService,
        private dailyRegisterSV: DailyRegisterPaymentService,
        private dailyRegisterTotalSV: DailyRegisterPaymentTotalService,
        private dialog: MatDialog,
        private periodRegisterSV: PeriodRegisterPaymentOnlineService,
    ) {
        super()
    }

    ngOnInit(): void {

    }

    ngOnChanges() {
        this.paymentList = this.updateMatTable(this.paymentList ? this.paymentList : [])
    }

    total() {
        const total = this.paymentList.data.reduce((prev, cur) => {
            return prev + +cur.testing_fee
        }, 0)
        return total
    }

    onSearch() {
        if (this.paymentselect.length != 0) {
            this.query.is_cash = false;
            this.query.is_payin = false;
            this.query.is_qrcode = false;
            this.query.is_credit_card = false;
            this.paymentselect.forEach(option => {
                switch (option) {
                    case 'CASH':
                        this.query.is_cash = true;
                        break;
                    case 'PAYIN':
                        this.query.is_payin = true;
                        break;
                    case 'QRCODE':
                        this.query.is_qrcode = true;
                        break;
                    case 'CREDIT-CARD':
                        this.query.is_credit_card = true;
                        break;
                }
            });
        }
        this.search.emit(this.query)
    }

    reportEtest(type: string) {
        const obj = {
            type: type,
            date: formatDate(this.paymentList.data[0].payment_datetime, 'yyyy-MM-dd', 'en-US')
        }
        const dialogRef = this.dialog.open(DialogComponent, {
            width: '500px',
            data: obj,
        });

        dialogRef.afterClosed().pipe(
            filter(x => x.obj),
            switchMap(x => {
                if (type == '101') {
                    console.log(x.obj)
                    if (x.obj.is_testing_information_report) {
                        return window.open(this.dailyRegisterSV.etest101InformationOnline(x.obj))
                    } else {
                        return window.open(this.dailyRegisterSV.etest101Online(x.obj))
                    }
                }
                if (type == '102') {
                    if (x.obj.is_testing_information_report) {
                        return window.open(this.dailyRegisterTotalSV.etest102InformationOnline(x.obj))
                    } else {
                        return window.open(this.dailyRegisterTotalSV.etest102Online(x.obj))
                    }
                }
                if (type == '103') {
                    if (x.obj.is_testing_information_report) {
                        return window.open(this.periodRegisterSV.etest103InformationOnline(x.obj))
                    } else {
                        return window.open(this.periodRegisterSV.etest103Online(x.obj))
                    }
                }
            })
        ).subscribe()

        // const data = {...this.paymentList.data[0]}
        // const date = formatDate(data.payment_datetime,'yyyy-MM-dd','en-US')
        // const query = {
        //   date : date
        // }
        // if(type == '101'){
        //   window.open(this.dailyRegisterSV.etest101InformationOnline(query))
        // }else{
        //   window.open(this.dailyRegisterTotalSV.etest102InformationOnline(query))
        // }
    }

}
