import * as asyncFetch from '../apis/importresource';
import {
	observable,
	action,
	extendObservable
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	message
} from 'antd';
import {
	hrmHolidySettingNew
} from './holidaySettingNew';
import {
	hrmVacationBalance
} from './vacationBalance';
const getLabel = WeaLocaleProvider.getLabel;
const {
	TableStore
} = WeaTableNew;

class HrmImportCommon {
	@observable importType = '';
	@observable importDialog = {
		visible: false,
		condition: [],
		title: '',
		loading: true,
		templetName: '',
		selectedValue: 'workcode',
	}

	@observable resultDialog = {
		title: observable.ref(getLabel(82341, '导入结果')),
		errorTip: observable.ref(getLabel(529729, '导入文件存在以下错误，请解决后重新上传！')),
		visible: false,
		index: 1,
		datas: [],
		importStatus: '',
		pId: '',
		interval: '',
		tableStore: new TableStore(),
		errorInfo: [],
		succnum: 0,
		failnum: 0,
		loading: true,
		component: '',
	}

	@observable recordDialog = {
		title: observable.ref(getLabel(24644, '历史导入记录')),
		visible: false,
		tableStore: new TableStore(),
	}

	@observable logDialog = {
		title: observable.ref(getLabel(24835, '导入日志')),
		visible: false,
		tableStore: new TableStore(),
		isPanelShow: false,
		form: new WeaForm(),
		condition: [],
		loading: true,
	}

