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 { FrameFunction } from "../../../../../shared/frame/frame_function";
import * as _ from 'lodash';

@Component({
  selector: "app-pl-college-statistic",
  templateUrl: "./college-statistic.component.html",
  styleUrls: ["./college-statistic.component.scss"]
})
export class CollegeStatisticComponent extends FlexTableComponent implements OnInit {
  
  public queryDetail = {
    course_id: "",
    course_name: "",
    major_id: "",
    year_code: "",
    score_type: "",
    query: "",
    joinSchoolId:""
  }

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

  filePath : String;
  yearCodeList = [];
  courseList = [];
  batchList = [];//列表上报批次
  impBatchList = [];//导入界面批次列表

  QUERY_URL = "kj/kjCollegeScoreMain/page.htm";
  DELETE_URL = "kj/kjCollegeScoreMain/delete.htm";
  GET_URL = "kj/kjCollegeScoreMain/get.htm";
  ADD_URL = "kj/kjCollegeScoreMain/add.htm";
  EDIT_URL = "kj/kjCollegeScoreMain/edit.htm";

  UPLOAD_URL = "pl/kjCollegeScoreMain/practical/upload.htm";
  ORG_LIST_URL = "cf/cfOrganization/list.htm";
  QUERY_DETAIL_URL = "kj/kjCollegeScoreDetail/queryScore.htm";
  //主记录提交审核
  SUBMIT_TO_AUTH = "kj/kjCollegeScoreMain/submitaudit.htm";
  //删除详情记录
  DETAIL_DELETE_URL = "kj/kjCollegeScoreDetail/delete.htm";
   // 导出
   EXPORT_URL= "kj/kjCollegeScoreMain/exportScore.htm";  
   EXPORT_FALIE_URL= "kj/kjCollegeScoreMain/exportFalieScore.htm";  

  year_code="";
  school_id="";
  majorList = [];
  joinSchoolList = [];
  defaultBatchName = "";

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

  ngOnInit() {
    let this_ = this;
    FrameRequest.requestArray("pl/plMajor/openMajorList.htm", {}, (arrary) => {
      this.majorList = arrary;
    });

    setTimeout(() => {
        FrameRequest.requestMap("kj/kjCollegeScoreMain/initdata.htm", {}, (map)=>{
           // this_.majorList = map["majorList"];
            this_.joinSchoolList = map["joinSchoolList"];
            this_.yearCodeList = map["yearCodeList"];
            if (map["year_code"]) {
                this_.queryDetail.year_code = map["year_code"];
                this_.detailPager.reload();
            }
        });
    }, 1000);
  }

  searchDetail(paramDetail) {
    if (!this.queryDetail.course_id) {
      this.queryDetail.course_id = "";
    }
    if (!this.queryDetail.major_id) {
      this.queryDetail.major_id = "";
    }
    if (!this.queryDetail.year_code) {
      this.queryDetail.year_code = "";
    }

    if (!this.queryDetail.joinSchoolId) {
      this.queryDetail.joinSchoolId = "";
    }

    

    paramDetail.query = this.queryDetail.query;
    paramDetail.courseId = this.queryDetail.course_id;
    paramDetail.majorId = this.queryDetail.major_id;
    //paramDetail.schoolId = this.school_id;
    paramDetail.yearCode = this.queryDetail.year_code;
    paramDetail.scoreType = this.queryDetail.score_type;
    paramDetail.joinSchoolId = this.queryDetail.joinSchoolId;
  }

  //关闭导入框，留在详情界面
  hideImpWin() {
    if(this.bsModalWin) 
      this.bsModalWin.hide();
  }

  //实践课的成绩 部分是存的 201，202，203，204
  formatScore(item) {
    if (item.score_type == 0) {//实践课
      if (item.score == '201') {
        return '合格';
      } else if (item.score == '202') {
        return '中等';
      } else if (item.score == '203') {
        return '良好';
      } else if (item.score == '204') {
        return '优秀';
      } else {
        return item.score;
      }
    } else if (item.score_type == 4) {//毕业论文
      if (item.score == '1') {
        return '及格';
      } else if (item.score == '2') {
        return '中等';
      } else if (item.score == '3') {
        return '良好';
      } else if (item.score == '4') {
        return '优秀';
      }else if (item.score == '0') {
        return '不及格';
      }  else {
        return item.score;
      }
    } else {
      return item.score;
    }

  }

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

  //重置
  reset() {
    this.queryDetail.course_id = "";
    this.queryDetail.course_name= "";
    this.queryDetail.major_id = "";
    this.queryDetail.year_code = "";
    this.queryDetail.score_type = "";
    this.queryDetail.query = "";
    this.queryDetail.joinSchoolId = "";
    

  }

  //导出
  exportScore() {
    if (!this.queryDetail.year_code) {
      AppComponent.alert({ title: "信息提示", text: "请选择年度码!" });
      return true;
    }


    if (!this.queryDetail.course_id) {
      this.queryDetail.course_id = "";
    }

    if (!this.queryDetail.major_id) {
      this.queryDetail.major_id = "";
    }

    if (!this.queryDetail.score_type) {
      this.queryDetail.score_type = "";
    }

    if (!this.queryDetail.joinSchoolId) {
      this.queryDetail.joinSchoolId = "";
    }
    
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download(this.EXPORT_URL, {
      yearCode : this.queryDetail.year_code,
      query:this.queryDetail.query,
      courseId:this.queryDetail.course_id,
      majorId:this.queryDetail.major_id,
      scoreType:this.queryDetail.score_type,
      joinSchoolId:this.queryDetail.joinSchoolId
    }, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("合成成绩导出成功");
    });
  }

    //导出合成成绩失败记录
	exportFalieScore() {
		if (!this.queryDetail.year_code) {
		  AppComponent.alert({ title: "信息提示", text: "请选择年度码!" });
		  return true;
		}
		
		AppComponent.GobalComponent.setLoading(true);
		FrameRequest.download(this.EXPORT_FALIE_URL, {
		  yearCode : this.queryDetail.year_code
		}, () => {
		  AppComponent.GobalComponent.setLoading(false);
		  AppComponent.GobalComponent.successMsg("导出合成成绩失败记录成功");
		});
	  }
}