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


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

  private intervalId: any;
  judgescore: any;
  agentArray = [];
  dateArray = [];
  roomArray = [];
  chooseRoomArray = [];
  all = [];
  currentStep: any;


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

  ngOnInit() {

  }

  goInit() {
    clearTimeout(this.intervalId);
    this.agentArray = [];
    this.dateArray = [];
    this.roomArray = [];
    this.judgescore = {start_time: '06:00', end_time: '23:00'};

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


  // 获取考试日期
  requestDateArray() {
    AjaxService.requestArray('exam/checkin/init/date.htm', this.judgescore, (array) => {
      this.dateArray = [];
      // 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.judgescore.exam_date = this.dateArray[0].exam_date;
        this.requestRoomArray();
      }
    });
  }

  // 获取分组
  requestRoomArray() {
    AjaxService.requestArray('exam/interview/room/list.htm', this.judgescore, (array) => {
      this.roomArray = array;

    });
  }

  checkAllGroup() {
    for (const s of this.roomArray) {
      s.checked = this.judgescore.checked;
    }
  }

  viewJudgeScore() {
    if (this.judgescore.start_time === undefined || this.judgescore.end_time === undefined) {
      MsgService.errorToastr('请输入开考时间段！');
      return;
    }
    const array = [];
    for (const s of this.roomArray) {
      if (s.checked === true) {
        array.push(s.ly_room_id);
      }
    }
    if (array.length === 0) {
      MsgService.errorToastr('请选择需要查看分数的考场！');
      return;
    }
    this.judgescore.room_ids = array;
    AjaxService.requestMap('exam/judge/grade/judge/stat.htm', this.judgescore, (map) => {
      this.currentStep = 'Scoreing';
      this.chooseRoomArray = map['roomArray'];
      for (let i = 0; i < this.chooseRoomArray.length; i++) {
        let room = this.chooseRoomArray[i];
        this.setStatData(i, map['scoreArray' + room.ly_room_id]);
      }
    }, {RequestWithLoading: true});
  }


  setStatData(index: any, scoreArray: any) {
    var data = [];
    var series = [];
    var xAxisData = [];
    var ticket = [];
    let examSeq = {};
    for (const score of scoreArray) {
      ticket[score.i02 - 1] = score.s01;
      if (score.i03) {
        // 修正可能跳号的问题  2021-01-13 西音
        if (examSeq['x_' + score.i03] === undefined) {
          xAxisData[xAxisData.length] = score.i03;
          examSeq['x_' + score.i03] = score.i03;
        }
      }
      if (data[score.i02 - 1] == undefined)
        data[score.i02 - 1] = new Array();
      data[score.i02 - 1].push(score.s09);
    }
    for (var k = 0; k < data.length; k++) {
      series[k] = {
        name: ticket[k],
        type: 'line',
        data: data[k],
        animationDelay: function (idx) {
          return idx * 10;
        }
      }
    }
    this.all[index] = {
      tooltip: {
        trigger: 'axis',
        axisPointer: { // 坐标轴指示器，坐标轴触发有效
          type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
        }
      },
      toolbox: {
        feature: {
          dataView: {
            show: false,
            readOnly: false
          },
          magicType: {
            show: true,
            type: ['line', 'bar']
          },
          saveAsImage: {
            show: false
          }
        }
      },
      legend: {
        data: ticket,
        align: 'left'
      },
      grid: {
        left: '1%',
        right: '2%',
        bottom: '1%',
        containLabel: true
      },
      xAxis: {
        data: xAxisData,
        silent: false,
        splitLine: {
          show: false
        }
      },
      yAxis: {},
      series: series,
      animationEasing: 'elasticOut',
      animationDelayUpdate: function (idx) {
        return idx * 5;
      }
    };

  }

  onChartInit(chart) {
  }


}
