import {Component, OnInit, ChangeDetectionStrategy, Output, EventEmitter} from '@angular/core';
import {createQueryStringFromObject} from '../../../../../shared/util/func';
import {SweetalertService} from '../../../../../core/service/sweetalert/sweetalert';

@Component({
  selector: 'app-report-payment-and-app-by-center',
  templateUrl: './report-payment-and-app-by-center.component.html',
  styleUrls: ['./report-payment-and-app-by-center.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportPaymentAndAppByCenterComponent implements OnInit {

  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();

  query = {
    academic_year_uid: null,
    academic_semester_uid: null,
    branch_uid: null,
    testing_field_uid: null,
    testing_center_uid: null,
    subject_uid: null,
    from_date: null,
    to_date: null,
  };
  constructor(
    private swSV: SweetalertService,
  ) { }

  ngOnInit(): void {
  }

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

  onExcel(){
    let checkNULL = false;
    checkNULL = this.checkFunction(this.query);
    if (checkNULL === false){
      this.excel.emit(this.query);
    }
  }

  checkFunction(object){
    for (const key in object) {
      if (!object[key]) {
        this.swSV.errText('กรุณาใส่ข้อมูลให้ครบถ้วน');
        return true;
      }
    }
    return false;
  }

  clear(){
    this.query = {
      academic_year_uid: null,
      academic_semester_uid: null,
      branch_uid: null,
      testing_field_uid: null,
      testing_center_uid: null,
      subject_uid: null,
      from_date: null,
      to_date: null,
    };
  }

}
