import {Component, OnInit, ChangeDetectionStrategy, Input, Output, EventEmitter} from '@angular/core';
import {tap} from 'rxjs/operators';
import {TestingFieldService} from 'src/app/core/service/settings/testing-field.service';
import {TestingCenterService} from 'src/app/core/service/TestingCenter/testing-center.service';
import {createQueryStringFromObject} from 'src/app/shared/util/func';
import {formatDate} from '@angular/common';

@Component({
    selector: 'app-check-candidate-list-search',
    templateUrl: './check-candidate-list-search.component.html',
    styleUrls: ['./check-candidate-list-search.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class CheckCandidateListSearchComponent implements OnInit {
    @Input() testingInfoData: any;
    @Input() subjectList: any = [];
    @Output() add = new EventEmitter();
    @Output() cleardata = new EventEmitter();
    @Output() search = new EventEmitter();
    statusMock = [{id: 1, name: 'ใช้งาน'}, {id: 2, name: 'ยกเลิก'}];
    sectionsFake = [
        {
            "status_id": 1,
            "testing_center_period_uid": null,
            "period_id": 1,
            "is_check": false,
            "start_time": "09:00"
        },
        {
            "status_id": 1,
            "testing_center_period_uid": null,
            "period_id": 2,
            "is_check": false,
            "start_time": "12:00"
        },
        {
            "status_id": 1,
            "testing_center_period_uid": null,
            "period_id": 3,
            "is_check": false,
            "start_time": "15:00"
        },
        {
            "status_id": 1,
            "testing_center_period_uid": null,
            "period_id": 4,
            "is_check": false,
            "start_time": "18:00"
        }
    ];
    query = {
        subject_uid: null,
        student_code: null,
        has_exam: null,
        start_testing_date: null,
        end_testing_date: null,
        period_id: null,
        testing_information_uid: null
    };

    constructor() {
    }

    ngOnInit(): void {

    }

    onClear() {
        this.cleardata.emit();
    }

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

    onSearch() {
        this.query.testing_information_uid = this.testingInfoData.testing_information_uid
        const periodid = [];
        let textfree = '';
        if (this.query.start_testing_date != null) {
            this.query.start_testing_date = formatDate(this.query.start_testing_date, 'yyyy-MM-dd', 'en-US');
        }
        if (this.query.end_testing_date != null) {
            this.query.end_testing_date = formatDate(this.query.end_testing_date, 'yyyy-MM-dd', 'en-US');
        }
        if (this.query.period_id != null) {
            periodid.push(this.query.period_id);
            for (let i = 0; i < periodid[0].length; i++) {
                console.log(periodid[0][i]);
                textfree += `&period_id=${periodid[0][i]}`;
            }
            this.query.period_id = null;
        }
        const query = createQueryStringFromObject(this.query);
        // this.search.emit({value: `${query}${textfree}`, have_exan: this.query.have_exan === 'true' ? true : null});
        // this.search(emit)
        this.search.emit(`${query}${textfree}`);
    }

    displaySubject(uid) {
        if (uid && this.subjectList) return this.subjectList.find(x => x.subject_uid === uid)?.subject_code
    }

    clear() {
        this.query = {
            subject_uid: null,
            student_code: null,
            has_exam: null,
            start_testing_date: null,
            end_testing_date: null,
            period_id: null,
            testing_information_uid: this.testingInfoData.testing_information_uid
        };
        const query = createQueryStringFromObject(this.query);
        this.search.emit(query);
    }

    checkDatesearch() {
        if (this.query.start_testing_date != null && this.query.end_testing_date === null) {
            this.query.end_testing_date = this.query.start_testing_date;
        }
    }

}
