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-receipt',
  templateUrl: './report-receipt.component.html',
  styleUrls: ['./report-receipt.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ReportReceiptComponent implements OnInit {

  @Output() search = new EventEmitter();
  @Output() excel = new EventEmitter();
  query = {
    student_code: null,
    branch_name_th: null,
    testing_field_name_th: null,
    testing_room_name_th: null,
    testing_datetime: null,
    period_id: 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 = {
      student_code: null,
      branch_name_th: null,
      testing_field_name_th: null,
      testing_room_name_th: null,
      testing_datetime: null,
      period_id: null,
    };
  }

}
