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


@Component({
  selector: 'app-conf-variety-tracks',
  templateUrl: './conf-variety-tracks.component.html',
  styleUrls: ['./conf-variety-tracks.component.scss']
})
export class ConfVarietyTracksComponent extends FrameDetailComponent implements OnInit {

  tracksMap = {array_1: [], array_2: [], array_3: []};

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

  ngOnInit() {
  }

  initDefautEntity() {
    for (const t of this.ResponseArray) {
      if (t.enrol_batch === 1) {
        this.tracksMap.array_1.push(t);
      } else if (t.enrol_batch === 2) {
        this.tracksMap.array_2.push(t);
      } else if (t.enrol_batch === 3) {
        this.tracksMap.array_3.push(t);
      }
    }
  }

  changeConfig(batch, $event) {
    // if ($event.key !== 'Enter') {
    //   return;
    // }
    if (this.currentEntity['enrol_tracks_' + batch] === undefined || this.currentEntity['enrol_tracks_' + batch] < 0) {
      this.currentEntity['enrol_tracks_' + batch] = 0;
    }
    const array = [];
    if (this.tracksMap['array_' + batch].length === this.currentEntity['enrol_tracks_' + batch]) {
      // 没有改变配置
      return;
    }
    if (this.tracksMap['array_' + batch].length > this.currentEntity['enrol_tracks_' + batch]) {
      // 比原来少了，直接截取
      this.tracksMap['array_' + batch] = this.tracksMap['array_' + batch].slice(0, this.currentEntity['enrol_tracks_' + batch]);
    } else {
      // 比原来多了
      const total = this.currentEntity['enrol_tracks_' + batch] - this.tracksMap['array_' + batch].length;
      for (let index = 1; index <= total; index++) {
        this.tracksMap['array_' + batch].push({tracks_order: this.tracksMap['array_' + batch].length + 1, 'require_exam_status': 'InActive', enrol_batch: batch});
      }
    }
    // this.tracksMap['array_' + batch]
  }

  saveTracks() {
    for (let k = 1; k <= FrameService.getParamValue('ExamBatchs'); k++) {
      if (this.currentEntity['enrol_tracks_' + k] === undefined || this.currentEntity['enrol_tracks_' + k] < 0) {
        this.currentEntity['enrol_tracks_' + k] = 0;
      }
      if (this.currentEntity['exam_tracks_' + k] === undefined || this.currentEntity['exam_tracks_' + k] < 0) {
        this.currentEntity['exam_tracks_' + k] = 0;
      }
      const batch_name = FrameService.getFrameDictText('ExamBatchName', k + '');
      if (this.currentEntity['exam_tracks_' + k] > this.currentEntity['enrol_tracks_' + k]) {
        MsgService.errorToastr(`【${batch_name}】实际考试的曲目数量不能超过报考曲目数量！`);
        return;
      }
      if (this.currentEntity['enrol_tracks_' + k] > 0) {
        if (this.currentEntity['exam_tracks_' + k] < 1) {
          MsgService.errorToastr(`【${batch_name}】配置了包括曲目，实际考试的曲目不能小于1！`);
          return;

        }
      }
      // 判断是否必填
      for (const s of this.tracksMap['array_' + k]) {
        if (s.tracks_enrol_type === undefined || s.tracks_enrol_type === '') {
          MsgService.errorToastr(`【${batch_name}】请选择曲目填报类型！`);
          return;
        }
        if (s.tracks_enrol_type === 'Appoint' && (s.tracks_name === undefined || s.tracks_name.trim().length === 0)) {
          MsgService.errorToastr(`【${batch_name}】配置了指定曲目，但实际未填写曲目信息！`);
          return;
        }
      }

    }

    const fn = () => {
      const array = [];
      for (const a in this.tracksMap) {
        for (const s of this.tracksMap[a]) {
          array.push(s);
        }
      }
      this.currentEntity.json_value = JSON.stringify(array);
      AjaxService.request('conf/variety/tracks/save.htm', this.currentEntity, () => {
        MsgService.successToastr('曲目配置成功！');
        this.closeWindow();
      });
    };
    MsgService.confirmAlert('确定要提交曲目配置信息吗？', fn);
  }
}
