import { Component, OnInit, TemplateRef } from '@angular/core';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { AppComponent } from '../../../../../app.component';
import { BsModalService } from 'ngx-bootstrap/modal';
declare var $: any;
@Component({
  selector: 'app-kw-special-layout',
  templateUrl: './kw-special-layout.component.html',
  styleUrls: ['./kw-special-layout.component.scss']
})
export class KwSpecialLayoutComponent extends FlexTableComponent implements OnInit {

  query = {
    batch_id: '',
    exam_time_id: ''
  };
  EXAM_TIME_URL = "pl/plExamTime/listtime.htm";
  LIST_URL = "kw/kwSpecialLayout/list.htm";
  POINT_LIST_URL = "kw/kwSpecialLayout/listPoint.htm";
  ROOM_LIST_URL = "kw/kwSpecialExamRoom/list.htm";
  LAYOUT_URL = "kw/kwSpecialLayout/layout.htm";
  CANCEL_URL = "kw/kwSpecialLayout/cancel.htm"
  examtimeList = [];
  pointList = [];
  layoutList = [];
  noLayoutList = [];
  roomList = [];

  constructor(public bsModalService?: BsModalService) {
    super();
  }

  ngOnInit() {
  }


  getPoint(exam_time_id: any) {
    if (exam_time_id) {
      FrameRequest.requestArray(this.POINT_LIST_URL, { exam_time_id: exam_time_id }, (array) => {
        this.pointList = array;
      });
    } else {
      this.pointList = [];
    }
  }
  listLayout() {
    if (!this.nullJudge()) return;
    FrameRequest.requestMap(this.LIST_URL,
      { batch_id: this.query.batch_id, exam_time_id: this.query.exam_time_id }, (map) => {
        this.layoutList = this.objToArray(map['layoutList']);
        this.noLayoutList = this.objToArray(map['noLayoutList']);
      });
  }

  goPointDetail(template: TemplateRef<any>, modalcss: string) {
    if (!this.nullJudge('layout')) return;
    FrameRequest.requestArray(this.ROOM_LIST_URL, { batch_id: this.query.batch_id }, (array) => {
      this.roomList = array;
    });
    let css = modalcss ? modalcss : "modal-lg";
    this.bsModalWin = this.bsModalService.show(template, { class: css });
  }

  //开始编排
  layout() {
    if (!this.nullJudge()) return;
    FrameRequest.request(this.LAYOUT_URL, { batch_id: this.query.batch_id, exam_time_id: this.query.exam_time_id }, () => {
      AppComponent.GobalComponent.successMsg("编排成功");
      this.hideModal();
      this.listLayout();
    });
  }

  //取消编排
  cancel() {
    if (!this.nullJudge('cancel')) return;
    AppComponent.confirm("确定取消编排吗？取消编排后，编排好的结果将清空！",()=>{
      FrameRequest.request(this.CANCEL_URL, { batch_id: this.query.batch_id, exam_time_id: this.query.exam_time_id }, () => {
        AppComponent.GobalComponent.successMsg("取消编排成功");
        this.hideModal();
        this.listLayout();
      });
    });
  }

  /**
   * 对象转换为数组，便于模板循环数据
   * @param obj ：需要转换的对象
   */
  objToArray(obj: any) {
    let result = [];
    if (obj) {
      for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
          const element = obj[key];
          result.push(element);
        }
      }
      return result;
    }
  }

  nullJudge(flag?: any) {
    if (!this.query.exam_time_id) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择考试时间' });
      return false;
    }
    if (!this.query.batch_id) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择考点' });
      return false;
    }
    if(flag === 'cancel' && this.layoutList.length === 0 ) {
      AppComponent.alert({ 'title': '提示', 'text': '没有可取消的编排数据' });
      return false;
    }
    if(flag === 'layout' && this.noLayoutList.length === 0 ){
      AppComponent.alert({ 'title': '提示', 'text': '没有可编排的数据' });
      return false;
    }
    return true;
  }

}
