import {
    Component,
    OnInit,
    ChangeDetectionStrategy,
    Output,
    EventEmitter,
    ChangeDetectorRef,
    Input
} from '@angular/core';
import {createQueryStringFromObject} from "../../../../../../shared/util/func";
import {AcademicYearService} from "../../../../../../core/service/settings/academic-year.service";
import {AcademicSemesterService} from "../../../../../../core/service/settings/academic-semester.service";
import {map, tap} from "rxjs/operators";
import {
    TestingInformationService
} from "../../../../../../core/service/testing-information/testing-information.service";

@Component({
    selector: 'app-manage-receipt-search',
    templateUrl: './manage-receipt-search.component.html',
    styleUrls: ['./manage-receipt-search.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class ManageReceiptSearchComponent implements OnInit {
    @Input() testingInformationList: any = [];

    query = {
        payment_code: null,
        counter_no: null,
        academic_semester_code: null,
        academic_year_code: null,
        testing_information_uid: null,
    };
    academicYearList = [];
    academicSemList: any = [];

    @Output() search = new EventEmitter();

    constructor(
        private AcademicYearSV: AcademicYearService,
        private AcademicSemSV: AcademicSemesterService,
        private testingInformationSV: TestingInformationService,
        private cdr: ChangeDetectorRef
    ) {
    }

    ngOnInit(): void {
        this.AcademicYearSV.getAll().subscribe(
            result => {
                this.academicYearList = result;
            }
        );
        this.AcademicSemSV.getAll().subscribe(
            result => {
                this.academicSemList = result;
            }
        );
        // this.testingInformationSV.getAll().pipe(
        //     tap(x => {
        //         this.testingInformationList = x.map((res: any) => {
        //             res.nameandcode = `${res.testing_information_code} ${res.testing_information_name} `;
        //             return res
        //         })
        //     }),
        //     tap(x => this.cdr.detectChanges())
        // ).subscribe();
    }

    onSearch() {
        const queryString = createQueryStringFromObject(this.query)
        this.search.emit(queryString);
    }

    display(uid) {
        if (uid && this.testingInformationList) return this.testingInformationList.find(x => x.testing_information_uid === uid)?.nameandcode
    }

    clear() {
        this.query = {
            payment_code: null,
            counter_no: null,
            academic_semester_code: null,
            academic_year_code: null,
            testing_information_uid: null,
        };
        this.onSearch();
    }

}
