import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {CookieService} from 'ngx-cookie-service';
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 { FrameService } from '../../../../../core/service/frame.service';
import { UmpGroupInterviewDetailComponent } from '../ump-group-interview-detail/ump-group-interview-detail.component';

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


  batchNameArray: any;
  agent: any;
  searchText: any;
  exam_batch: any;
  showSingleGroup: any;

  constructor(private router: Router, private cookies: CookieService, modalService: BsModalService) {
    super(modalService);
    this.modalComponent = UmpGroupInterviewDetailComponent;

    // 获取缓存中的微信编号
    const agent_id = this.cookies.get('CurrentAgentId');
    if (agent_id !== undefined && agent_id.length > 0) {
      // 去获取
      AjaxService.requestEntity('layout/agent/find.htm', {ly_agent_id: agent_id}, (entity) => {
        this.agent = entity;
        setTimeout(() => {
          if (this.ajax) {
            this.ajax.reload();
          }
        }, 1);
      });
      const array = [{dict_value: '0', dict_text: '全批次'}];
      for (const v of FrameService.getFrameDictArray('ExamBatchName')) {
        array.push(v);
      }
      this.batchNameArray = array;
    } else {
      this.router.navigate(['/art/layout/agent/page']);
    }
  }

  ngOnInit(): void {
  }

  onModalClose() {
    this.ajax.reload();
  }

  makeGroup() {
    const array = [];
    for (const v of this.ajax.getMap()['SingleArray']) {
      if (v.checked === true) {
        array.push(v);
      }
    }
    if (array.length < 2) {
      MsgService.errorToastr('分组操作至少需要2个科目！');
      return;
    }

    this.showComponentModal(UmpGroupInterviewDetailComponent, {
      ResponseArray: array,
      ResponseEntity: {ly_agent_id: this.agent.ly_agent_id}
    });
  }

  optrAllGroup(flag) {
    for (const v of this.ajax.getMap()['MoreArray']) {
      v.show_flag = flag;
    }
  }

/*******************************
 *   同步到UMP系统
 */
  syncUmp() {
    if(!this.exam_batch) {
      MsgService.errorAlert('请选择同步的批次！');
      return;
    }
    MsgService.confirmAlert('确定要同步所有的分组数据到UMP吗？',  () => {
      AjaxService.request('ump/group/data/push.htm', {
        agent_id : this.agent.ly_agent_id,
        exam_batch: this.exam_batch
      }, (result) => {
        if(result && result.entity.indexOf('404.html') != -1) {
          MsgService.errorAlert('推送失败，请联系UMP后台开发人员！');
        } else {
          MsgService.successAlert('已成功将数据推送给UMP！');
        }
      });

    });
  }

  /***************************
   *  同步到评分分组-(ump_ly_group 同步到 ly_group)
   */
  syncJudgeGroup() {
    MsgService.confirmAlert('该操作会将艺考系统中和UMP一致的分组同步到评分分组中（ly_group表），请谨慎操作！', () => {
      AjaxService.request('layout/ump/group/sync.htm', {
        agent_id: this.agent.ly_agent_id
      }, () => {
        MsgService.successAlert('同步成功！');
      })
    });
  }

}
