import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators } from '@angular/forms';
import { CustomValidators } from 'ng2-validation';
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../../shared/common/FlexTable.component";
import { AppComponent } from "../../../../../app.component";
import { FrameRequest } from "../../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../../core/pager/pager.component";

@Component({
  selector: "app-kw-examrelation",
  templateUrl: "./examrelation.component.html",
  styleUrls: ["./examrelation.component.scss"]
})
export class ExamRelationComponent extends FlexTableComponent
  implements OnInit {

  examtimeList = [];
  examPointList = [];
  orgList = [];

  //TODO
  QUERY_URL = "kw/kwExamRelation/page.htm";
  DELETE_URL = "kw/kwExamRelation/delete.htm";
  GET_URL = "kw/kwExamRelation/get.htm";
  ADD_URL = "kw/kwExamRelation/add.htm";
  EDIT_URL = "kw/kwExamRelation/edit.htm";

  TIME_URL = "pl/plExamTime/listtime.htm";
  POINT_TYPE_URL = "kw/kwExamPoint/page.htm";
  ORG_LIST_URL = "cf/cfOrganization/list.htm";

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    this.formModel = fb.group({
      id: [null],
      'exam_point_id': [null, Validators.compose([Validators.required])],
      'exam_time_id': [null, Validators.compose([Validators.required])],
      'exam_area_id': [null, Validators.compose([Validators.required])],
    });
  }

  public query = {
    examTimeId: "",
    query: "",
  };

  search(param) {
    param.exam_time_id = this.query.examTimeId;
    param.query = this.query.query;
  }

  ngOnInit() {
    FrameRequest.requestMap("kw/kwExamRelation/initdata.htm", {}, (map)=>{
      this.examtimeList = map["examtimeList"];
      this.examPointList = map["examPointList"];
      this.orgList = map["orgList"];
      if (map["exam_time_id"]) {
          this.query.examTimeId = map["exam_time_id"];
      }
    });
  }

  async aftGoEditEvent() {
    if(!this.item || this.item.id === undefined) {
      this.item = {};
      this.item.exam_time_id = this.query.examTimeId;
    }
   }
}