import {Component, OnInit} from '@angular/core';
import {FormBuilder} from '@angular/forms';
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-agent-category',
  templateUrl: './conf-agent-category.component.html',
  styleUrls: ['./conf-agent-category.component.scss']
})
export class ConfAgentCategoryComponent extends FrameDetailComponent implements OnInit {
  categoryArray = [];

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

  }

  ngOnInit() {
  }

  initDefautEntity() {
    for (const s of this.ResponseArray) {
      if (s.agent_id !== undefined) {
        s.ac_status = 'Active';
      } else {
        s.ac_status = 'InActive';
      }
      this.categoryArray.push(s);
    }
  }


  saveCategory() {
    const array = [];
    for (const s of this.categoryArray) {
      if (s.ac_status === 'Active') {
        s.agent_id = this.currentEntity.agent_id;
        array.push(s);
      }
    }
    let msg = `该考点确定开放这【${array.length}】个大类吗？`;
    if (array.length === 0) {
      msg = '确定要取消所有大类吗？';
    }

    MsgService.confirmAlert(msg, () => {
      AjaxService.request('conf/agent/category/save.htm', {
        agent_id: this.currentEntity.agent_id,
        json_value: JSON.stringify(array)
      }, () => {
        MsgService.successToastr('考点大类配置操作完成！');
        this.closeWindow();
      });
    });


  }
}
