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

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

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  yearList = [];
  courseList = [];
  filePath : String;

  public query = {
    examYear: "",
    examMonth: "",
    impExpYear:"",  //导入导出年份
    query: "",
    score: ""
  };

  //TODO
  QUERY_URL = "pl/provExamPlan/page.htm";
  DELETE_URL = "pl/provExamPlan/delete.htm";
  GET_URL = "pl/provExamPlan/get.htm";
  ADD_URL = "pl/provExamPlan/add.htm";
  EDIT_URL = "pl/provExamPlan/edit.htm";
  ERROR_DOWNLOAD_URL = "pl/provExamPlan/downloaderror.htm";

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'prov_course_id': [null, Validators.compose([Validators.required])],
      'prov_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() {
    this.initYearList();
  }

  initYearList() {
    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:0,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.prov_course_code=courseCode;
        this.item.prov_course_name=courseName;
        this.item.score=this.courseList[i].course_score;//学分仅仅显示，不提交后台
        break;
      }
    }
  }



  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;
        AppComponent.alert({ 'title': '提示', 'text': '有错误数据，请修正后，在重新上传' });
      } else {
        this.hideModal();
        this.framePager.reload();
        AppComponent.alert({title:"信息提示",text:"上传成功!"});
      }
    }));
  }

  //错误数据下载
  downloadErrFile() {
    FrameRequest.download(this.ERROR_DOWNLOAD_URL,  { filePath: this.filePath });
  }

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

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

  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";
    }
  }
}