import { Component, OnInit, ChangeDetectionStrategy, EventEmitter, Output, ChangeDetectorRef } from '@angular/core';
import { tap } from 'rxjs/operators';
import { AcademicYearService } from 'src/app/core/service/settings/academic-year.service';
import { createQueryStringFromObject } from 'src/app/shared/util/func';

@Component({
    selector: 'app-testing-info-search',
    templateUrl: './testing-info-search.component.html',
    styleUrls: ['./testing-info-search.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class TestingInfoSearchComponent implements OnInit {
    @Output() add = new EventEmitter();
    @Output() search = new EventEmitter();
    academicYearList
    statusMock = [{id:1, name:'ใช้งาน'},{id:2, name:'ยกเลิก'}];
    query = {
        academic_year_code: null,
        testing_information_code: null,
        testing_information_name: null,
        academic_semester_code: null,
        // registration_type: 1
    };
    constructor(
        private acdmYearSV: AcademicYearService,
        private cdr: ChangeDetectorRef
    ) { }

    ngOnInit(): void {
        this.acdmYearSV.getAll().pipe(
            tap(x => this.academicYearList = x),
            tap(x => this.cdr.detectChanges())
        ).subscribe()
    }

    onAdd(){
        this.add.emit();
    }

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

    clear(){
        this.query = {
            academic_year_code: null,
            testing_information_code: null,
            testing_information_name: null,
            academic_semester_code: null,
            // registration_type: 1
        };
    }
}
