import { Component, OnInit, ViewChild, TemplateRef } from '@angular/core';
import { FlexTableComponent } from '../../../../../shared/common/FlexTable.component';
import { AppComponent } from '../../../../..';
import { FrameRequest } from '../../../../../shared/frame/frame-request';
import { BsModalService } from 'ngx-bootstrap/modal';
import { FormBuilder, Validators } from '@angular/forms';
import { FrameUploaderComponent } from '../../../../../core/uploader/uploader.component';

@Component({
  selector: 'app-kw-security-print',
  templateUrl: './kw-security-print.component.html',
  styleUrls: ['./kw-security-print.component.scss']
})
export class KwSecurityPrintComponent extends FlexTableComponent implements OnInit {

  query = {
    exam_time_id:"",
    dishidaiha: "",
    kaoqudaiha:"",
    pj_type:"",
    kechengdai:"",
    kaodiandai:"",
    kaoshicish:""
  }

  QUERY_URL = "kw/kwLayoutDetail/querySecurityPage.htm";

  cityList = [];
  timeList = [];
  schoolList = [];
  courseList = [];
  pointList = [];
  cishList = [];


  currentYearCode = "";
  flag = false;
  filePath: any;//错误文件路径
  modalWin: any;
  yearCode: any;
  course_yearCode: any;
  historyYearcode: any;

  @ViewChild('uploader')
  uploader: FrameUploaderComponent;

  constructor(public bsModalService: BsModalService, fb: FormBuilder) {
    super();
    this.formModel = fb.group({
      id: [null],
      year_code: [null, Validators.required],
      pj_school_id: [null, Validators.required],
      course_id: [null, Validators.required],
    });
  }

  ngOnInit() {
    FrameRequest.requestMap("kw/kwLayoutDetail/initSecurityData.htm", {}, async (map) => {
			this.cityList = map.dishiList;//地市
      this.timeList = map.timeList;//批次列表
      this.query.exam_time_id = map.exam_time_id;//当前批次
      this.schoolList = map.schoolList;//考区
      this.courseList = map.courseList;//课程列表
      this.cishList = map.cishList //场次列表
      //this.framePager.reload();
		});
  }

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

    if (this.query.dishidaiha === null) {
      this.query.dishidaiha = "";
    }

    if (this.query.kaoqudaiha === null) {
      this.query.kaoqudaiha = "";
    }

    if (this.query.pj_type === null) {
      this.query.pj_type = "";
    }

    if (this.query.kechengdai === null) {
      this.query.kechengdai = "";
    }

    if (this.query.kaodiandai === null) {
      this.query.kaodiandai = "";
    }

