import {Component, OnInit} from '@angular/core';
import {FormBuilder, Validators} from '@angular/forms';
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-std-later-enrol',
  templateUrl: './std-later-enrol.component.html',
  styleUrls: ['./std-later-enrol.component.scss']
})
export class StdLaterEnrolComponent extends FrameDetailComponent implements OnInit {

  currentEnrol;
  aspectArray;
  agentArray;
  aspectVarietyArray;
  subjectMap;
  tracksMap;
  subjectGroupArr;
  tracksArray;

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.currentEnrol = {};
  }

  ngOnInit() {
  }

  initDefautEntity() {
    this.currentEnrol.std_id = this.currentEntity.std_id;
    if (this.ResponseArray.length === 1) {
      this.currentEnrol.category_id = this.ResponseArray[0].category_id;
      this.changeCategory();
    }
  }

  changeCategory() {
    if (this.currentEnrol.category_id === undefined)
      return;
    this.currentEnrol = {
      std_id: this.currentEnrol.std_id,
      category_id: this.currentEnrol.category_id
    };
    // 科目为空
    this.subjectMap = undefined;
    this.tracksMap = undefined;
    this.agentArray = undefined;
    this.aspectVarietyArray = [];

    AjaxService.requestArray('enrol/config/aspect/list.htm', this.currentEnrol, (array) => {
      this.aspectArray = array;
      if (this.aspectArray.length === 1) {
        this.currentEnrol.aspect_id = this.aspectArray[0].aspect_id;
        this.changeAspect();
      }
    }, {RequestWithLoading: true});

  }

  changeAspect() {
    if (this.currentEnrol.aspect_id === undefined || this.currentEnrol.aspect_id === null)
      return;
    // 品种设置为空
    this.currentEnrol.variety_id = undefined;
    // 科目为空
    this.subjectMap = undefined;
    this.tracksMap = undefined;
    this.agentArray = undefined;
    for (const ap of this.aspectArray) {
      if (ap.aspect_id === this.currentEnrol.aspect_id) {
        this.currentEnrol = Object.assign(this.currentEnrol, ap);
        break;
      }
    }
    AjaxService.requestArray('enrol/config/aspect/variety/list.htm', this.currentEnrol, (array) => {
      this.aspectVarietyArray = array;
      this.changeAspectVariety();
      AjaxService.requestArray('enrol/config/agent/list.htm', {
        category_id: this.currentEnrol.category_id,
        aspect_id: this.currentEnrol.aspect_id,
        std_id: this.currentEnrol.std_id
      }, (vArray) => {
        this.agentArray = vArray;
        if (this.agentArray.length === 1) {
          this.currentEnrol.agent_id = vArray[0].agent_id;
        }
      }, {RequestWithLoading: true});
    }, {RequestWithLoading: true});
  }

  changeAspectVariety() {
    if (this.aspectVarietyArray.length > 0 && (this.currentEnrol.variety_id === undefined || this.currentEnrol.variety_id === null)) {
      return;
    }
    AjaxService.requestMap('enrol/config/aspect/subject/list.htm', this.currentEnrol, (map) => {
      this.subjectMap = map;
      this.getSubjectGroup(map);
      this.queryTracks();
    }, {RequestWithLoading: true});
  }


  //针对有分组，选一组的情况
  getSubjectGroup(subjectMap) {
    for(const s of subjectMap['BatchSubjectArray_1']) {
      if(s.subject_group !== undefined) {
         this.subjectGroupArr = subjectMap['SubjectGroup_'+s.subject_group];
      }
    }
  }



  // 设置主项
  setMaster(sb) {
    for (const s of this.subjectMap['BatchSubjectArray_1']) {
      if (s.master_group !== undefined) {
        if (s.subject_id !== undefined) {
          if (s.subject_id === sb.sujbect_id) {
            s.master_flag = 'Active';
          } else {
            s.master_flag = undefined;
          }
        }

      }
    }
  }

  queryTracks() {
    const array = [];
    this.tracksMap = undefined;
    for (const s of this.subjectMap['BatchSubjectArray_1']) {
      if (s.subject_id === null || s.subject_id === undefined)
        break;
      if (this.subjectMap['SubjectVariety_' + s.subject_id] !== undefined && this.subjectMap['SubjectVariety_' + s.subject_id].length > 0) {
        // 有品种
        if (s.variety_id === null || s.variety_id === undefined)
          continue;
        array.push(s.variety_id);
      }
    }
    if (array.length == 0)
      return;
    AjaxService.requestMap('enrol/config/variety/tracks/list.htm', {
      'variety_ids[]': array,
      enrol_batch: 1,
      std_id: this.currentEntity.std_id
    }, (map) => {
      if (map.VarietyArray !== undefined) {
        // 需要曲目
        this.tracksMap = map;
        for(const v of map['VarietyArray']) {
          AjaxService.requestArray('conf/tracks/list.htm', {variety_id: v.variety_id}, (array) => {
              this.tracksArray = array;
          });

        }
       
      }
    });
  }

  /***************
   * todo 做必要判断
   */
  saveEnrol() {
    debugger
    const tracksArray = [];
    if (this.tracksMap !== undefined) {
      for (const v of this.tracksMap['VarietyArray']) {
        for (const t of this.tracksMap['VarietyTracks_' + v.variety_id]) {


          //设置品种
          for(const subject of this.subjectMap['BatchSubjectArray_1']) {
            if(subject.variety_id == v.variety_id) {
              subject.variety_name = v.variety_name;
            }
          }

          //设置曲目信息(需要选择曲目的情况)
          if(t.tracks_id != undefined) {
            for(const track of this.tracksArray) {
              if(track.tracks_id == t.tracks_id) {
                t.tracks_name= track.tracks_name;
                t.tracks_code= track.tracks_code;
                t.tracks_author= track.tracks_author;
                break;
              }
            }
          }

          // 判断曲目是否填写
          if(!t.tracks_name || $.trim(t.tracks_name).length == 0) {
            MsgService.errorAlert('请填写曲目！');
            return ;
          }
          tracksArray.push(t);
        }
      }
    }

    //设置品种名称
    for(const subject of this.subjectMap['BatchSubjectArray_1']) {
      if(subject.variety_id !== undefined) {
        for(const s of this.subjectMap['SubjectVariety_'+ subject.subject_id]) {
          if(s.variety_id === subject.variety_id) {
            subject.variety_name = s.variety_name;
          }
        }
      }
    }


    MsgService.confirmAlert('确定要对考生进行该专业补报吗？', () => {

      const entity = {
        std_id: this.currentEnrol.std_id,
        aspect_id: this.currentEnrol.aspect_id,
        agent_id: this.currentEnrol.agent_id,
        variety_id: this.currentEnrol.variety_id,
        variety_name: this.currentEnrol.variety_name,
        subject_json: JSON.stringify(this.subjectMap['BatchSubjectArray_1']),
        tracks_json: JSON.stringify(tracksArray),
      };
      AjaxService.request('enrol/std/enrol/add.htm', entity, () => {
        MsgService.successAlert('后台专业报考成功！');
        this.closeWindow();

      }, {RequestWithLoading: true});

    });
  }
  
}
