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

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

  schoolArr = [];
  batchArr = [];
  dateArr = [];
  roomArr = []; //考场
  recordArr = []; //考生分数记录
  detailArr = []; //考生详细打分
  record: any;
  currentStep: any;

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

  ngOnInit(): void {
    this.record = {};
    this.currentStep = 'main';
    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.record.school_id = this.schoolArr[0].school_id;
        this.requestBatch();
      }
    });
  }

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

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

  //考场
  requestRoom() {
    if(!this.record.exam_date || !this.record.batch_id) return;
    this.AjaxService.requestArray('ly/room/interview/list/bydate.htm', this.record, (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.record.ly_room_id = this.roomArr[0].ly_room_id;
      }
    });
  }


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

  //某个考生，某个评委的打分记录
  requestRecordDetail(v) {
    this.AjaxService.requestArray('ex/judge/score/record/list/detail.htm', v, (array) => {
      this.detailArr = array;
      this.currentStep = 'detail';
    });
  }

  goStep(step?) {
    this.currentStep = step;
  }

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

  deleteScoreRecord(v) {
    this.MsgService.confirmAlert('确定要删除该分数吗？', () => {
      this.AjaxService.requestEntity('ex/judge/score/record/delete.htm', v, (entity) => {
        this.MsgService.successAlert('删除成功，请提醒评委重新登录！');
      });
    });
  }

}
