import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators, FormGroup} 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 { ThemesService } from "../../../../../core/themes/themes.service";
import * as _ from 'lodash';

@Component({
  selector: "app-pl-querybyscore",
  templateUrl: "./query-by-score.component.html",
  styleUrls: ["./query-by-score.component.scss"]
})
export class QueryByScoreComponent extends FlexTableComponent implements OnInit {

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

  courseList=[];
  //TODO
  QUERY_URL = "by/byApply/queryPage.htm";
  DELETE_URL = "by/byApply/delete.htm";
  GET_URL = "by/byApply/get.htm";
  ADD_URL = "by/byApply/add.htm";
  EDIT_URL = "by/byApply/edit.htm";

  ORG_LIST_URL = "cf/cfOrganization/list.htm";
  AUTH_DETAIL_URL = "by/byApply/getdetail.htm";
  AUTH_LIST_URL = "by/byApply/viewauth.htm";//审核记录
  OLD_CJ_URL = "hz/cjJigechengjiOld/page.htm";
  UPLOAD_URL = "pl/byApply/upload.htm";
  TICKET_ASSO_SCORE_URL = "kj/kjUnifiedScoreDetail/ticketassopage.htm";//准考证关联成绩查询

  List_URL = "std/stdRegInfo/list.htm";

  HISTROY_SCORE_URL = "kj/kjUnifiedScoreDetail/byPrediction.htm";


  orgList = [];
  filePath : String;
  scoreList = [];//考生的毕业成绩Map
  majorList = [];



  majorLevel = "";


  photo_url: any;


  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();
    this.formModel = fb.group({
      id: [null],
      'std_name' : [null],
      'cert_no': [null],
      'ticket_no': [null],
      'std_sex': [null],
      'nation': [null],
      'std_birth': [null],
      'native_prov': [null],
      'native_city': [null],
      'political': [null],
      'pre_edu': [null],
      'work_unit': [null],
      'telephone': [null],
      'address': [null],
      'post_code': [null],
      'radioSelect' : [null, Validators.compose([Validators.required])],
      'remark' : [null, Validators.compose([Validators.required])],
    });
    
  }

  ngOnInit() {
  
    FrameRequest.requestMap("by/byApply/initdata.htm", {}, async (map)=>{
     
			this.photo_url = map.photo_url;
    });

  }

  public query = {
    query: "",
    cityOrgId: "",
    masterSchoolId: "",
    jionSchoolId: "",
    examTimeId: "",
    majorId: "",
    status: "",
    std_source:""
  };

  

  isActive=0;

  stdList = [];
  ticket_no: any;
  std_name: any;
  cert_no: any;
  std_mobile: any;

  majorArraySource: any;
  majorArray: any;

  historyScore = []; //考生历史成绩
  orderNum : any;

  searchData(event){
    if(!this.query.query){
      AppComponent.alert({title:"信息提示",text:"请填写关键字!"});
      return;
    }else{
      
      this.framePager.reload();

    }

  }

  searchStd() {
    FrameRequest.requestArray(this.List_URL, {
      ticket_no : this.ticket_no === undefined ? '': this.ticket_no,
      std_name :this.std_name === undefined ? '': this.std_name,
      cert_no : this.cert_no === undefined ? '': this.cert_no,
      std_mobile: this.std_mobile === undefined ? '': this.std_mobile,
    }, (array) => {
      this.stdList = array;
      if(array && array.length>0){
        
      }else{
        AppComponent.GobalComponent.errorMsg("没有可查询的记录");
      }
     
     });
  }


  //弹出审核框
  showEditWin(obj) {
   
   
    this.editShowType="only";
    FrameRequest.requestMap(this.AUTH_DETAIL_URL, {id: obj.id}, (map) => {
      this.item = map.detail;
      this.majorLevel = map.majorLevel;
      //this.scoreList = map.scoreMap;
    });
    //详情界面的成绩
    FrameRequest.requestArray("by/byApply/getscorelistForQuery.htm", {id: obj.id}, (arrary)=>{
      this.scoreList = arrary;
    });
  }

    //弹出审核框
    showEditWin1(obj) {

      this.editShowType="only1";

      FrameRequest.requestEntity("std/stdRegInfo/get.htm",{id: obj.id}, async (entity) => {
        if(entity) {
          this.item = entity;

          FrameRequest.requestArray('std/stdRegInfo/queryOpenMajorAndStdMajor.htm',{major_id:this.item.major_id}, (array) => {
              this.majorArraySource = array;
              this.majorLevelChange(this.item.major_level);
              this.majorChange(this.item);
            })

        }
       
      });

     
    }

    majorLevelChange(major_level){
     
      this.majorFilterByLevel(major_level);
  
    }
  
    majorFilterByLevel(major_level){
      this.majorArray = _.filter(this.majorArraySource,{'major_level': parseInt(major_level)});
      
    }

    majorChange(item) {
      if(!item.major_id){
        item.major_id = "";
      }
      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.requestArray(this.HISTROY_SCORE_URL, {
        ticket_no : item.ticket_no, 
        major_id : item.major_id
      }, (array) => {
        this.historyScore = array;
        this.orderNum = array.length;
        AppComponent.GobalComponent.setLoading(false);
      },(error)=>{
        AppComponent.GobalComponent.setLoading(false);
      }
     );
      
    }




  formateOrignScore(item) {
    if (item.score_source == '顶替' || item.origin_score == '888') {
      return '顶替';
    } else if (item.score_source == '免考' || item.origin_score == '999') {
      return '免考';
    }
    if (item.origin_score == '-9') {
      return '缺考';
    } else if (item.origin_score == '-3') {
      return '违纪';
    } else if (item.origin_score == '-2') {
      return '免考';
    } else if (item.origin_score == '-1') {
      return '舞弊';
    }
    return item.origin_score;
  }

  formateFinalScore(item) {
    if (item.score_source == '顶替' || item.final_score == '888') {
      return '顶替';
    } else if (item.score_source == '免考' || item.final_score == '999') {
      return '免考';
    }
    if (item.final_score == '-9') {
      return '缺考';
    } else if (item.final_score == '-3') {
      return '违纪';
    } else if (item.final_score == '-2') {
      return '免考';
    } else if (item.final_score == '-1') {
      return '舞弊';
    }
    return item.final_score;
  }

  

  
  downloadByScoreTable(id) {

      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.download("by/byApply/downloadByScoreTable.htm",  {ids: id}, ()=>{
          AppComponent.GobalComponent.setLoading(false);
          AppComponent.GobalComponent.successMsg("下载成功!");
      });

  }

  downloadStdMajorScoreTable(item) {
    if (!item.major_id) {
      AppComponent.alert({title:"信息提示",text:"请选择专业!"});
      return;
    }

    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("by/byApply/downloadStdScoreTable.htm",  {std_id:item.id,major_id:item.major_id}, ()=>{
        AppComponent.GobalComponent.setLoading(false);
        AppComponent.GobalComponent.successMsg("下载成功!");
    });

}



  changeLi(event,index,status){
    //点击时，将索引值赋值给变量isActive可以实现点击切换样式
    this.isActive = index;
    
  }

}