import {Component, OnInit, ViewChild} from '@angular/core';
import {CookieService} from 'ngx-cookie-service';
import {Router} from '@angular/router';
import {LayoutWrittenArrangeComponent} from '../layout-written-arrange/layout-written-arrange.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 {FrameUpload} from "../../../../../core/frame/upload/frame-upload/frame-upload";
import { ModalService } from 'src/app/core/service/modal.service';

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

  FrameUpload = FrameUpload;
  agent: any;
  batchNameArray: any;
  searchText: any;
  exam_batch:any;
  showSingleGroup = false;
  // 分页组件
  // @ViewChild('uploader', {static: false}) uploader: UploaderComponent;

  constructor(private router: Router, private cookies: CookieService, modalService: BsModalService) {
    super(modalService);
    this.modalComponent = LayoutWrittenArrangeComponent;
    // 获取缓存中的微信编号
    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']);
    }
  }

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

  ngOnInit() {

  }


  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(LayoutWrittenArrangeComponent, {
      ResponseArray: array
    });
  }

  confirmNoRoom(group) {
      MsgService.confirmAlert(`笔试【${group.group_name}】设置了集中候考，未设置考场，确定该笔试以无考场的形式发布吗？`, () => {
        AjaxService.request('layout/group/written/confirm.htm', {ly_group_id: group.ly_group_id}, () => {
          this.ajax.reload();
        });
      });
  }


   publishWritten(group) {
    let msg = `确定要发布笔试【${group.group_name}】吗？一旦发布，将不可回退，请谨慎进行此操作！`;
    if (group.room_total === undefined || group.room_total < 1) {
      msg = `笔试【${group.group_name}】设置了统一候考，未指定考场信息，确定进行发布操作吗？`;
    }
    // this.ajax.optrRecord('layout/group/written/publish.htm',{ly_group_id: group.ly_group_id},{ confirmMsg: msg}, {RequestWithLoading: true});
    this.MsgService.confirmAlert(msg, () => {
      this.AjaxService.request('layout/group/written/publish.htm',{ly_group_id: group.ly_group_id}, (ajax) => {
        this.ModalService.showThreadWindow(this.bsService, ajax.entity, {threadTitle: '正在发布编排!'});
        this.ajax.reload();
      }, {RequestWithLoading: true});
    });
   }

  uploadRoom(group) {
    MsgService.confirmAlert('这是发布后上传考场，将直接将考生安排到考场，确定要上传考场并自动安排考场座位吗？', () => {
    ModalService.showUploadWindow(this.bsService,'layout/group/written/room/upload.htm',
            {ly_group_id: group.ly_group_id},{
              uploadTitle:'上传考场',
              templateFile:'模版-笔试考场.xls',
              uploadCallback:this.uploadCallback.bind(this),
              autoClose:true
      })
    });
  }

  uploadCallback(ajax) {
    MsgService.successAlert('导入成功!');
  }

  //重置编排
  resetData(v) {
    this.MsgService.confirmAlert('确定要重置分组【'+v.group_name+'】的编排吗？', () => {
      this.AjaxService.request('layout/group/written/reset.htm',{ly_group_id: v.ly_group_id}, () => {
        this.MsgService.successToastr('重置编排成功！');
        this.ajax.reload();
      });
    });
  }
}
