import { Component, OnInit } from '@angular/core';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { BsModalService } from 'ngx-bootstrap/modal';

@Component({
  selector: 'app-kw-special-exambatch',
  templateUrl: './kw-special-exambatch.component.html',
  styleUrls: ['./kw-special-exambatch.component.scss']
})
export class KwSpecialExambatchComponent extends FlexTableComponent implements OnInit {

  query = {
    query: '',
    exam_time_id: ''
  }
  QUERY_URL = "kw/kwSpecialExamBatch/page.htm";
  DELETE_URL = "kw/kwSpecialExamBatch/delete.htm";
  GET_URL = "kw/kwSpecialExamBatch/get.htm";
  ADD_URL = "kw/kwSpecialExamBatch/add.htm";
  EDIT_URL = "kw/kwSpecialExamBatch/edit.htm";
  EXAM_TIME_URL = "pl/plExamTime/listtime.htm";
  SPECIAL_COURSE_URL = "pl/plCourse/listSpecial.htm";
  ROOM_LIST_URL = "kw/kwSpecialExamRoom/list.htm";
  examtimeList = [];
  roomList = [];
  addFormModel: FormGroup;
  tempFormModel: FormGroup;

  constructor(public bsModalService ?: BsModalService) {
    super();
    const fb = new FormBuilder();
    this.formModel = fb.group({
      id: [''],
      exam_time_id: ['', Validators.required],
      batch_num: ['', Validators.required],
      batch_type: ['', Validators.required],
      start_time: ['', Validators.required],
      batch_code: ['', Validators.compose([Validators.required, Validators.pattern("^[a-zA-Z0-9]+$")])],
      exam_length: ['', Validators.required],
      batch_name: ['', Validators.required],
      exam_interval: ['', Validators.required],
      exam_time: ['', Validators.required],
      course_id: ['', Validators.required],
      address: ['', Validators.required],
      room_capacity: ['', Validators.required],
      remark: ['']
    });
    this.addFormModel = fb.group({
      id: [''],
      exam_time_id: ['', Validators.required],
      batch_type: ['', Validators.required],
      batch_code: ['', Validators.required],
      batch_name: ['', Validators.required],
      exam_time: ['', Validators.required],
      address: ['', Validators.required],
      remark: ['']
    });
  }

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
  }
  preSave() {
    this.tempFormModel = this.formModel;
    this.formModel = this.addFormModel;
    this.save();
    this.formModel = this.tempFormModel;
  }

  goRoomDetail(editFormTemplateModal, batch_id: number, modalcss?) {
    FrameRequest.requestArray(this.ROOM_LIST_URL, {batch_id: batch_id}, (array) => {
      this.roomList = array;
    });
    let css = modalcss ? modalcss : "modal-lg";
    this.bsModalWin = this.bsModalService.show(editFormTemplateModal, { class: css });
  }
}
