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 {FrameUploaderComponent} from '../../../../../core/uploader/uploader.component';
import { FrameRequest } from "../../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../../core/pager/pager.component";
import { isNumber } from "util";

@Component({
  selector: "app-pl-countryplan",
  templateUrl: "./country-plan.component.html",
  styleUrls: ["./country-plan.component.scss"]
})
export class CountryPlanComponent extends FlexTableComponent implements OnInit {

  public query = {
    examMonth: "",
    examYear: "",
    impExpYear:"",
    query: "",
    score:""
  };

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;
  @ViewChild('framePager')
  framePager:FramePagerComponent;

  courseList=[];
  //TODO
  QUERY_URL = "pl/countryExamPlan/page.htm";
  DELETE_URL = "pl/countryExamPlan/delete.htm";
  GET_URL = "pl/countryExamPlan/get.htm";
  ADD_URL = "pl/countryExamPlan/add.htm";
  EDIT_URL = "pl/countryExamPlan/edit.htm";
  UPLOAD_URL = "pl/countryExamPlan/upload.htm";
  ERROR_DOWNLOAD_URL = "pl/countryExamPlan/downloaderror.htm";

  yearList = [];
  filePath : String;


  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'country_course_id': [null, Validators.compose([Validators.required])],
      'country_exam_time': [null, Validators.compose([Validators.required])],
      'exam_time_id': [null, Validators.compose([Validators.required])],
      'score' : [null],
     // 'start_time': [null, Validators.compose([Validators.required])],
     // 'end_time': [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});
     }
     FrameRequest.requestPage("pl/plCourse/page.htm", {start:0,limit:9999,nation_exam:1,query:""}, (page)=>{
       this.courseList = page.records;
     });
  }

  search(param) {
    if (this.query.examYear == "null" || this.query.examYear == null) {
      this.query.examYear = "";
    }
    if (this.query.score == "null" || this.query.score == null) {
      this.query.score = "";
    }  
    param.exam_year=this.query.examYear;
    param.exam_month=this.query.examMonth;
    param.query=this.query.query;
    param.score=this.query.score;
  }

  selectCourse(courseId) {
    for (let i=0;i<this.courseList.length;i++) {
      if (this.courseList[i].id == courseId) {
        let codeStr = this.courseList[i].code_course_name.split(")")[0];
        let nameStr = this.courseList[i].code_course_name.split(")")[1];
        let courseCode = codeStr.substring(1, codeStr.length);
        let courseName = nameStr.trim();
        this.item.country_course_code=courseCode;
        this.item.country_course_name=courseName;
        this.item.score=this.courseList[i].course_score;//学分仅仅显示，不提交后台
        break;
      }
    }
  }
  //错误数据下载
  downloadErrFile() {
    FrameRequest.download(this.ERROR_DOWNLOAD_URL,  { filePath: this.filePath });
  }

  importFunc() {
    if (!this.query.impExpYear) {
      AppComponent.alert({title:"信息提示",text:"选择年度!"});
      return;
    }
    let param = {year:this.query.impExpYear,title_rows:1};
    this.uploader.choooseFileAndUpload(param, ((ret) => {
      if (ret.errorCode == -1) {
        this.filePath = ret.url;
        this.hideModal();
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else {
        this.hideModal();
        this.framePager.reload();
        AppComponent.alert({title:"信息提示",text:"导入成功!"});
      }
    }));
  }

  exportFunc() {
    if (!this.query.impExpYear) {
      AppComponent.alert({title:"信息提示",text:"选择导出年份!"});
      return;
    }
    //window.open('http://localhost:8080/ses-manage-v1/pl/countryExamPlan/export.htm?year='+this.query.impExpYear);
    FrameRequest.download("pl/countryExamPlan/export.htm",{year: this.query.impExpYear});
    this.hideModal();
  }

  downTemplate() {
    //window.open('http://localhost:8080/ses-manage-v1/pl/countryExamPlan/download.htm');
    FrameRequest.download("pl/countryExamPlan/download.htm");
  }

  selectImpYear(year) {
    this.query.impExpYear=year;
  }

  freshStartEndTime(examBatch) {
    if (examBatch == "A") {
      this.item.start_time="09:30";
      this.item.end_time="11:00";
    } else if (examBatch == "B") {
      this.item.start_time="14:30";
      this.item.end_time="17:00";
    } else if (examBatch == "C") {
      this.item.start_time="09:30";
      this.item.end_time="11:00";
    } else if (examBatch == "D") {
      this.item.start_time="14:30";
      this.item.end_time="17:00";
    }else if (examBatch == "X") {
      this.item.start_time="09:30";
      this.item.end_time="11:00";
    } else if (examBatch == "Y") {
      this.item.start_time="14:30";
      this.item.end_time="17:00";
    }
  }
}