import {Component, OnInit} from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import { FrameService } from '../../../../core/service/frame.service';
import { FuncService } from 'src/app/core/service/func.service';

@Component({
  selector: 'app-score-aspect',
  templateUrl: './score-aspect.component.html',
  styleUrls: ['./score-aspect.component.scss']
})
export class ScoreAspectComponent extends FramePageComponent implements OnInit {

  aspectArray = [];
  subjectArray = [];
  score: any;
  aspect;

  batchNameArray: any;

  constructor(modalService: BsModalService) {
    super(modalService);
    this.batchNameArray = FrameService.getFrameDictArray('ExamBatchName');
    this.score = {batch_id: '1'};
    this.changeBatch();
  }

  ngOnInit() {
  }


  //获取专业
  changeBatch() {
    AjaxService.requestArray('score/aspect/list.htm', this.score, (array) => {
      for (const as of array) {
        as.aspect_value = as.aspect_id + ' ' + as.aspect_name;
        if (as.std_total !== undefined) {
          as.aspect_value =  as.aspect_id + ' ' +as.aspect_name + `[${as.qualified_total}/${as.std_total}]`;
        }
      }
      this.aspectArray = array;
    });

  }

  changeAspect() {
    if (this.score.aspect_id === undefined)
      return;
    for (const s of this.aspectArray) {
      if (s.aspect_id === this.score.aspect_id) {
        this.aspect = s;
      }
    }

    AjaxService.requestArray('conf/aspect/subject/group.htm', this.score, (subjectArray) => {
      this.subjectArray = subjectArray;
      this.pager.reload(this.score);
    });
  }


  /********************
   * @todo 需要修改成线程的模式
   * @param subject_id
   */
  makeAspectScore(aspect_id ?) {
    const data = {};
    MsgService.confirmAlert('确定要重新统计成绩吗？', () => {
      AjaxService.request('score/aspect/make.htm', {aspect_id: aspect_id, batch_id: this.score.batch_id}, () => {
        this.pager.reload({aspect_id: this.score.aspect_id, batch_id: this.score.batch_id});
        MsgService.successToastr('合成成功！');
      }, {RequestWithLoading: true});
    });
  }
  /********************
   * 导出专业成绩
   */
  exportAspect(aspect_id ?) {
    if(!aspect_id) {
      MsgService.errorToastr('请选择专业！');
      return;
    }
    //武音专业分数导出
    if(FrameService.getParamValue('SchoolCode') == '11524' ) {
      FuncService.ajaxDownload('./score/aspect/export.htm', {aspect_id: aspect_id});
    } else {
      FuncService.ajaxDownload('./aspect/score/export.htm', {aspect_id: aspect_id, batch_id : this.score.batch_id});
    }
  }

  uploadCallback(ajax) {
    MsgService.successAlert('导入成功!');
  }

  //导出所有专业成绩
  exportAspectScore() {
    if(!this.score.batch_id) {
      this.MsgService.errorAlert('请选择批次！');
      return;
    }
    if(this.score.batch_id == 1) {
      FuncService.ajaxDownload('frame/report/download.htm', {report_name:'StdAspectScoreReport', report_title : '考生专业成绩', batch_id: this.score.batch_id});
    }
    if(this.score.batch_id == 2) {
      FuncService.ajaxDownload('frame/report/download.htm', {report_name:'StdAspectScoreFsReport', report_title : '考生专业成绩', batch_id: this.score.batch_id});
    }
    if(this.score.batch_id == 3) {
      FuncService.ajaxDownload('frame/report/download.htm', {report_name:'StdAspectScoreSsReport', report_title : '考生专业成绩', batch_id: this.score.batch_id});
    }
  }

}
