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";
import { FrameValidator } from "../../../../../shared/frame/Frame-validator";
import { FlexSelectComponent } from "../../../../../shared/components/flexSelect/FlexSelect.component";

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

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

  examtimeList = [];

  //TODO
  QUERY_URL = "kw/kwExamPoint/page.htm";
  DELETE_URL = "kw/kwExamPoint/delete.htm";
  GET_URL = "kw/kwExamPoint/get.htm";
  ADD_URL = "kw/kwExamPoint/add.htm";
  EDIT_URL = "kw/kwExamPoint/edit.htm";
  TIME_URL = "pl/plExamTime/listtime.htm";
  @ViewChild('exam_time_select')  
  exam_time_select:FlexSelectComponent;

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'exam_name': [null, Validators.compose([Validators.required,CustomValidators.rangeLength([2, 50])])],
      'exam_code': [null, Validators.compose([Validators.required,Validators.pattern(FrameValidator.pattern_en_number)])],
      'point_type': [null],
      'exam_time_id': [null, Validators.compose([Validators.required])],
      'remark': [null]
    });
  }

  ngOnInit() {
    //考试日期
    FrameRequest.requestArray(this.TIME_URL, {}, (arrary) => {
      this.examtimeList = arrary;
    });
    FrameRequest.requestMap("kw/kwExamPoint/initdata.htm", {}, (map)=>{
      if (map["exam_time_id"]) {
          this.query.examTimeId = map["exam_time_id"];
      }
    });
  }

  //分页查询条件
  search(param) {
    if (!this.query.examTimeId) {
        this.query.examTimeId = "";
    }
    param.query = this.query.query;
    param.exam_time_id = this.query.examTimeId;
    param.point_type = this.query.point_type;
  }

  hideCouseWindow() {
    if (this.bsModalWin) this.bsModalWin.hide();
    this.query.query = "";
    this.resetPage();
  }

  async aftGoEditEvent() {
    if(!this.item || this.item.id === undefined) {
      this.item = {};
      this.item.exam_time_id = this.query.examTimeId;
      FrameRequest.requestMap("kw/kwExamPoint/getcode.htm", {exam_time_id: this.query.examTimeId}, (map)=>{
        
        if (map["code"]) {
          this.item.exam_code = map["code"];
        }
      });
    }
   }
}