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


  varietyArray = [];
  all_flag = 'InActive';

  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/aspect/list.htm', {aspect_id: this.currentEntity.aspect_id, all_flag: all_flag}, (array) => {
      for (const v of array) {
        if (v.aspect_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/aspect/variety/save.htm', {
        aspect_id: this.currentEntity.aspect_id,
        variety_ids: array
      }, () => {
        MsgService.successToastr('乐器/舞种配置成功。');
        this.closeWindow();
      });
    };
    let msg = `确定要为此专业方向配置【${array.length}】个乐器/舞种`;
    if (array.length === 0) {
      msg = '确定要取消该专业方向的所有乐器/舞种配置吗？';
    }
    MsgService.confirmAlert(msg, fn);
  }


}
