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

@Component({
  selector: 'app-conf-agent-aspect',
  templateUrl: './conf-agent-aspect.component.html',
  styleUrls: ['./conf-agent-aspect.component.scss']
})
export class ConfAgentAspectComponent extends FrameEditComponent implements OnInit {
  aspectArray = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);

  }

  ngOnInit() {
  }

  initDefautEntity() {
    for (const s of this.ResponseArray) {
      if (s.agent_id !== undefined) {
        s.checked = true;
      } else {
        s.checked = false;
      }
      if (s.aa_start_date !== undefined) {
        s.aa_start_date = s.aa_start_date.replace(' 00:00:00', '');
      }
      this.aspectArray.push(s);
    }
  }


  saveAspect() {
    const array = [];
    for (const s of this.aspectArray) {
      if (s.checked === true) {
        s.agent_id = this.currentEntity.agent_id;
        array.push(s);
      }
    }
    let msg = `该考点确定开放这【${array.length}】个专业方向吗？`;
    if (array.length === 0) {
      msg = '确定要取消所有专业方向吗？';
    }

    MsgService.confirm(msg, () => {
      AjaxService.request('conf/agent/aspect/save.htm', {
        agent_id: this.currentEntity.agent_id,
        json_value: JSON.stringify(array)
      }, () => {
        MsgService.successToaster('考点专业方向配置操作完成！');
        this.closeWindow();
      });
    });


  }

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