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-conf-subject-variety',
  templateUrl: './conf-subject-variety.component.html',
  styleUrls: ['./conf-subject-variety.component.scss']
})
export class ConfSubjectVarietyComponent  extends FrameDetailComponent implements OnInit {


  varietyArray = [];
  all_flag = 'Active';

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

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

  requestVarietyArray(all_flag: any) {
    this.all_flag = all_flag;
    AjaxService.requestArray('conf/variety/subject/list.htm', {subject_id: this.currentEntity.subject_id, all_flag: all_flag}, (array) => {
      for (const v of array) {
        if (v.subject_id !== undefined) {
          v.checked = true;
        }
      }
      this.varietyArray = array;
    });
  }

  selectAll(b) {
    for (const v of this.varietyArray) {
      v.checked = b;
    }
  }

  saveVariety() {
    const array = [];
    for (const v of this.varietyArray) {
      if (v.checked === true) {
        array.push(v.variety_id);
      }
    }
    const fn = () => {
      AjaxService.requestEntity('conf/subject/variety/save.htm', {
        subject_id: this.currentEntity.subject_id,
        variety_ids: array
      }, () => {
        MsgService.successToastr('乐器/舞种配置成功。');
        this.closeWindow();
      });
    };
    let msg = `确定要为此考试科目配置【${array.length}】个乐器/舞种`;
    if (array.length === 0) {
      msg = '确定要取消该考试科目的所有乐器/舞种配置吗？';
    }
    MsgService.confirmAlert(msg, fn);
  }



}
