import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { LayoutInterviewDetailComponent } from '../layout-interview-detail/layout-interview-detail.component';
import { LayoutInterviewParamComponent } from '../layout-interview-param/layout-interview-param.component';
import { LayoutInterviewRoomComponent } from '../layout-interview-room/layout-interview-room.component';

@Component({
  selector: 'app-layout-interview',
  templateUrl: './layout-interview.component.html',
  styleUrls: ['./layout-interview.component.scss']
})
export class LayoutInterviewComponent extends FramePageComponent implements OnInit {

  schoolArr = [];
  batchArr = [];
  param = {
    school_id : '',
    batch_id : ''
  }
  showSingleGroup: any;
  searchText: any;

  constructor(modalService: BsModalService, private cdr : ChangeDetectorRef) {
    super(modalService);
    this.modalComponent = LayoutInterviewDetailComponent;
  }

  ngOnInit(): void {
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.param.school_id = this.schoolArr[0].school_id;
        this.requestBatch(this.schoolArr[0].school_id);
        this.requestGroup();
      }
    });
  }

  requestBatch(school_id) {
    this.AjaxService.requestArray('cf/school/batch/list.htm', {
      school_id : school_id
    }, (array) => {
      this.batchArr = array;
      this.cdr.detectChanges();
      if(this.batchArr && this.batchArr.length == 1) {
        this.param.batch_id = this.batchArr[0].batch_id;
      }
      this.requestGroup();
    });
  }

  onModalClose() {
    this.ajax.reload();
  }

  // 获取分组
  requestGroup() {
    this.ajax.reload({school_id: this.param.school_id, batch_id: this.param.batch_id});
  }

  //合并分组
  makeGroup() {
    const array = [];
    for(const v of this.ajax.getMap()['SingleArr']) {
      if(v.checked == true) {
        array.push(v);
      }
    }
    if(array.length < 2) {
      this.MsgService.errorToastr('分组合并至少需要选择2个科目！');
      return;
    }
    this.showComponentModal(LayoutInterviewDetailComponent, {
      ResponseArray: array
    })
  }
  //分组参数
  showGroupParam(group) {
    this.showComponentModal(LayoutInterviewParamComponent, {
      entityUrl: 'ly/group/param/find.htm',
      entityParam: {ly_group_id : group.ly_group_id}
    });
  }

  //设置分组考场
  showGroupRoom(group) {
    this.showComponentModal(LayoutInterviewRoomComponent, {
      ResponseEntity : group
    });
  }

}
