import {Component, OnInit} from '@angular/core';
import {FrameEditComponent} from '../../../../frame/core/FrameEditComponent';
import {BsModalRef} from 'ngx-bootstrap';
import {FormBuilder, Validators} from '@angular/forms';
import {AjaxService} from '../../../../../core/ajax/ajax.service';
import {MsgService} from '../../../../../core/msg/msg.service';

@Component({
  selector: 'app-std-reg-aspect',
  templateUrl: './std-reg-aspect.component.html',
  styleUrls: ['./std-reg-aspect.component.scss']
})
export class StdRegAspectComponent extends FrameEditComponent implements OnInit {

  constructor(bsModalRef: BsModalRef) {
    super(bsModalRef);
  }

  ngOnInit() {
  }

  initDefautEntity() {
    this.currentEntity = this.ResponseMap.StdEnrol;
    this.ResponseMap['VarietyArray'] = [];
  }

  changeAspect() {
    if (this.currentEntity.new_aspect_id === undefined || this.currentEntity.new_aspect_id === '') {
      return;
    }
    AjaxService.requestArray('enrol/config/aspect/variety/list.htm', {
      aspect_id: this.currentEntity.new_aspect_id,
      std_id: this.currentEntity.std_id
    }, (array) => {

      this.currentEntity.new_variety_id = null;
      this.ResponseMap['NewSubjectArray'] = undefined;
      this.ResponseMap['NewTracksArray'] = undefined;

      this.ResponseMap['VarietyArray'] = array;
      if (array.length < 2) {
        this.changeVariety();
      }
    });
  }

  changeVariety() {
    if (this.ResponseMap['VarietyArray'].length === 1) {
      this.currentEntity.new_variety_id = this.ResponseMap['VarietyArray'][0].variety_id;
    }
    if (this.ResponseMap['VarietyArray'].length > 0 && (this.currentEntity.new_variety_id === undefined || this.currentEntity.new_variety_id === null)) {
      return;
    }
    AjaxService.requestMap('enrol/config/aspect/subject/list.htm', {
      aspect_id: this.currentEntity.new_aspect_id,
      std_id: this.currentEntity.std_id,
      variety_id: this.currentEntity.new_variety_id,
      batch_id: 1
    }, (map) => {
      const varietyArray = new Array();
      for (const sb of map['BatchSubjectArray_1']) {
        if (map[`SubjectVariety_${sb.subject_id}`] !== undefined && map[`SubjectVariety_${sb.subject_id}`].length > 0) {
          sb.varietyArray = map[`SubjectVariety_${sb.subject_id}`];
          if (map[`SubjectVariety_${sb.subject_id}`].length === 1) {
            sb.variety_id = map[`SubjectVariety_${sb.subject_id}`][0].variety_id;
            sb.variety_name = map[`SubjectVariety_${sb.subject_id}`][0].variety_name;
            varietyArray.push(map[`SubjectVariety_${sb.subject_id}`][0].variety_id);
          }
        }
      }
      this.ResponseMap['NewSubjectArray'] = map['BatchSubjectArray_1'];
      if (varietyArray.length > 0) {
        AjaxService.requestMap('enrol/config/variety/tracks/list.htm', {
          std_id: this.currentEntity.std_id,
          'variety_ids[]': varietyArray,
          enrol_batch: 1
        }, (map) => {
          // 临时处理，只有1个品种的情况
          this.ResponseMap['NewTracksArray'] = map['VarietyTracks_' + varietyArray[0]];
          // console.log(map);
        });
      }
    });
  }


  saveAspect() {
    if (this.currentEntity.new_aspect_id === undefined || this.currentEntity.new_aspect_id === null) {
      return;
    }
    if (this.currentEntity.business_note === undefined || $.trim(this.currentEntity.business_note.length).length === 0) {
      MsgService.errorToaster('请输入改专业/品种说明信息');
      return;
    }
    // 判断品种
    if (this.ResponseMap['VarietyArray'].length > 0 && (this.currentEntity.new_variety_id === null || this.currentEntity.new_variety_id === undefined)) {
      MsgService.errorToaster('请选择乐器/舞种');
      return;
    }
    if (this.ResponseMap['NewTracksArray'] && this.ResponseMap['NewTracksArray'].length > 0) {
      // 判断是否填写了曲目
      for (const t of this.ResponseMap['NewTracksArray']) {
        if (!t.tracks_name || $.trim(t.tracks_name).length == 0) {
          MsgService.errorToaster('请填写曲目');
          return;
        }
      }
    }

    MsgService.confirm('确认要修改考生专业吗，请谨慎进行此操作！', () => {
      AjaxService.request('std/reg/enrol/aspect/save.htm', {
        enrol_id: this.currentEntity.enrol_id,
        aspect_id: this.currentEntity.new_aspect_id,
        variety_id: this.currentEntity.new_variety_id,
        subject_json: JSON.stringify(this.ResponseMap['NewSubjectArray']),
        tracks_json: JSON.stringify(this.ResponseMap['NewTracksArray']),
        business_note: this.currentEntity.business_note
      }, () => {
        MsgService.successAlert('专业修改成功！');
        this.closeWindow();
      }, {
        RequestWithLoading: true
      });
    });
  }
}
