import { FlexTableComponent } from "./../../../../shared/common/FlexTable.component";
import { Component, OnInit } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms";
import { CustomValidators } from "ng2-validation";
import { BsModalService } from "ngx-bootstrap/modal";

@Component({
  selector: "app-pl-exam-time",
  templateUrl: "./pl-exam-time.component.html",
  styleUrls: ["./pl-exam-time.component.scss"]
})
export class PlExamTimeComponent extends FlexTableComponent implements OnInit {
  public query = {
    year_code: "",
    exam_year: "",
    exam_month: "",
    status: ""
  };

  //TODO
  QUERY_URL = "pl/plExamTime/page.htm";
  DELETE_URL = "pl/plExamTime/deleteBatch.htm";
  GET_URL = "pl/plExamTime/get.htm";
  ADD_URL = "pl/plExamTime/add.htm";
  EDIT_URL = "pl/plExamTime/edit.htm";


  yearList = [];

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      year_code: [null, Validators.compose([Validators.required])],
      exam_year: [null, Validators.compose([Validators.required])],
      exam_month: [null, Validators.compose([Validators.required])],
      exam_first_day: [null, Validators.compose([Validators.required])],
      exam_second_day: [null, Validators.compose([Validators.required])],
      exam_third_day: [null],
      status: [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    let dateNow = new Date();
    this.yearList.push({key:dateNow.getFullYear() + 1,val:dateNow.getFullYear() + 1});
    this.yearList.push({key:dateNow.getFullYear(),val:dateNow.getFullYear()});
    for (let i=1;i<=8;i++) {
      let yearN = dateNow.getFullYear() - i;
      this.yearList.push({key:yearN,val:yearN});
    }
  }
  
  makeYearCode() {
    if (this.item) {
      if (this.item.exam_year != null && this.item.exam_month != null) {
        if (this.item.exam_month < 10) {
          this.item.year_code = this.item.exam_year + "0" + this.item.exam_month;
        } else {
          this.item.year_code = this.item.exam_year + "" + this.item.exam_month;
        }
      }
    }
  }
}