	@action
	getImportForm = () => {
		this.importDialog.loading = true;
		let params = {
			importType: this.importType
		}
		if (this.importDialog.formAsyncParams) {
			Object.assign(params, {
				...this.importDialog.formAsyncParams
			});
		}
		asyncFetch.getImportForm(params).then(rs => {
			const {
				status,
				condition = [],
			} = rs;

			if (status == '1') {
				this.importDialog.condition = condition;
				this.importDialog.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getImportProcessInfo = () => {
		let params = {
			importType: this.importType,
			index: this.resultDialog.index,
		};
		asyncFetch.getImportProcessLog(params).then(rs => {
			const {
				datas,
				index,
				pId = '',
				status
			} = rs;

			this.resultDialog.loading = false;
			if (status == '1') {
				const {
					importStatus,
					interval,
					errorInfo,
					component
				} = this.resultDialog;

				component.scrollToLast();

				if (importStatus == 'over' || importStatus == 'error') {
					clearInterval(interval);
					if (!this.hasErrorInfo(errorInfo)) {
						this.getResultInfo();

						switch (this.importType) {
							case 'holidaySet':
								hrmHolidySettingNew.refreshContent();
								break;
							case 'balanceOfLeave':
								hrmVacationBalance.resetTableEditableDatas();
								hrmVacationBalance.getSearchList();
								break;
						}
					}
					return
				}

				if (rs.importStatus == 'over' || rs.importStatus == 'error') {
					this.resultDialog.loading = true;
				}

				this.resultDialog.importStatus = rs.importStatus;

				if (pId) this.resultDialog.pId = pId;

				if (this.resultDialog.datas.length == 0) {
					this.resultDialog.datas = datas;
				} else {
					this.resultDialog.datas = [...this.resultDialog.datas, ...datas]
				}

				this.resultDialog.index = index;
			}
		});
	}

	@action
	getResultInfo = () => {
		let params = {
			pId: this.resultDialog.pId
		};
		this.resultDialog.loading = true;
		asyncFetch.getImportResult(params).then(rs => {
			const {
				datas,
				status
			} = rs, {
				succnum,
				failnum,
				sessionkey = ''
			} = datas;

			if (status == '1') {
				this.resultDialog.succnum = succnum;
				this.resultDialog.failnum = failnum;
				sessionkey && this.resultDialog.tableStore.getDatas(sessionkey, 1);
			}
			this.resultDialog.loading = false;
		});
	}

	@action
	getRecordTable = () => {
		let params = {
			importType: this.importType
		};
		asyncFetch.getImportHistory(params).then(rs => {
			const {
				status,
				sessionkey,
			} = rs;

			if (status == '1') {
				this.recordDialog.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getLogTable = () => {
		let params = {
			importType: this.importType,
			...this.logDialog.form.getFormParams(),
		};
		if (this.resultDialog.pId) {
			params = {
				...params,
				pId: this.resultDialog.pId
			}
		}
		asyncFetch.getImportLogDetail(params).then(rs => {
			const {
				status,
				sessionkey
			} = rs;

			if (status == '1') {
				this.logDialog.tableStore.getDatas(sessionkey, 1);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	submitImportInfo = () => {
		let params = {
			importType: this.importType,
			excelfile: this.importDialog.excelFileId,
			operateType: 'add'
		};
		if (this.importType == 'group') {
			params = {
				...params,
				keyField: this.importDialog.selectedValue
			}
		}
		if (this.importDialog.formAsyncParams) {
			Object.assign(params, {
				...this.importDialog.formAsyncParams
			});
		}
		this.setResultInfoDialogVisible(true);
		asyncFetch.saveImport(params).then(rs => {
			const {
				status,
				errorInfo
			} = rs;

			if (status == '1') {
				if (this.hasErrorInfo(errorInfo)) {
					this.resultDialog.errorInfo = errorInfo;
					this.resultDialog.importStatus = 'error';
				}
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
		this.resultDialog.interval = setInterval(() => this.getImportProcessInfo(), 2000);
	}

	@action
	getLogSearchPanelCondition = () => {
		this.logDialog.loading = true;
		let params = {
			importType: this.importType
		};
		asyncFetch.getImportLogSearchCondition(params).then(rs => {
			const {
				status,
				searchCondition
			} = rs;

			if (status == '1') {
				this.logDialog.condition = searchCondition;
				this.logDialog.form.initFormFields(searchCondition);
			} else {
				message.error(rs.message);
			}
			this.logDialog.loading = false;
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	setExcelFileNo = (id) => {
		extendObservable(this.importDialog, {
			excelFileId: id
		});
	}

	@action
	submitExcel = () => {
		if (!this.importDialog.excelFileId) {
			message.warning(getLabel(385362, '请选择导入模板!'));
			return;
		}
		this.submitImportInfo();
	}

	@action
	queryRecord = () => {
		this.setRecordDialogVisible(true);
	}

	@action
	queryLog = () => {
		this.setLogDialogVisible(true);
	}

	@action
	jumpToImportResult = (id) => {
		this.resultDialog.pId = id;
		this.resultDialog.importStatus = 'over';
		this.setResultInfoDialogVisible(true);
		this.getResultInfo();
	}


	@action
	setImportDialogVisible = (bool) => {
		this.importDialog.visible = bool;
		if (bool) {
			this.getImportForm();
		} else {
			this.importDialogReset();
		}

	}

	@action
	setResultInfoDialogVisible = (bool) => {
		this.resultDialog.visible = bool;
		if (!bool) {
			clearInterval(this.resultDialog.interval);
			this.resetResultDialogStatus();
		}
	}

	@action
	importDialogReset = () => {
		this.importDialog.condition = [];
		this.importDialog.selectedValue = 'workcode';
		this.importDialog.excelFileId = '';
	}

	@action
	resetResultDialogStatus = () => {
		this.resultDialog.index = 1;
		this.resultDialog.datas = [];
		this.resultDialog.importStatus = '';
		this.resultDialog.pId = '';
		this.resultDialog.interval = '';
		this.resultDialog.errorInfo = '';
		this.resultDialog.loading = true;
	}

	@action
	setRecordDialogVisible = (bool) => {
		this.recordDialog.visible = bool;
		bool && this.getRecordTable();
	}

	@action
	setLogDialogVisible = (bool) => {
		this.logDialog.visible = bool;
		bool && this.getLogTable();
		if (!bool) {
			this.logDialog.isPanelShow = false;
			this.logDialog.form = new WeaForm();
			this.logDialog.condition = [];
		}
	}

	@action
	setLogPanelStatus = (bool) => {
		this.logDialog.isPanelShow = bool;
		bool && this.logDialog.condition.length == 0 && this.getLogSearchPanelCondition();
	}

	@action
	searchLogInfo = () => {
		this.getLogTable();
	}

	hasErrorInfo = (errorInfo) => {
		if (errorInfo.length == 0) return false;
		return true
	}

	@action
	setTempletName = (templetName) => {
		this.importDialog.templetName = templetName;
	}

	@action
	setImportDialogTitle = (title) => {
		this.importDialog.title = title;
	}

	@action
	setImportType = (type) => {
		this.importType = type;
	}

	@action setFormAsyncParams = (params) => {
		this.importDialog.formAsyncParams = params
	}

	@action
	setSelectedValue = (value) => {
		this.importDialog.selectedValue = value;
	}

	@action
	setScoll = (component) => {
		this.resultDialog.component = component;
	}

}

const hrmImportCommon = new HrmImportCommon();

export {
	hrmImportCommon
}