import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import { FrameService } from '../../../../core/service/frame.service';

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

  std: any;
  agentArray = [];
  dateArray = [];
  ssArray = [];
  groupArray = [];
  roomArray=[];
  judgeArray = [];
  currentStep:any;
  stdInfo : any;
  echoArray = [];

  constructor(modalService: BsModalService) {
    super(modalService);
    this.goInit();
   }

  ngOnInit() {
  }

  goInit() {
    this.std = {};
    this.agentArray = [];
    this.dateArray = [];
    this.groupArray = [];
    this.roomArray = [];
    this.judgeArray = [];
    // 获取所有考点
    AjaxService.requestArray('exam/checkin/init/agent.htm', {}, (array) => {
      this.agentArray = array;
      if (this.agentArray.length === 1) {
        this.std.agent_id = this.agentArray[0].ly_agent_id;
      }
      this.currentStep = 'Init';
    });
  }


  // searchStd() {
  //   if(!this.std.cert_id) {
  //     MsgService.errorToastr('请填写考生的证件号码！');
  //     return ;
  //   }
  //   AjaxService.requestMap('exam/ops/score/get.htm' , this.std, (map) => {
  //     if(map) {
  //       this.ssArray = map['ssArray'];
  //     }
  //   });
  // }
  //清理进场信息
  clearStdRoom() {
    MsgService.confirmAlert('确定要清除序号为：' + this.std.real_exam_echo +'考生进场信息吗？一旦清除，评委需要重新登录，请慎重！', () => {
      AjaxService.request('exam/ops/room/clear.htm', this.std, () => {
        MsgService.successToastr('清除考生考场信息成功！');
        this.ssArray = [];
        this.std.cert_id = '';
      });
    });
  }

 // 获取考试日期
 requestDateArray() {
   if(this.std.agent_id) {
     AjaxService.requestArray('exam/checkin/init/date.htm', this.std, (array) => {
       if (FrameService.FrameOptr.optr_id !== 0) {
         for (const d of array) {
           if (d.ly_room_id === 0) {
             const s = [];
             s.push(d);
             this.dateArray = s;
             break;
           }
         }
         if(this.dateArray ===undefined || this.dateArray.length ==0) {
           this.dateArray = array;
         }
       } else {
         this.dateArray = array;
       }
       if (this.dateArray.length === 1) {
         this.std.exam_date = this.dateArray[0].exam_date;
         this.requestGroupArray();
       }
     });

   }
  }

// 获取分组
requestGroupArray() {
  AjaxService.requestArray('exam/checkin/init/group.htm', this.std, (array) => {
    this.groupArray = array;
    if(this.groupArray && this.groupArray.length == 1) {
      this.requestRoomArray();
    }
  });
} 

 //获取考场
 requestRoomArray() {
  AjaxService.requestArray('exam/interview/room/batch/list.htm', this.std, (array) => {
    this.roomArray = array;
    for(const room of this.roomArray) {
      room.room_name = '第【'+room.room_seq+'】考场';
    }
    if(this.roomArray.length === 1) {
      this.std.room_id = this.roomArray[0].ly_room_id;
      this.requestRoomEcho();
    }
  })
}

//获取评委
// requestJudge() {
//   AjaxService.requestArray('exam/judge/list/judge.htm', this.std, (array) => {
//     this.judgeArray = array;
//   });
// }

clearJudgeScore() {
  MsgService.confirmAlert('确定要清理评委打的分数吗？，一旦清理，评委需要重新给考生打分，请谨慎操作！', () => {
    AjaxService.request('exam/ops/score/clear.htm', this.std, () => {
      MsgService.successToastr('评委分数清理成功！');
      this.dateArray = [];
      this.groupArray = [];
      this.roomArray = [];
      this.judgeArray = [];
      this.std = {};
    });
  });
}

requestRoomEcho() {
  AjaxService.requestArray('exam/ops/list/inroom/seq.htm', {room_id : this.std.room_id}, (array) => {
    this.echoArray = array;
  })
}

  searchStd() {
    if(!this.std.real_exam_echo || !this.std.room_id) return;
    AjaxService.requestEntity('exam/ops/get/std/info.htm', this.std, (entity) => {
      this.stdInfo = entity;
    });
  }

}
