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-by-handwritting",
  templateUrl: "./handwritting.component.html",
  styleUrls: ["./handwritting.component.scss"]
})
export class HandWritting extends FlexTableComponent implements OnInit {

  public query = {
    query: ""
  };

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

  courseList=[];
  //TODO
  QUERY_URL = "by/byBlackList/page.htm";
  DELETE_URL = "by/byBlackList/delete.htm";
  GET_URL = "by/byBlackList/get.htm";
  ADD_URL = "by/byBlackList/add.htm";
  EDIT_URL = "by/byBlackList/edit.htm";
  UNLOCK_URL = "by/byBlackList/unlock.htm";

  yearList = [];
  filePath : String;


  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'ticket_no': [null, Validators.compose([Validators.required])],
      'cert_no': [null, Validators.compose([Validators.required])],
      'std_name': [null, Validators.compose([Validators.required])],
      'course_id': [null, Validators.compose([Validators.required])],
      'start_date': [null, Validators.compose([Validators.required])],
      'end_date': [null, Validators.compose([Validators.required])],
      'exam_date': [null, Validators.compose([Validators.required])],
      'remark': [null, Validators.compose([Validators.required])],
    });
  }

  ngOnInit() {
     FrameRequest.requestPage("pl/plCourse/page.htm", {start:0,limit:9999,nation_exam:1,query:""}, (page)=>{
       this.courseList = page.records;
     });
  }

  search(param) {
    param.query=this.query.query;
  }

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

  //解禁
  unLock() {
    let checkedArray = this.framePager.getCheckedArray();
    if(checkedArray.length === 0){
      AppComponent.alert({title:"信息提示",text:"请选择数据!"})
      return;
    }
    AppComponent.confirm("确定删除",()=>{
      let checkedArray = this.framePager.getCheckedArray((item)=>item.id);
      FrameRequest.requestMap(this.UNLOCK_URL, {ids:checkedArray.join()}, () => {
        AppComponent.GobalComponent.successMsg('解禁成功!');
        this.aftDeleteEvent();
        this.framePager.reload();
      });
    });
  }
}