import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from 'src/app/routes/frame/core/page/frame.page';

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

  draw:any;
  currentStep = 'init';
  agentArr = []; //考点
  groupArr = []; //分组
  currentStd:any;
  text = '考题抽签';

  constructor(modalService: BsModalService) {
    super(modalService);
   }

  ngOnInit(): void {
    this.draw = {};
    this.requestAgent();
  }

  //考点
  requestAgent() {
    this.AjaxService.requestArray('exam/checkin/init/agent.htm', {}, (array) => {
      this.agentArr = array;
      if (this.agentArr.length === 1) {
        this.draw.ly_agent_id = this.agentArr[0].ly_agent_id;
        this.requestGroup();
      }
    
    });
  }

  //分组
  requestGroup() {
    this.AjaxService.requestArray('layout/group/all.htm',{agent_id: this.draw.ly_agent_id},(array) => {
      this.groupArr = array;
      if(this.groupArr && this.groupArr.length == 1) {
        this.currentStep = 'draw';
      }
    });
  }

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


searchStd() {
  if (this.draw.cert_id === undefined || $.trim(this.draw.cert_id).length === 0)
      return;
   this.currentStd = {};
   this.AjaxService.requestMap('exam/interview/drawresult.htm',this.draw, (map) => {
     this.currentStd = map.stdReg;
     this.draw.topic_num=map.topic_num;
     this.draw.create_time=map.create_time;
     this.draw.file_name = map.file_name;
   }, {requestWithLoading:true}); 
}

  //抽取考题
  drawTopic() {
	if (this.draw.cert_id === undefined || $.trim(this.draw.cert_id).length === 0)
      return;
    this.MsgService.showLoading();
    this.text = '正在抽题';
    setTimeout(() => {
      this.AjaxService.requestMap('exam/interview/drawtopic.htm',this.draw, (map) => {
       this.MsgService.hideLoading();
        this.currentStd = map.stdReg;
        this.draw.topic_num=map.topic_num;
        this.draw.create_time=map.create_time;
        this.draw.file_name = map.file_name;
      }, {requestWithLoading:true});
    }, 500);
  }

  //下载
  downloadTopic() {
    if(this.draw.file_name) {
      window.open(this.draw.file_name);
    } else{
      this.MsgService.errorToastr('未设置考题下载地址，请先设置！');
    }
  }

}
