import { Component, OnInit } from '@angular/core';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FramePageComponent } from '../../../../frame/core/page/frame.page';
import { ModalService } from '../../../../../core/service/modal.service';

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

  schoolArr = [];
  subjectArr = [];
  score: any;
  subject: any;

  constructor(modalService: BsModalService) {
    super(modalService);
   }

  ngOnInit(): void {
    this.score = {};
    this.subject = {};
    this.requestSchool();
  }

  //学校
  requestSchool() {
    this.AjaxService.requestArray('cf/school/list.htm',{}, (array) => {
      this.schoolArr = array;
      if(this.schoolArr && this.schoolArr.length == 1) {
        this.score.school_id = this.schoolArr[0].school_id;
      }
    });
  }

  //科目
  requestSubject() {
    this.AjaxService.requestArray('cf/subject/list/by/school.htm', {school_id: this.score.school_id}, (array) => {
      this.subjectArr = array;
      for(const subject of this.subjectArr) {
        subject.subject_full_name = subject.subject_code +"|" + subject.subject_name;
      }
      if(this.subjectArr && this.subjectArr.length == 1) {
        this.score.subject_id = this.subjectArr[0].subject_id;
        this.requestSubjectScore();
      }
    });
  }

   //加载所有的考生科目分数
  requestSubjectScore() {
    for(const sb of this.subjectArr) {
      if(sb.subject_id === this.score.subject_id) {
        this.subject = sb;
      }
    }
    this.pager.reload({subject_id: this.score.subject_id});
  }

  //重算科目成绩-内部计算
  recalSubjectScore(subject_id?) {
    this.MsgService.confirmAlert('确定要重新计算考生的科目成绩吗？使用场景：科目分合成后，合分规则有变化。', () => {
      this.AjaxService.requestEntity('sc/std/subject/recal.htm', {subject_id: subject_id, school_id: this.score.school_id}, (entity) => {
        ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在重算科目成绩，请稍等'});
        this.pager.reload({subject_id: this.score.subject_id});
      }, {RequestWithLoading: true});
    });
  }

  //重新生成科目成绩-删掉重算
  generateSubjectScore(subject_id?) {
    this.MsgService.confirmAlert('确定要重新生成考生的科目成绩吗？使用场景：评委的原始分数有改动，会删掉已合成的科目分，重新从评委原始打分中合成。', () => {
      this.AjaxService.requestEntity('sc/std/subject/generate.htm', {subject_id: subject_id,school_id: this.score.school_id}, (entity) => {
        ModalService.showThreadWindow(this.bsService, entity, {threadTitle: '正在重新生成考生的科目成绩，请稍等'});
        this.pager.reload({subject_id: this.score.subject_id });
      }, {RequestWithLoading: true});
    });
  }
  
  //下载成绩
  downloadScore() {
    this.AjaxService.requestEntity('frame/report/download/thread.htm', {
      subject_id: this.subject ? this.subject.subject_id : undefined,
      report_name: 'ScoreOriginalSubject',
      report_title: '考生科目成绩-'  +(this.subject ? ('【'+this.subject.subject_name+'】') : '')
    }, (thread) => {
      ModalService.showThreadWindow(this.bsService, thread);
    });
  }

  //推送数据到艺考系统
  pushDataToArt(subject_id?, subject_name?) {
    let msg = '';
    if(!subject_id) {
      msg = '确定要推送该学校下所有的科目成绩到艺考系统吗？';
    } else {
      msg = '确定要推送【'+ subject_name +'】的科目成绩到艺考系统吗？';
    }
    this.MsgService.confirmAlert(msg, () => {
      this.AjaxService.requestEntity('push/score/data.htm', this.score, (entity) => {
        if(entity.success) {
          this.AjaxService.request('art/data/push/get/result.htm', {req_id: entity, school_id: this.score.school_id}, (ajax) => {
            ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '数据正在执行!'});
          });
        } else {
          this.MsgService.errorAlert('推送失败，请确保评分系统和艺考系统的学校代码一致。如果一致，还出现推送失败，请联系开发人员！');
        }
      });
    });
  }

}
