import { Component, OnInit } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../../core/service/ajax.service';
import { MsgService } from '../../../../../core/service/msg.service';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';
@Component({
  selector: 'app-exam-subject-score-config',
  templateUrl: './exam-subject-score-config.component.html',
  styleUrls: ['./exam-subject-score-config.component.scss']
})
export class ExamSubjectScoreConfigComponent extends FrameDetailComponent implements OnInit {

  subjectArray = [];
  chooseArray = [];
  choose_subject_id: any;
  constructor(bsModalRef: BsModalRef) { 
    super(bsModalRef);
  }

  ngOnInit() {
    setTimeout(() => {
      this.requestSubjectArray();
    }, 1);
  }

  requestSubjectArray() {
    AjaxService.requestArray('exam/subject/score/subject/list.htm', {}, (array) => {
      this.subjectArray = array;
      for(const subject of this.subjectArray) {
        subject.full_subject_name = subject.subject_code + '|' +subject.subject_name;
      }
    });

    AjaxService.requestArray('exam/subject/score/list.htm', this.ResponseEntity, (array) => {
      this.chooseArray = array;
    })
  }


  saveSubject() {
    if(this.chooseArray.length === 0) {
      MsgService.errorToastr('请添加至少一个科目！');
      return;
    }
    const array = [];
    for (const v of this.chooseArray) {
        array.push(v.subject_id);
    }
    this.ResponseEntity.subject_ids = array;
    MsgService.confirmAlert('确定要配置【'+array.length +'】个科目吗？', () => {
      AjaxService.request('exam/subject/score/save.htm' , this.ResponseEntity, () => {
        MsgService.successToastr('配置成功！');
        this.closeWindow();
      });
    })
  }

  addSubject() {
    //判断是否已经添加
    for(const choose of this.chooseArray) {
      if(choose.subject_id === this.choose_subject_id) {
        MsgService.warningToastr('该科目已经添加，请不要重复添加！');
        return ;
      }
    }
    for(const subject of this.subjectArray) {
      if(subject.subject_id === this.choose_subject_id) {
        this.chooseArray.push(subject);
      }
    }
  }

   //删除科目
   removeSubject(subject: any) {
    const index = this.chooseArray.findIndex(text => text.subject_id === subject.subject_id);
    this.chooseArray.splice(index, 1);
    // for(const choose of this.chooseArray) {
    //   if(choose.subject_id === subject.subject_id) {
    //     this.chooseArray.splice(1);
    //     break;
    //   }
    // }
  }

}
