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

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

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

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

  stdEnrol = [];
  sum: 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_MAJOR_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;
    for (var i = 0; i < this.stdEnrol.length; i++) {
      var stdEnrol = this.stdEnrol[i];
      this.sum += stdEnrol.num;
    }
  }

}
