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-std-reg-tracks',
  templateUrl: './std-reg-tracks.component.html',
  styleUrls: ['./std-reg-tracks.component.scss']
})
export class StdRegTracksComponent extends FrameDetailComponent implements OnInit {

  tracksList = [];

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

  ngOnInit() {
  }

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

  //查找曲目
  listTracks() {
    let variety_id = '';
    for(const tracks of this.ResponseMap['TracksArray']) {
      if(tracks.variety_id) {
        variety_id = tracks.variety_id;
      break;
      }
    }
    AjaxService.requestArray('conf/tracks/list.htm',{variety_id: variety_id},(array) => {
      this.tracksList = array;
    })
  }



  saveTracks() {

    if(this.currentEntity.business_note === undefined || $.trim(this.currentEntity.business_note.length).length ===0 ) {
      MsgService.errorToastr('请输入曲目说明信息');
      return;
    }
    if (this.ResponseMap['TracksArray'] && this.ResponseMap['TracksArray'].length > 0) {
      // 判断是否填写了曲目
      for (const t of this.ResponseMap['TracksArray']) {
        if (!t.tracks_name || $.trim(t.tracks_name).length == 0) {
          MsgService.errorToastr('请填写曲目');
          return;
        }
      }
    }
    
    MsgService.confirmAlert('确认要修改考生曲目吗，请谨慎进行此操作！', () => {
      AjaxService.request('std/reg/enrol/tracks/save.htm', {
        enrol_id: this.currentEntity.enrol_id,
        tracks_json:JSON.stringify(this.ResponseMap['TracksArray']),
        business_note: this.currentEntity.business_note,
        aspect_batch: this.currentEntity.std_batch
      }, () => {
        MsgService.successAlert('专业修改成功！');
        this.closeWindow();
      });
    });
  }
}
