import { Component, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { FrameDetailComponent } from '../../../../frame/core/detail/frame.detail';

declare let laydate;

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

  roomArray = [];
  step = 'main';
  roomResult = [];
  deleteRoomIds = [];

  constructor(bsModalRef: BsModalRef, fb: FormBuilder) {
    super(bsModalRef);
    this.detailForm = fb.group({

    });
   }

  ngOnInit(): void {
  }
  
  initDefautEntity(): void {
    this.AjaxService.requestArray('ly/group/room/list.htm', {ly_group_id : this.currentEntity.ly_group_id}, (array) => {
      this.roomResult = array;
      if(this.roomResult && this.roomResult.length>0) {
        this.step = 'room';
      } else {
        //编排表-查询考试日期
        this.getExamDate();
      }
    });
  }

  // 查询考试日期
  getExamDate() {
    this.AjaxService.requestEntity('ly/group/exam/date/list.htm', {ly_group_id : this.currentEntity.ly_group_id}, (entity) => {
      if(entity) {
        this.currentEntity.group_start_date = entity.min_exam_date;
        this.currentEntity.group_end_date = entity.max_exam_date;
      } else {
        this.currentEntity.group_start_date = '';
        this.currentEntity.group_end_date = '';
      }
    });
    this.step = 'main';
  }


  //初始化时间控件
  getDate(index?) {
    setTimeout(() => {
      laydate.render({
        elem:'#group_start_date',
        type:'date',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.currentEntity.group_start_date = value;
        }
      });
      laydate.render({
        elem:'#group_end_date',
        type:'date',
        theme:'#0c6acf',
        trigger: 'click',
        done:(value, date, endDate)=>{
          this.currentEntity.group_end_date = value;
        }
      });
      
  
    }, 500);
  }

  changeRoomTotal() {
    const array = [];
    if (this.currentEntity.room_total !== undefined) {
      this.currentEntity.room_total = parseInt(this.currentEntity.room_total, 10);
    }
    if (this.currentEntity.room_total >= 0) {
      if (this.roomArray === undefined) {
        this.roomArray = [];
      }
      for (let i = 1; i <= this.currentEntity.room_total; i++) {
        if (this.roomArray.length >= i) {
          array.push(this.roomArray[i - 1]);
        } else {
          array.push({
            room_order: i
          });
        }
      }
    }
    this.roomArray = array;
  }

  //确认考场数量
  confirmRoom() {
    if(!this.currentEntity.group_start_date) {
      this.MsgService.errorToastr('请填写开始日期！');
      return;
    }
    if(!this.currentEntity.group_end_date) {
      this.MsgService.errorToastr('请填写结束日期！');
      return;
    }
    if(!this.currentEntity.room_total) {
      this.MsgService.errorToastr('请填写结束日期！');
      return;
    }
    this.currentEntity.room_json = JSON.stringify(this.roomArray);
    this.AjaxService.requestArray('ly/group/room/list/confirm.htm', this.currentEntity, (array) => {
      this.roomResult = array;
      this.step = 'room';
    });
  }

  //删除考场
  deleteRoom(v, i) {
    this.MsgService.confirmAlert('确定要删除考场吗？', () => {
      this.AjaxService.requestArray('ly/group/room/exist/std.htm', {ly_room_id : v.ly_room_id}, (array) => {
        if(array && array.length > 0) {
          this.MsgService.errorAlert('该考场已经存在考生数据，不能删除！');
          return;
        } else {
          for(const room of this.roomResult) {
            debugger
            if(v.exam_date == room.exam_date) {
              if(room.roomArr[i].ly_room_id) {
                this.deleteRoomIds.push(room.roomArr[i].ly_room_id);
              }
              room.roomArr.splice(i,1);
            }
          }
        }
      });
      
    });
  }

  //保存考场
  saveGroupRoom() {
    var arr = [];
    for(const room of this.roomResult) {
      //设置考场序号
      // var i = 1;
      // for(const result of room.roomArr) {
      //   result.room_seq = i;
      //   arr.push(result);
      //   i++;
      // }
      
      //通过页面设置，不在自动生成
      for(const result of room.roomArr) {
        if(!result.room_seq) {
          this.MsgService.errorToastr('考场序号不能为空！');
          return;
        }
        arr.push(result);
      }
      
    }
    this.currentEntity.room_json = JSON.stringify(arr);
    this.currentEntity.group_start_date = this.getDateStr(this.currentEntity.group_start_date);
    this.currentEntity.group_end_date = this.getDateStr(this.currentEntity.group_end_date);
    this.currentEntity.delete_room_ids  = this.deleteRoomIds == undefined ? '' : this.deleteRoomIds;
    this.AjaxService.request('ly/group/room/save.htm',this.currentEntity, () => {
      this.MsgService.successToastr('考场配置成功!');
      this.closeWindow();
    });
  }

  getDateStr(date) {
    if(date && date.length) {
      return date.split(' ')[0];
    } else {
      return '';
    }
  }

}
