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

  public query = {
    year_code: "",
    school_id: "",
    major_id:"",
    course_id:"",
    course_name:"",
    query: ""
  };

  @ViewChild('framePager')
  framePager:FramePagerComponent;

  majorList=[];
  yearCodeList=[];
  masterSchoolList=[];
  
  //TODO
  QUERY_URL = "kj/kjCourseRatio/page.htm";
  DELETE_URL = "kj/kjCourseRatio/delete.htm";
  GET_URL = "kj/kjCourseRatio/get.htm";
  ADD_URL = "kj/kjCourseRatio/add.htm";
  EDIT_URL = "kj/kjCourseRatio/edit.htm";

  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    // CustomValidators.rangeLength([1, 6])
    this.formModel = fb.group({
      id: [null],
      'school_id': [null, Validators.compose([Validators.required])],
      'major_id': [null],
      'course_id': [null, Validators.compose([Validators.required])],
      'course_name': [null, Validators.compose([Validators.required])],
      'year_code' : [null, Validators.compose([Validators.required])],
      'unified_ratio': [null, Validators.compose([Validators.required])],
      'other_ratio': [null, Validators.compose([Validators.required])],
      'apply_form': [null, Validators.compose([Validators.required])]
    });
  }

  ngOnInit() {
    //年度码
    FrameRequest.requestArray("pl/plExamTime/list.htm", {}, (array) => {
      this.yearCodeList = array;
    });
    //专业
    var parm = {ids:""};
    FrameRequest.requestArray("pl/plMajor/list.htm", parm, (arrary) => {
      this.majorList = arrary;
    });
    //主考学校
    FrameRequest.requestArray("cf/cfOrganization/list.htm", {org_type: 1}, (arrary) => {
      this.masterSchoolList = arrary;
    });
  }

  search(param) {
    if (this.query.year_code == "null" || this.query.year_code == null) {
      this.query.year_code = "";
    }
    if (this.query.school_id == "null" || this.query.school_id == null) {
      this.query.school_id = "";
    }
    if (this.query.course_id == "null" || this.query.course_id == null) {
      this.query.course_id = "";
    }
    if (this.query.major_id == "null" || this.query.major_id == null) {
      this.query.major_id = "";
    }   
    param.year_code=this.query.year_code;
    param.school_id=this.query.school_id;
    param.query=this.query.query;
    param.course_id=this.query.course_id;
    param.major_id=this.query.major_id;
  }

  /**
   * 查询列表课程选择
   */
  courseQueryModal(modal) {
    let checkedArray = modal.framePager.getCheckedArray();
    if (checkedArray.length > 0) {
      this.query.course_name = checkedArray[0].course_name;
      this.query.course_id = checkedArray[0].id;
      modal.hideModal();
    }
  }

  //编辑界面的课程选择
  addCourseModalConfirm(modal) {
    let checkedArray = modal.framePager.getCheckedArray();
    if (checkedArray.length > 0) {
      this.item.course_name = checkedArray[0].course_name;
      this.item.course_id = checkedArray[0].id;
      modal.hideModal();
    }
  }

  formateShow(status) {
    if (status == 1) {
      return "生效";
    } else {
      return "失效";
    }
  }
}