import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import {StdRegChooseComponent} from '../std-reg-choose/std-reg-choose.component';

@Component({
  selector: 'app-std-reg-search',
  templateUrl: './std-reg-search.component.html',
  styleUrls: ['./std-reg-search.component.scss']
})
export class StdRegSearchComponent implements OnInit {

  @Input()
  width: number;
  @Input()
  height: number;
  @Input()
  font: number;

  @Output()
  chooseStudent = new EventEmitter<any>();

  searchText;
  bsOptions;

  constructor(private modalService: BsModalService) {
    this.bsOptions = {
      animated: true,
      keyboard: false,
      backdrop: true,
      ignoreBackdropClick: true,
      initialState: {},
      class: 'modal-75p'
    };
  }

  ngOnInit() {
  }

  searchStd() {
    if (this.searchText === undefined || $.trim(this.searchText).length === 0) {
    }
    AjaxService.requestArray('std/reg/list.htm', {query: this.searchText}, (array) => {
      if (array.length === 1) {
        this.chooseStudent.emit(array[0]);
      } else if (array.length > 1) {
        const modal = Object.assign({}, this.bsOptions);
        modal.initialState = {'StudentArray': array, 'AfterChooseStdudent': this.afterChooseStd.bind(this),'AfterCloseWindow':this.afterCloseWindow.bind(this)};
        this.modalService.show(StdRegChooseComponent, modal);
      } else {
        MsgService.warningToastr('没有找到符合条件的考生！');
      }
    });

  }

  afterChooseStd(std) {
    this.chooseStudent.emit(std);
  };

  afterCloseWindow() {
    this.searchText = undefined;
  }




}