    if (this.query.kaoshicish === null) {
      this.query.kaoshicish = "";
    }
    
    
    param.exam_time_id = this.query.exam_time_id;
    param.dishidaiha = this.query.dishidaiha;
    param.kaoqudaiha = this.query.kaoqudaiha;
    param.pj_type = this.query.pj_type;
    param.kechengdai = this.query.kechengdai;
    param.kaodiandai = this.query.kaodiandai;
    param.kaoshicish = this.query.kaoshicish;
    
  }


   //生成保密号报表
   buildSecurity() {
    if (!this.query.exam_time_id) {
      AppComponent.alert({ 'title': '提示', 'text': '请选择批次' });
      return;
    }
   
    AppComponent.confirm("确定生成保密号数据吗?", () => {
      AppComponent.GobalComponent.setLoading(true);
      FrameRequest.request("kw/kwLayoutDetail/buildSecurity.htm", { exam_time_id: this.query.exam_time_id }, (ajax) => {
        AppComponent.GobalComponent.setLoading(false);
        if (ajax.errorCode == -1) {
          AppComponent.alert({ 'title': '提示', 'text': ajax.errorMsg });
        } else {
          AppComponent.GobalComponent.successMsg("保密号数据生成成功");
          
        }
      });
    });

  }

  changetime(){
    
    if(this.query.exam_time_id){
      FrameRequest.requestMap("kw/kwLayoutDetail/initSecurityData.htm", {exam_time_id:this.query.exam_time_id}, async (map) => {
        this.cityList = map.dishiList;//地市
        this.schoolList = null;//考区
        this.courseList = map.courseList;//课程列表
        this.pointList = null;
        this.cishList = map.cishList //场次列表
        this.query.dishidaiha = "";
        this.query.kaoqudaiha = "";
        this.query.kaodiandai = "";
        this.query.kaoshicish = "";
        this.query.kechengdai = "";
      });
    }
    
  }

  changedishi(){
    if(this.query.exam_time_id && this.query.dishidaiha){
      FrameRequest.requestMap("kw/kwLayoutDetail/initSecurityData.htm", {exam_time_id:this.query.exam_time_id,dishidaiha:this.query.dishidaiha}, async (map) => {
        this.schoolList = map.schoolList;//考区
        this.pointList = null;
        this.query.kaoqudaiha = "";
        this.query.kaodiandai = "";
      });
    }
    
  }

  changekaoqu(){
    if(this.query.exam_time_id && this.query.dishidaiha && this.query.kaoqudaiha){
      FrameRequest.requestMap("kw/kwLayoutDetail/initSecurityData.htm", {exam_time_id:this.query.exam_time_id,dishidaiha:this.query.dishidaiha,kaoqudaiha:this.query.kaoqudaiha}, async (map) => {
        this.pointList = map.pointList;
        this.query.kaodiandai = "";
      });
    }
    
  }


  exportSecurityCodeOnePdf(one) {
    let id = "";
    if('one' == one){
      let checkedArray = this.framePager.getCheckedArray();
      if(checkedArray.length == 0 || checkedArray.length > 1){
        AppComponent.alert({title:"信息提示",text:"请选择一条记录!"})
        return;
      }
      id = checkedArray[0].id;
    }else{
      if (!this.query.exam_time_id) {
        AppComponent.alert({ title: "信息提示", text: "请选择批次!" })
        return;
      }
    }
    
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/exportSecurityCodeOnePdf.htm", {id:id,exam_time_id:this.query.exam_time_id,dishidaiha:this.query.dishidaiha,kaoqudaiha:this.query.kaoqudaiha,pj_type:this.query.pj_type,kechengdai:this.query.kechengdai,kaodiandai:this.query.kaodiandai,kaoshicish:this.query.kaoshicish}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("考场信息表导出成功");
    });
  }

  exportSecurityCodeTwoPdf(one) {
    let id = "";
    if('one' == one){
      let checkedArray = this.framePager.getCheckedArray();
      if(checkedArray.length == 0 || checkedArray.length > 1){
        AppComponent.alert({title:"信息提示",text:"请选择一条记录!"})
        return;
      }
      id = checkedArray[0].id;
    }else{
      if (!this.query.exam_time_id) {
        AppComponent.alert({ title: "信息提示", text: "请选择批次!" })
        return;
      }
    }
    
    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/exportSecurityCodeTwoPdf.htm", {id:id,exam_time_id:this.query.exam_time_id,dishidaiha:this.query.dishidaiha,kaoqudaiha:this.query.kaoqudaiha,pj_type:this.query.pj_type,kechengdai:this.query.kechengdai,kaodiandai:this.query.kaodiandai,kaoshicish:this.query.kaoshicish}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("考场登记表导出成功");
    });
  }

  exportSecurityCodeThreePdf(one) {
    let id = "";
    if('one' == one){
      let checkedArray = this.framePager.getCheckedArray();
      if(checkedArray.length == 0 || checkedArray.length > 1){
        AppComponent.alert({title:"信息提示",text:"请选择一条记录!"})
        return;
      }
      id = checkedArray[0].id;
    }else{
      if (!this.query.exam_time_id) {
        AppComponent.alert({ title: "信息提示", text: "请选择批次!" })
        return;
      }
    }
    

    AppComponent.GobalComponent.setLoading(true);
    FrameRequest.download("kw/nomal/exportSecurityCodeThreePdf.htm", {id:id,exam_time_id:this.query.exam_time_id,dishidaiha:this.query.dishidaiha,kaoqudaiha:this.query.kaoqudaiha,pj_type:this.query.pj_type,kechengdai:this.query.kechengdai,kaodiandai:this.query.kaodiandai,kaoshicish:this.query.kaoshicish}, () => {
      AppComponent.GobalComponent.setLoading(false);
      AppComponent.GobalComponent.successMsg("保密号标签导出成功");
    });
  }


}
