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-pl-publishplan",
  templateUrl: "./publish-plan-detail.component.html",
  styleUrls: ["./publish-plan-detail.component.scss"]
})
export class PublishPlanDetailComponent extends FlexTableComponent
  implements OnInit {

  @ViewChild('framePager')
  framePager:FramePagerComponent;

  yearList = [];
  courseList = [];
  showBtn = "pubfirst";
  //TODO
  QUERY_URL="pl/mergePublishPlan/page.htm";
  PUB_FIRST = "pl/mergePublishPlan/pubfirst.htm";
  PUB_FINAL = "pl/mergePublishPlan/pubfinal.htm";
  GET_URL = "pl/provExamPlan/get.htm";  //获取从省考表
  ADD_URL = "pl/provExamPlan/add.htm";  //添加都是保存到省考
  EDIT_URL = "pl/provExamPlan/edit.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_month': [null, Validators.compose([Validators.required])],
      'score' : [null]
    });
  }

  ngOnInit() {
    this.initYearList();
  }

  public query = {
    examMonth: "",
    query: "",
    score: "",
    status: "",
    pubYear: ""
  };

  search(param){
    param.exam_month=this.query.examMonth;
    param.query=this.query.query;
    param.score=this.query.score;
    param.status=this.query.status;
  }

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

  /*pubFirst() {
    let reqParam = {year:this.query.pubYear,months:[4,10],RequestLoading:true};
    FrameRequest.request(this.PUB_FIRST, reqParam, (ret) => {
      if (ret.errorCode == 0) {
          this.hideModal();
          this.showBtn = 'pubfinal';
          AppComponent.alert({title:"信息提示",text:"成功发布初稿!"});
          this.framePager.reload();
          return;
      }
    });
  }*/

  /*pubFinal() {
    AppComponent.confirm("确定发布?",()=>{
      let reqParam = {year:this.query.pubYear,RequestLoading:true};
      FrameRequest.request(this.PUB_FINAL, reqParam, (ret) => {
        if (ret.errorCode == 0) {
            this.hideModal();
            this.showBtn = 'pubfirst';
            AppComponent.alert({title:"信息提示",text:"发布成功!"});
            this.framePager.reload();
            return;
        }
      });
    });
  }*/

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

}