import { Component, OnInit, TemplateRef, ViewChild, ViewContainerRef } from "@angular/core";
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../shared/common/FlexTable.component";
import {FrameUploaderComponent} from '../../../../core/uploader/uploader.component';
import { AppComponent } from "../../../../app.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-std-retrieval",
  templateUrl: "./stdretrieval.component.html",
  styleUrls: ["./stdretrieval.component.scss"]
})
export class StdRetrievalComponent extends FlexTableComponent
  implements OnInit {

  @ViewChild('framePager')
  framePager:FramePagerComponent;
  @ViewChild('recordPager')
  recordPager:FramePagerComponent;
  @ViewChild('mkPager')
  mkPager:FramePagerComponent;
  @ViewChild('nmkPager')
  nmkPager:FramePagerComponent;
  @ViewChild('turnoutPager')
  turnoutPager:FramePagerComponent;
  @ViewChild('turninPager')
  turninPager:FramePagerComponent;
  @ViewChild('byPager')
  byPager:FramePagerComponent;
  
  @ViewChild('auditModal')
  auditModal:TemplateRef<any>;

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;



  isActive=0;

  public query = {
    query:"",
    std_reg_id:"",
    ticket_no:"",
    status:"",
    exam_time_id:""
  };


  queryWin: any;

  centralScore: any;
  historyScore: any;

  score_count=0;
  record_count=0;
  mk_count=0;
  turn_count=0;
  by_count=0;

  timeList = [];


  QUERY_URL = "std/stdRegInfo/pageForRetrieval.htm";
  GET_URL = "std/stdRegInfo/get.htm";
  EXAM_TIME_URL = "pl/plExamTime/list.htm";


  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    
  }

  ngOnInit() {
    FrameRequest.requestArray(this.EXAM_TIME_URL, {}, (array) => {
      this.timeList = array;
    });

  }

  searchData(templateModal){
    if (!this.query.query) {
      AppComponent.alert({title:"信息提示",text:"请输入关键字!"});
      return false;
    }

    FrameRequest.requestEntity("std/stdRegInfo/pageForRetrievalSize.htm", {
      query : this.query.query,
      start : 0 ,
      limit : 10 
     
    }, (entity) => {
      
      if(entity==-1){
        AppComponent.alert({title:"信息提示",text:"请输入关键字"});
        return false;
      }else if(entity==-2){
        AppComponent.alert({title:"信息提示",text:"没有可查询的记录"});
        return false;
      }else if(entity==2){
        this.queryWin = this.bsModalService.show(templateModal, { class: "modal-lg" });
        
      }else{
        this.goStdDetail(entity.id,'0');
      }
      
    });


    
    
    
    
  }

  search(param){
    if (!this.query.query) {
      this.query.query="";
    }

    if (!this.query.std_reg_id) {
      this.query.std_reg_id="";
    }
   
    param.query=this.query.query;
    param.std_reg_id=this.query.std_reg_id;

    if (!this.query.query) {
      AppComponent.alert({title:"信息提示",text:"请输入关键字!"});
      return false;
    }
   
  }

  
  recordsearch(param){
    if (!this.query.ticket_no) {
      param.query='000000000000';
      return false;
    }else{
      param.query=this.query.ticket_no;
      param.status=3;
    }
   
  }

  mksearch(param){
    if (!this.query.ticket_no) {
      param.ticket_no='000000000000';
      return false;
    }else{
      param.ticket_no=this.query.ticket_no;
      param.status=3;
    }
   
  }

  nmksearch(param){
    if (!this.query.ticket_no) {
      param.ticket_no='000000000000';
      return false;
    }else{
      param.ticket_no=this.query.ticket_no;
      param.status=3;
    }
   
  }

  turnoutsearch(param){
    if (!this.query.ticket_no) {
      param.query='000000000000';
      return false;
    }else{
      param.query=this.query.ticket_no;
      param.status=6;
    }
   
  }

  turninsearch(param){
    if (!this.query.ticket_no) {
      param.query='000000000000';
      return false;
    }else{
      param.query=this.query.ticket_no;
      param.status=5;
    }
   
  }

  bysearch(param){
    if (!this.query.ticket_no) {
      param.query='000000000000';
      return false;
    }else{
      param.query=this.query.ticket_no;
      param.by_status=5;
    }
   
  }
  


  


  //进入详情
  goStdDetail(id,editShowType) {
    FrameRequest.requestEntity(this.GET_URL,{id: id}, async (entity) => {
      if(entity) {
        this.item = entity;
        
        this.query.std_reg_id = this.item.id;
        this.query.ticket_no = this.item.ticket_no;
      }

     if(this.queryWin){
      this.queryWin.hide();
     }
      
      this.editShowType = editShowType;
      this.isActive=0;
      this.query.exam_time_id="";

      FrameRequest.requestMap("/std/stdRegInfo/getStdScoreTotal.htm",{std_reg_id:this.query.std_reg_id,ticket_no:this.query.ticket_no,status:3,flag:0}, async (resp) => {
        this.score_count=resp.score_count;
       
      });

      FrameRequest.requestMap("/std/stdRegInfo/getStdRecordTotal.htm",{std_reg_id:this.query.std_reg_id,ticket_no:this.query.ticket_no,status:3}, async (resp) => {
       
        this.record_count=resp.record_count;
       
      });

      FrameRequest.requestMap("/std/stdRegInfo/getStdMkTotal.htm",{std_reg_id:this.query.std_reg_id,ticket_no:this.query.ticket_no,status:3}, async (resp) => {
        
        this.mk_count=resp.mk_count;
        
      });

      FrameRequest.requestMap("/std/stdRegInfo/getStdTurnTotal.htm",{std_reg_id:this.query.std_reg_id,ticket_no:this.query.ticket_no,status:3}, async (resp) => {
        
        this.turn_count=resp.turn_count;
      });

      FrameRequest.requestMap("/std/stdRegInfo/getStdByTotal.htm",{std_reg_id:this.query.std_reg_id,ticket_no:this.query.ticket_no,status:3}, async (resp) => {
        
        this.by_count=resp.by_count;
      });
      
    });
  
  }


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

  queryScore(){
    if(this.query.std_reg_id){
      if(!this.query.exam_time_id){
        this.query.exam_time_id="";
      }

      FrameRequest.requestArray("kj/kjUnifiedScoreDetail/central.htm", {
        std_reg_id : this.query.std_reg_id,
        exam_time_id:this.query.exam_time_id,
        flag:0
      }, (array) => {
        this.centralScore = array;
      });
    }
    
  }

  querycollegeScore(){
    if(this.query.ticket_no){
      if(!this.query.exam_time_id){
        this.query.exam_time_id="";
      }

      FrameRequest.requestArray("kj/kjUnifiedScoreDetail/collegeScore.htm", {
        ticket_no : this.query.ticket_no,
        exam_time_id:this.query.exam_time_id
      }, (array) => {
        this.historyScore = array;
      });
    }

  }


  
  carouselContentFilter(str) {
    
    if(str){
        var carContent ='';
        if(str.length >= 20){
           
            carContent = str.substring(0,20) + '...';
        }
        else {
            carContent = str
        }
        return carContent;
    }else{
      return str;
    }
}

}