import { Component, OnInit } from '@angular/core';
import { FrameRequest } from '../../../../shared/frame/frame-request';

@Component({
  selector: 'app-std-enrol-exam',
  templateUrl: './std-enrol-exam.component.html',
  styleUrls: ['./std-enrol-exam.component.scss']
})
export class StdEnrolExamComponent implements OnInit {

  query = {
    exam_time_id: "",
    query : ""
  };

  STATIS_ENROL_URL = "std/stdEnrol/statisEnrolExam.htm";
  LATEST_TIME_URL = "cf/cfOperateTime/getLatest.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";

  stdEnrol = [];
  sum: number = 0;
  personSum: number = 0;
  netSum : number = 0;
  localSum : number = 0;

  constructor() { }

  ngOnInit() {
    FrameRequest.requestArray(this.LATEST_TIME_URL, { oper_type: 2 }, (array) => {
      if (array && array.length > 0) {
        this.query.exam_time_id = array[0].exam_time_id;
        this.search();
      }
    });
  }

  search() {
    FrameRequest.requestArray(this.STATIS_ENROL_URL, {
      exam_time_id: (this.query.exam_time_id === undefined
        || this.query.exam_time_id == 'null' || this.query.exam_time_id === null
      ) ? "" : this.query.exam_time_id,
      query: this.query.query
    }, (array) => {
      this.stdEnrol = array;
      this.sumStd();
    });
  }

  sumStd() {
    this.sum = 0;
    this.personSum = 0;
    this.localSum = 0;
    this.netSum = 0;
    for (var i = 0; i < this.stdEnrol.length; i++) {
      var stdEnrol = this.stdEnrol[i];
      this.sum += stdEnrol.nums;
      this.personSum += stdEnrol.numt;
      this.localSum += stdEnrol.localnum;
      this.netSum += stdEnrol.netnum;
    }
  }

}
