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

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

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

  STATIS_ENROL_URL = "std/stdEnrol/statisEnrolCourse.htm";
  LATEST_TIME_URL = "cf/cfOperateTime/getLatest.htm";
  YEAR_CODE_URL = "pl/plExamTime/list.htm";
  EXPORT_URL = "std/stdEnrol/courseExport.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_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;
    }
  }

  export() {
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.EXPORT_URL, {}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("报考课程统计导出成功");
    });
  }

}
