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

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

  query = {
    exam_time_id: ""
  };

  sumPerson = {
    all_person: 0,
    all_num : 0,
    fee_Person : 0,
    fee_num : 0,
    net_fee_person: 0,
    net_fee_num : 0,
    local_person: 0,
    local_num: 0,
    new_person: 0
  }

  STD_ENROL_STA_URL = "std/stdEnrol/statisStdEnrol.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  LATEST_TIME_URL = "cf/cfOperateTime/getLatest.htm";
  ENROL_NUM_URL = "std/stdEnrol/statisEnrolNum.htm";

  stdEnrol = [];
  enrol: any;

  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();
    });
    FrameRequest.requestEntity(this.ENROL_NUM_URL, {exam_time_id : this.query.exam_time_id} , (entity) => {
      this.enrol = entity;
    });

  }

  search() {
    FrameRequest.requestArray(this.STD_ENROL_STA_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
    }, (array) => {
      this.stdEnrol = array;
      this.sumStd();
    });

    FrameRequest.requestEntity(this.ENROL_NUM_URL, {exam_time_id : this.query.exam_time_id} , (entity) => {
      this.enrol = entity;
    });
    
  }

  sumStd() {
    this.initData();
    for (var i = 0; i < this.stdEnrol.length; i++) {
        var stdEnrol = this.stdEnrol[i];
        this.sumPerson.all_person += parseInt(stdEnrol.person_num == undefined ?  "0" : stdEnrol.person_num) ;
        this.sumPerson.all_num += parseInt(stdEnrol.num == undefined ?  "0" : stdEnrol.num) ;
        this.sumPerson.fee_Person += parseInt(stdEnrol.have_fee_person == undefined ?  "0" : stdEnrol.have_fee_person) ;
        this.sumPerson.fee_num += parseInt(stdEnrol.have_fee_num == undefined ?  "0" : stdEnrol.have_fee_num) ;
        this.sumPerson.net_fee_person += parseInt(stdEnrol.net_fee_person == undefined ?  "0" : stdEnrol.net_fee_person) ;
        this.sumPerson.net_fee_num += parseInt(stdEnrol.person_p_num == undefined ?  "0" : stdEnrol.person_p_num) ;
        this.sumPerson.local_person += parseInt(stdEnrol.local_fee_person == undefined ?  "0" : stdEnrol.local_fee_person) ;
        this.sumPerson.local_num += parseInt(stdEnrol.college_p_num == undefined ?  "0" : stdEnrol.college_p_num) ;
        this.sumPerson.new_person += parseInt(stdEnrol.new_std_num == undefined ?  "0" : stdEnrol.new_std_num) ;
    }
  }

  initData() {
    this.sumPerson = {
      all_person: 0,
      all_num : 0,
      fee_Person : 0,
      fee_num : 0,
      net_fee_person: 0,
      net_fee_num : 0,
      local_person: 0,
      local_num: 0,
      new_person: 0
    }
  }

}
