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

@Component({
  selector: 'app-score-curve-report',
  templateUrl: './score-curve-report.component.html',
  styleUrls: ['./score-curve-report.component.scss']
})
export class ScoreCurveReportComponent  extends FramePageComponent implements OnInit {
  
  score: any;
  schoolArr = [];
  batchArr = [];
  dateArr = [];
  roomArr = []; //所有的考场
  chooseRoomArr = []; //选择的考场
  all = [];
  currentStep:any;
  private intervalId: any;

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

  ngOnInit(): void {
  }
  
  goInit() {
    clearTimeout(this.intervalId);
    this.schoolArr = [];
    this.batchArr = [];
    this.dateArr = [];
    this.roomArr = [];
    this.score = {start_time: '06:00', end_time: '23:00'};
    this.AjaxService.requestArray('cf/school/list.htm', {}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.score.school_id = this.schoolArr[0].school_id;
        this.requestBatch();
      }
      this.currentStep = 'Init';
    });
  }

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

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

  //考场
  requestRoom() {
    this.AjaxService.requestArray('ly/room/interview/list.htm', this.score, (array) => {
      this.roomArr = array;
    });
  }

  //全选和反选
  checkAllRoom() {
    for(const room of this.roomArr) {
      room.checked = this.score.checked;
    }
  }

  //查看评分
  viewRoomScore() {
    if(!this.score.start_time  || !this.score.end_time) {
      this.MsgService.errorToastr('请输入开考时间段!');
      return;
    }
    const array = [];
    for(const s of this.roomArr) {
      if(s.checked == true) {
        array.push(s.ly_room_id);
      }
    }
    if(array.length == 0) {
      this.MsgService.errorToastr('请选择考场！');
      return;
    }
    this.score.room_ids = array;
    this.requestScoreStat();
    //50秒刷新
    this.intervalId = setInterval(this.requestScoreStat.bind(this), 50000);
  }

  requestScoreStat() {
    this.AjaxService.requestMap('ex/judge/score/report/stat/by/room.htm', this.score, (map) => {
      this.currentStep = 'Scoreing';
      this.chooseRoomArr = map['roomArr'];
      for(let i = 0; i<this.chooseRoomArr.length; i++) {
        //设置曲线显示的参数
        this.setStatData(i, map['scoreArr'+this.chooseRoomArr[i].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) {
        // 修正可能跳号的问题
        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.s03);
    }
    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) {
  }

}
