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


  provinceArray = [];

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

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

  requestProvinceArray() {
    AjaxService.requestArray('conf/agent/province/list.htm', {agent_id: this.currentEntity.agent_id}, (array) => {
      for (const v of array) {
        if (v.agent_id !== undefined) {
          v.checked = true;
        }
      }
      this.provinceArray = array;
    });
  }


  saveProvince() {
    const array = [];
    for (const v of this.provinceArray) {
      if (v.checked === true) {
        array.push(v.province_id);
      }
    }
    const fn = () => {
      AjaxService.requestEntity('conf/agent/province/save.htm', {
        agent_id: this.currentEntity.agent_id,
        province_ids: array
      }, () => {
        MsgService.successToastr('区域性考点服务省份设置成功。');
        this.closeWindow();
      });
    };
    let msg = `确定要为此考点配置【${array.length}】个省份吗`;
    if (array.length === 0) {
      msg = '确定要取消该考点的服务省份吗？';
    }
    MsgService.confirmAlert(msg, fn);
  }


}
