import {Component, OnInit} from '@angular/core';
import {PlanGroupInitComponent} from '../plan-group-init/plan-group-init.component';
import {PlanGroupDetailComponent} from '../plan-group-detail/plan-group-detail.component';
import { BsModalService } from 'ngx-bootstrap/modal';
import { AjaxService } from '../../../../core/service/ajax.service';
import { MsgService } from '../../../../core/service/msg.service';
import { FramePageComponent } from '../../../frame/core/page/frame.page';
import {PlaGroupPublishComponent} from '../pla-group-publish/pla-group-publish.component';
import { FrameService } from '../../../../core/service/frame.service';

@Component({
  selector: 'app-plan-group-page',
  templateUrl: './plan-group-page.component.html',
  styleUrls: ['./plan-group-page.component.scss']
})
export class PlanGroupPageComponent extends FramePageComponent implements OnInit {

  groupMap;
  searchValue;

  constructor(modalService: BsModalService) {
    super(modalService);
    this.modalComponent = PlanGroupInitComponent;

    this.requestGroupMap();
  }

  ngOnInit() {
  }

  dismissGroup(group) {
    MsgService.confirmAlert(`确定要打散分组【${group.pn_name}】吗？`, () => {
      AjaxService.request('plan/group/dismiss.htm', group, () => {
        this.requestGroupMap();
        MsgService.successAlert('划线分组已解除');
      });
    });
  }

  requestGroupMap() {
    AjaxService.requestMap('plan/group/all.htm', {}, (map) => {
      this.groupMap = map;
    }, {RequestWithLoading: true});
  }

  openInitWindow() {
    this.showModal({closeCallback: this.requestGroupMap.bind(this)});
  }

  /**************
   * 打开修改分组信息
   */
  openGroupDetail(group) {
    this.showComponentModal(PlanGroupDetailComponent, {
      ResponseEntity: group,
      arrayUrl: 'plan/group/aspect/list.htm',
      arrayParam: {'pn_id': group.pn_id},
      closeCallback: this.requestGroupMap.bind(this)
    });
  }

  /*****************
   * 打开合并分组页面
   */
  openUnionGroup() {
    // 判断选中的
    const sArray = [];
    let batchs = 0;
    let pn_order = 1;
    for (const g of this.groupMap['GroupArray']) {
      if (g.checked === true) {
        pn_order = g.pn_order;
        sArray.push(this.groupMap['AspectArray_' + g.pn_id][0]);
        if (batchs === 0) {
          batchs = g.pn_batchs;
        } else {
          if (batchs !== g.pn_batchs) {
            MsgService.errorToastr('合并必须为同批次的分组');
            return;
          }
        }
      }
    }
    if (sArray.length === 0) {
      MsgService.warningToastr('请选择需要合并的划线分组');
      return;
    }
    if (sArray.length === 1) {
      MsgService.warningToastr('至少需要选中2个划线分组');
      return;
    }

    this.showComponentModal(PlanGroupDetailComponent, {
      ResponseEntity: {
        pn_batchs: batchs,
        pn_type: 'Country',
        sex_flag: 'InActive',
        qualified_min_score: 60,
        pn_order: pn_order
      },
      ResponseArray: sArray,
      closeCallback: this.requestGroupMap.bind(this)
    });
  }


  lockGroup(group) {
    AjaxService.requestEntity('plan/group/lock/check.htm', group, (entity) => {
      MsgService.confirmAlert(`该批次共有【${entity}】个考生合格，确定锁定吗？`, () => {
        AjaxService.requestEntity('plan/group/lock.htm', group, (entity) => {
          MsgService.successAlert('当前批次该分组已锁定！', () => {
            this.requestGroupMap();
          });

        });
      });
    });
  }

  completeGroup(group) {
    MsgService.confirmAlert(`确定要发布【${group.pn_name}】【${FrameService.getFrameDictText('ExamBatchName', '' + group.cur_batch)}】划线结果吗？`, () => {
      AjaxService.requestEntity('plan/group/compete.htm', group, (entity) => {
        MsgService.successAlert('已发布，请进行笔试、面试编排！',  () => {
          this.requestGroupMap();
        });

      }, {RequestWithLoading: true});
    });
  }

  publishPlan(group) {
    this.showComponentModal(PlaGroupPublishComponent, {ResponseEntity: group, closeCallback: this.requestGroupMap.bind(this)});
  }

  sendNotice(group) {
    MsgService.confirmAlert(`确定要发布想考生发送微信通知吗？`, () => {
      AjaxService.requestEntity('plan/group/notice.htm', group, (entity) => {
        MsgService.successAlert('已经将请求提交到后台！', () => {
          this.requestGroupMap();
        });
      }, {RequestWithLoading: true});
    });
  }
}
