import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { BsModalService } from "ngx-bootstrap/modal";
import { FlexTableComponent } from "../../../../../shared/common/FlexTable.component";
import { AppComponent } from "../../../../../app.component";
import { FrameRequest } from "../../../../../shared/frame/frame-request";
import { FramePagerComponent } from "../../../../../core/pager/pager.component";
import {PhotoUploaderComponent} from '../../../common/modal/photo-uploader/photo-uploader.component';
import { FrameFunction } from "../../../../../shared/frame/frame_function";
import { FrameValidator } from "../../../../../shared/frame/Frame-validator";
import * as _ from 'lodash';

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


	@ViewChild('byFramePager')
	byFramePager: FramePagerComponent;
	@ViewChild('uploadPhoto')
	uploadPhoto: PhotoUploaderComponent;

	logOrgType: string;
	courseList = [];
	//TODO
	QUERY_URL = "by/byCertificate/page.htm";
	DELETE_URL = "by/byCertificate/delete.htm";
	GET_URL = "by/byCertificate/get.htm";
	ADD_URL = "by/byCertificate/add.htm";
	EDIT_URL = "by/byCertificate/edit.htm";

	AUTH_URL = "by/byCertificate/auth.htm";

	orgList = [];
	majorList = [];
	masterSchoolList = [];
	url: any;
	photo_url: any;

	constructor(
		public bsModalService: BsModalService,
		fb: FormBuilder
	) {
		super();
		this.formModel = fb.group({
			id: [null],
			flag: [null],
			remark: [null],
			std_name: [null, Validators.compose([Validators.required])],//非特殊字符
			cert_no: [null, Validators.compose([Validators.required])],
			diaplma_no: [null, Validators.compose([Validators.required])],
			ticket_no: [null],
			major: [null, Validators.compose([Validators.required])],
			school: [null, Validators.compose([Validators.required])],
			by_date: [null, Validators.compose([Validators.required])],
			// std_source: [null, Validators.compose([Validators.required])],
			major_level: [null, Validators.compose([Validators.required])],
			phone: [null, Validators.compose([Validators.required])],
			reason: [null, Validators.compose([Validators.required])],
		});
	}

	ngOnInit() {
		FrameRequest.requestMap("by/byCertificate/initdata.htm", {}, async (map) => {
			this.majorList = map.majorList;//专业
			this.orgList = map.cityOrgList;//市考办
			this.masterSchoolList = map.schoolOrgList;//主考学校
			this.query.examTimeId = map.examTimeId;
			this.byQuery.examTimeId = map.examTimeId;
			this.logOrgType = map.logOrgType;
			this.url = map.url;
			this.photo_url = map.photo_url;

			this.framePager.reload();
		});

	}

	changeExamTimeId(examTimeId) {
		if(examTimeId){
			FrameRequest.requestMap("by/byCertificate/initdata.htm", {exam_time_id:examTimeId}, async (map) => {
				this.orgList = map.cityOrgList;//市考办
				
			});
		}
		
	  }

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

	search(param) {
		param.query = this.query.query;
		if (!this.query.cityOrgId) {
			this.query.cityOrgId = "";
		}
		if (!this.query.school) {
			this.query.school = "";
		}
		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.status) {
			this.query.status = "";
		}
		if (!this.query.examTimeId) {
			this.query.examTimeId = "";
		}

		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.status = this.query.status;
		param.school = this.query.school;
	}
	//审核
	doAudit() {
		// debugger
		// if (!FrameFunction.validForm(this.formModel)) {
		// 	return;
		// }
		if (!this.item.flag) {
			AppComponent.alert({ title: "信息提示", text: "请选择审批类型!" });
			return;
		}

		if (_.isUndefined(this.item.id)) delete this.item.id;
		//循环判断元素是否undefined
		for (const key in this.item) {
			if (this.item[key] === undefined) {
				this.item[key] = "";
			}
		}
		AppComponent.GobalComponent.setLoading(true);
		FrameRequest.request("by/byCertificate/audit.htm", this.item, async (ret) => {
			AppComponent.GobalComponent.setLoading(false);
			if (ret.errorCode == 1) {
				this.hideModal();
				this.framePager.reload();
				AppComponent.GobalComponent.successMsg("提交成功!");
			}else{
				
				AppComponent.GobalComponent.errorMsg(ret.errorMsg);
			}
		});
	}


	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("下载成功!");
		});

	}

	//弹出审核框
	showEditWin(obj) {
		
		this.item = obj;

		if (!obj) {
			let checkedArray = this.framePager.getCheckedArray();
			if (checkedArray.length == 0 || checkedArray.length > 1) {
				AppComponent.alert({ title: "信息提示", text: "请选择一条待审批记录!" });
				return;
			}

			this.item = checkedArray[0];
		}
		//if (this.logOrgType == '0' && this.item.status != 2) {
		//	AppComponent.alert({ title: "信息提示", text: "请选择待自考处审核记录!" });
		//	return;
		//} else if (this.logOrgType != '0' && this.item.status != 1) {
		//	AppComponent.alert({ title: "信息提示", text: "请选择待受理单位审核记录!" });
		//	return;
		//}
		this.editShowType = "only";
	}
	async preShowModalWin() {
		this.item.remark = '';
		return true;
	}

	callBackApply() {
		let checkedArray = this.framePager.getCheckedArray();
		if (checkedArray.length == 0 || checkedArray.length > 1) {
			AppComponent.alert({ title: "信息提示", text: "请选择一条记录!" });
			return;
		}
		// 考试院撤回
		
		if (this.logOrgType == '0') {
			if (checkedArray[0].status != '2'&&checkedArray[0].status != '0'&&checkedArray[0].status != '3') {
				AppComponent.alert({ title: "信息提示", text: "只能撤回状态是待考试院审核、审核通过、审核不通过的记录!" });
				return;
			}
		} else {
			//if (checkedArray[0].status != '2') {
			//	AppComponent.alert({ title: "信息提示", text: "只能撤回状态是待考试院审核的记录!" });
			//	return;
		//	}
			
		}
		AppComponent.confirm("确定撤回申请单吗?", () => {

			AppComponent.GobalComponent.setLoading(true);
			FrameRequest.request("by/byCertificate/callBack.htm", { id: checkedArray[0].id }, (ret) => {
				AppComponent.GobalComponent.setLoading(false);
				this.hideModal();
				this.framePager.reload();
				if (ret.errorCode == 1) {

					AppComponent.GobalComponent.successMsg("撤回成功!");
				} else {

					AppComponent.GobalComponent.errorMsg(ret.errorMsg);
				}
			});
		});
	}
	showByChooseDialog(templateModal) {
		this.bsModalWin = this.bsModalService.show(templateModal, { class: "modal-lg" });
	}

	BY_QUERY_URL = "by/byApply/queryPage.htm?by_status=5";
	UPLOAD_URL = "pl/byCertificate/upload.htm";
	public byQuery = {
		query: "",
		examTimeId: "",
		masterSchoolId: "",
		majorId: "",
		std_source: "",
	};
	byFrameSearch(param) {
		param.query = this.byQuery.query;
		if (!this.byQuery.std_source) {
			this.byQuery.std_source = "";
		}
		if (!this.byQuery.majorId) {
			this.byQuery.majorId = "";
		}
		if (!this.byQuery.masterSchoolId) {
			this.byQuery.masterSchoolId = "";
		}
		if (!this.byQuery.examTimeId) {
			this.byQuery.examTimeId = "";
		}

		param.exam_time_id = this.byQuery.examTimeId;
		param.std_source = this.byQuery.std_source;
		param.major_id = this.byQuery.majorId;
		param.school_id = this.byQuery.masterSchoolId;

	}
	byFramePagerReset() {
		for (let key in this.byQuery) {
			//console.log(key);
			if (this.byQuery.hasOwnProperty(key)) {
				this.byQuery[key] = "";
			}
		}
	}

	async preSaveEvent() {
		
		if (!this.item.id&&!this.item.uploadAready&&this.needUpload(this.item)) {
			this.uploadPhoto.show();
			return false;
		}
		return true;
	}

	needUpload(row) {
		return (!row.apply_date||parseInt(row.apply_date.substr(0, 4)) <= 2004) || (!row.by_photo && !row.uploadAready && !row.photo_url)
	}

	afterUpload(respObj) {
		if (respObj.success) {
			this.item.by_photo = respObj.map.file_path;
			this.item.by_photo_path = respObj.map.by_photo_path;
			this.item.by_photo_id = respObj.map.by_photo_id;
			this.item.uploadAready = true;
			this.save();
			this.uploadPhoto.hideModal();
			if (this.bsModalWin) this.bsModalWin.hide();
		} else {
			AppComponent.alert({ 'title': '提示', 'text': respObj.errorMsg });
		}
	}

	showManualDialog(templateModal, item?) {
		if (!item) {
			this.item = {};
		} else {
			this.item = item;
			this.item.app_id = item.id;
			this.item.id = "";
			this.item.remark = "";
			this.item.major = item.major_name
			this.item.by_date = item.by_date ? item.by_date.split(" ")[0] : null;
			this.item.school = item.school_name;
			this.item.by_photo_path = item.photo_path;
			this.item.by_photo_id = item.photo_id;
		}
		if (this.bsModalWin) this.bsModalWin.hide();
		this.bsModalWin = this.bsModalService.show(templateModal, { class: "modal-lg" });
	}

	
	export() {
	
		if (!this.query.cityOrgId) {
			this.query.cityOrgId = "";
		}
		if (!this.query.status) {
			this.query.status = "";
		}
		if (!this.query.examTimeId) {
			this.query.examTimeId = "";
		}
		
		if (!this.query.school) {
			this.query.school = "";
		}

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

		let params = {
			examTimeId: this.query.examTimeId,
			status: this.query.status,
			city_org_id: this.query.cityOrgId,
			school: this.query.school,
			query: this.query.query
		};
		
		AppComponent.GobalComponent.setLoading(true);
		FrameRequest.download("by/byCertificate/handoverTableExport.htm", params, () => {
		  AppComponent.GobalComponent.setLoading(false);
		  AppComponent.GobalComponent.successMsg("导出成功");
		});
	  }

}