import {Component, OnInit} from '@angular/core';
import {Router} from '@angular/router';
import {CookieService} from 'ngx-cookie-service';
import {LayoutInterviewArrangeComponent} from '../layout-interview-arrange/layout-interview-arrange.component';
import {LayoutInterviewDetailComponent} from '../layout-interview-detail/layout-interview-detail.component';
import {LayoutInterviewAppointmentComponent} from '../layout-interview-appointment/layout-interview-appointment.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 { FrameService } from '../../../../../core/service/frame.service';
import { LayoutGroupParamNewComponent } from '../layout-group-param-new/layout-group-param-new.component';

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

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

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

    // 获取缓存中的微信编号
    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() {

  }

  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(LayoutInterviewDetailComponent, {
      ResponseArray: array,
      ResponseEntity: {ly_agent_id: this.agent.ly_agent_id}
    });
  }

  // 开始编排
  startArrange(group) {
    this.showComponentModal(LayoutInterviewArrangeComponent, {
      ResponseEntity: group
    });
  }

  // 修改编排
  editArrange(group) {
    this.showComponentModal(LayoutInterviewAppointmentComponent, {
      ResponseEntity: group,
      mapUrl: 'layout/group/interview/view.htm',
      mapParam: {
        ly_group_id: group.ly_group_id
      }
    });
  }

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

  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！');
        }
      }, {RequestWithLoading: true});

    });
  }


  refreshVideoGroup() {
    MsgService.confirmAlert('针对视频评分，有合并分组的情况，会将UMP同步过来的视频分组，更新为最新评分的分组。确定要更新吗？', () => {
      AjaxService.request('exam/video/group/refresh.htm', {ly_agent_id: this.agent.ly_agent_id}, () => {
        MsgService.successAlert('更新视频分组成功！');
      });
    });
  }

  showParamModal(group) {
    this.showComponentModal(LayoutGroupParamNewComponent, {
      ResponseEntity: group
    });
  }
}

