import { ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { ScoreResultListDetailComponent } from '../score-result-list-detail/score-result-list-detail.component';

@Component({
  selector: 'app-score-result-list',
  templateUrl: './score-result-list.component.html',
  styleUrls: ['./score-result-list.component.scss']
})
export class ScoreResultListComponent extends FramePageComponent implements OnInit {

  score: any;
  schoolArr = [];
  batchArr = [];
  dateArr = [];
  roomArr = [];
  scoreArr = [];


  constructor(modalService: BsModalService, private cdr : ChangeDetectorRef) { 
    super(modalService);
  }

  ngOnInit(): void {
    this.score = {};
    this.requestSchool();
  }

   //学校
   requestSchool() {
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArr = array;
      this.cdr.detectChanges();
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.score.school_id = this.schoolArr[0].school_id;
        this.requestBatch();
      }
    });
  }

  //批次
  requestBatch() {
    if(!this.score.school_id) return;
    this.AjaxService.requestArray('cf/batch/list.htm',{school_id: this.score.school_id}, (array) => {
      this.batchArr = array;
      this.cdr.detectChanges();
      if(this.batchArr && this.batchArr.length == 1) {
        this.score.batch_id = this.batchArr[0].batch_id;
        this.requestDate();
      }
    });
  }

  //考试日期
  requestDate() {
    if(!this.score.batch_id) return;
    this.AjaxService.requestArray('ly/checkin/date/list.htm', { batch_id : this.score.batch_id }, (array) => {
      this.dateArr = array;
      this.cdr.detectChanges();
      if(this.dateArr && this.dateArr.length == 1) {
        this.score.exam_date = this.dateArr[0].exam_date;
        this.requestRoom();
      }
    });
  }

  //考场
  requestRoom() {
    if(!this.score.exam_date || !this.score.batch_id) return;
    this.AjaxService.requestArray('ly/room/interview/list/bydate.htm', this.score, (array) => {
      this.roomArr = array;
      for(const room of this.roomArr) {
        room.room_addr = room.group.group_name +'第【' + room.room_seq+'】考场';
      }
      this.cdr.detectChanges();
      if(this.roomArr && this.roomArr.length == 1) {
        this.score.ly_room_id = this.roomArr[0].ly_room_id;
      }
    });
  }


  //某个考生的所有评委打分
  requestScore() {
    if(!this.score.real_exam_echo) {
      this.MsgService.errorToastr('请输入考生进场序号！');
      return ;
    }
    this.AjaxService.requestArray('ex/judge/score/list/by/echo.htm', this.score, (array) => {
      this.scoreArr = array;
    });
  }

  showEditModal(v) {
    this.onModalClose = () => {
      this.requestScore();
    };
    this.showComponentModal(ScoreResultListDetailComponent, {ResponseEntity: v}, {class: 'modal-6'});
  }

}
