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";

@Component({
  selector: "certificate-query",
  templateUrl: "./certificate-query.component.html",
  styleUrls: ["./certificate-query.component.scss"]
})
export class CertificateQueryComponent extends FlexTableComponent implements OnInit {

	logOrgType: any;
  //TODO
  QUERY_URL = "by/byCertificate/page.htm?status=3";
  DELETE_URL = "by/byCertificate/delete.htm";
  GET_URL = "by/byCertificate/get.htm";
  ADD_URL = "by/byCertificate/add.htm";
  EDIT_URL = "by/byCertificate/edit.htm";


  orgList = [];
  majorList = [];
  masterSchoolList = [];
  constructor(
    public bsModalService: BsModalService,
    fb: FormBuilder
  ) {
    super();

  }
  public query = {
	query: "",
	status: "",
    cityOrgId: "",
    masterSchoolId: "",
    majorId: "",
	std_source:'',
	examTimeId:'',
	startTime:"",
	endTime:""
  };
  ngOnInit() {
	FrameRequest.requestMap("by/byApply/initdata.htm", {}, async (map) => {
		this.majorList  = map.majorList;//专业
		this.orgList = map.cityOrgList;//市考办
		this.masterSchoolList = map.schoolOrgList;//主考学校
		this.query.examTimeId = map.examTimeId;
		this.logOrgType=map.logOrgType;
		this.framePager.reload();
	});
  }



  search(param) {
    param.query = this.query.query;
    if (!this.query.cityOrgId) {
      this.query.cityOrgId = "";
	}
	if (!this.query.std_source) {
		this.query.std_source = "";
	  }
    if (!this.query.majorId) {
      this.query.majorId = "";
    }
    if (!this.query.masterSchoolId) {
      this.query.masterSchoolId = "";
    }
	if (!this.query.examTimeId) {
		this.query.examTimeId="";
	}

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

	if (!this.query.endTime) {
		this.query.endTime="";
	}
	
	param.examTimeId = this.query.examTimeId;
	param.city_org_id = this.query.cityOrgId;
	param.std_source = this.query.std_source;
    param.major_id = this.query.majorId;
		param.school_id = this.query.masterSchoolId;
		param.startTime = this.query.startTime;
		param.endTime = this.query.endTime;
  }

  

  getCertificatePdf() {
	if (this.logOrgType!=0) {
		AppComponent.alert({title:"信息提示",text:"只允许区考试院打印!"}); 
		return;
	} 

  let checkedArray = this.framePager.getCheckedArray();
	if (checkedArray.length == 0) {
		AppComponent.alert({ title: "信息提示", text: "请至少选择一条记录!" });
		return;
	}
	checkedArray = this.framePager.getCheckedArray((item)=>item.id);
	AppComponent.GobalComponent.setLoading(true);
	FrameRequest.download("by/byCertificate/getCertificatePdf.htm", {ids:checkedArray.join()}, ()=>{
		AppComponent.GobalComponent.setLoading(false);
		AppComponent.GobalComponent.successMsg("下载成功!");
	  });
	}
	
	getAllCertificatePdf() {
		if (!this.framePager.records||this.framePager.records.length==0) {
			AppComponent.alert({title:"信息提示",text:"没有审核完成的数据!"}); 
			return;
		} 
	
		if (!this.query.examTimeId) {
			AppComponent.alert({title:"信息提示",text:"请选择毕业批次!"}); 
			return;
		}
	
		if (this.logOrgType!=0) {
			AppComponent.alert({title:"信息提示",text:"只允许区考试院打印!"}); 
			return;
		} 
	
			if (!this.query.cityOrgId) {
				this.query.cityOrgId = "";
		}
		if (!this.query.std_source) {
			this.query.std_source = "";
			}
			if (!this.query.majorId) {
				this.query.majorId = "";
			}
			if (!this.query.masterSchoolId) {
				this.query.masterSchoolId = "";
			}

			if (!this.query.startTime) {
				this.query.startTime="";
			}
		
			if (!this.query.endTime) {
				this.query.endTime="";
			}
		
		let parm = {
			examTimeId : this.query.examTimeId,
			city_org_id : this.query.cityOrgId,
			std_source : this.query.std_source,
			major_id : this.query.majorId,
			school_id : this.query.masterSchoolId,
			query:this.query.query,
			startTime:this.query.startTime,
			endTime:this.query.endTime
		}
		
		AppComponent.GobalComponent.setLoading(true);
		FrameRequest.download("by/byCertificate/getAllCertificatePdf.htm", parm, ()=>{
			AppComponent.GobalComponent.setLoading(false);
			AppComponent.GobalComponent.successMsg("下载成功!");
			});
		}

  getApplyTable() {
	let checkedArray = this.framePager.getCheckedArray();
	if (checkedArray.length == 0) {
		AppComponent.alert({ title: "信息提示", text: "请至少选择一条记录!" });
		return;
	}
	checkedArray = this.framePager.getCheckedArray((item)=>item.id);
	AppComponent.GobalComponent.setLoading(true);
	FrameRequest.download("by/byCertificate/getApplyTable.htm", {ids:checkedArray.join()}, () => {
		AppComponent.GobalComponent.setLoading(false);
		AppComponent.GobalComponent.successMsg("下载成功!");
	});
  
}
}