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 { ModalService } from '../../../../core/service/modal.service';
import { FrameUpload } from '../../../../core/frame/upload/frame-upload/frame-upload';
import { FrameService } from '../../../../core/service/frame.service';

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

  subjectArray = [];
  score: any;
  subject: any;
  FrameUpload = FrameUpload;
  aspectArray = [];
  batchNameArray: any;

  constructor(modalService: BsModalService) {
    super(modalService);
    // AjaxService.requestArray('score/subject/list.htm', {}, (array) => {
    //   for (let sb of array) {
    //     sb.subject_text = (sb.subject_code ? sb.subject_code : sb.subject_id) + ' ' + (sb.subject_alias ? sb.subject_alias : sb.subject_name) + ' [共' + sb.subject_total + '考生]';
    //   }
    //   this.subjectArray = array;
    // });
    this.batchNameArray = FrameService.getFrameDictArray('ExamBatchName');
    AjaxService.requestArray('conf/aspect/all.htm', {}, (array) => {
      for (let sb of array) {
        sb.aspect_text = sb.aspect_id + ' ' + sb.aspect_name ;
      }
      this.aspectArray = array;
    });
    this.score = {};
  }

  ngOnInit() {
  }

  //请求成绩数据
  requestSubjectScore() {
    for (const sb of this.subjectArray) {
      if (sb.subject_id === this.score.subject_id)
        this.subject = sb;
    }
    this.pager.reload({subject_id: this.score.subject_id});
  }

  /********************
   * @todo 需要修改成线程的模式
   * @param subject_id
   */
  makeSubjectScore(subject_id?) {
    const data = {};
    MsgService.confirmAlert('确定要重新统计成绩吗？', () => {
      AjaxService.request('score/subject/make.htm', {subject_id: subject_id}, () => {
        this.pager.reload({subject_id: this.score.subject_id});
        MsgService.successToastr('合成成功！');
      }, {RequestWithLoading: true});
    });
  }

  makeInterviewSubjectScore(subject_id?) {
    const data = {};
    MsgService.confirmAlert('确定要重新合成无纸化分吗（如果无无纸化分不会进行处理）？', () => {
      AjaxService.requestEntity('score/subject/interview/make.htm', {
        subject_id: subject_id
      }, (thread) => {
        //this.showThreadRunning(thread);
        ModalService.showThreadWindow(this.bsService, thread);
      }, {RequestWithLoading: true});
    });
  }


  downloadScore() {
    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) => {
      // this.showThreadRunning(thread);
      ModalService.showThreadWindow(this.bsService, thread);
    }, {RequestWithLoading: true});
  }

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

  getSubject(aspect_id, batch_id) {
    console.log('aspect_id :' +aspect_id);
    AjaxService.requestArray('conf/aspect/subject/score/list.htm', {aspect_id : aspect_id, batch_id: batch_id}, (array) => {
      for (let sb of array) {
            sb.subject_text = (sb.subject_code ? sb.subject_code : sb.subject_id) + ' ' + (sb.subject_alias ? sb.subject_alias : sb.subject_name) ;
          }
      this.subjectArray = array;
    });
  }


}
