import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {CookieService} from 'ngx-cookie-service';
import {ExamJudgeRuleDetailComponent} from '../exam-judge-rule-detail/exam-judge-rule-detail.component';
import {ExamJudgeChooseComponent} from '../exam-judge-choose/exam-judge-choose.component';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';

@Component({
  selector: 'app-exam-judge-extract',
  templateUrl: './exam-judge-extract.component.html',
  styleUrls: ['./exam-judge-extract.component.scss']
})
export class ExamJudgeExtractComponent extends FramePageComponent implements OnInit {

  agent: any;
  dateArray;
  currentMap;
  currentRoom;

  constructor(private router: Router, private cookies: CookieService, modalService: BsModalService) {
    super(modalService);
    this.modalComponent = ExamJudgeRuleDetailComponent;
    // 获取缓存中的微信编号
    const agent_id = this.cookies.get('CurrentAgentId');
    if (agent_id !== undefined && agent_id.length > 0) {
      // 去获取
      AjaxService.requestEntity('layout/agent/find.htm', {ly_agent_id: agent_id}, (entity) => {
        this.agent = entity;
        AjaxService.requestArray('exam/judge/extract/date/list.htm', {ly_agent_id: agent_id}, (array) => {
          this.dateArray = array;
        });
      });
    } else {
      this.router.navigate(['/art/layout/agent/page']);
    }
  }

  changeDate() {
    // this.currentMap = undefined;
    AjaxService.requestMap('exam/judge/extract/date.htm', this.agent, (map) => {
      this.currentMap = map;
    });
  }

  onModalClose() {
  }

  deleteJudge(judge) {
    AjaxService.request('exam/judge/extract/judge/delete.htm', judge, () => {
      MsgService.successToastr('评委已从考场移除');
      this.changeDate();
    });
  }

  changeChief(judge) {
    AjaxService.request('exam/judge/extract/judge/chief.htm', judge, () => {
      this.changeDate();
    });
  }

  openChooseJudge(room) {
    this.currentRoom = room;
    this.onModalClose = () => {
      this.changeDate();
    };
    this.showComponentModal(ExamJudgeChooseComponent, {
      ResponseMap: {'ly_agent_id': this.agent.ly_agent_id}, afterChooseJudge: this.afterChooseJudge.bind(this)
    });
  }

  afterChooseJudge(judge) {
    AjaxService.request('exam/judge/extract/judge/add.htm', {
      ly_room_id: this.currentRoom.ly_room_id,
      judge_id: judge.judge_id,
      room_chief: judge.room_chief
    }, () => {
      MsgService.successToastr(`评委【${judge.judge_name}】已经加入考场`);
    });
  }

  optrExtract(msg, optr, group?) {
    let data = {
      ly_agent_id: this.agent.ly_agent_id,
      exam_date: this.agent.exam_date
    };
    if (group !== undefined) {
      data['ly_group_id'] = group.ly_group_id;
    }
    MsgService.confirmAlert(msg, () => {
      AjaxService.request(`exam/judge/extract/judge/${optr}.htm`, data, () => {
        this.changeDate();
        MsgService.successToastr('操作完成，请检查系统处理结果！');
      }, {RequestWithLoading: true});
    });
  }


  ngOnInit() {

  }

}
