import * as Api from '../apis/trainActivity';
import {
	toJS
} from 'mobx';
import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	message
} from 'antd';
import {
	i18n
} from '../public/i18n';
const {
	TableStore
} = WeaTableNew;

export class HrmTrainActivity {
	@observable main = {
		btnMenu: [],
		authorized: false,
		loading: true,
		title: i18n.module.trainActivity(),
	}
	@observable formInfo = {
		conditions: [],
		form: new WeaForm(),
		loading: true,
		date: '',
	}
	@observable mainTable = {
		tableStore: new TableStore(),
		selectedRowKey: '',
		index: '',
	}
	@observable subTable = {
		tableStore: new TableStore(),
		selectedRowKey: '',
	}
	@observable thirdTable = {
		tableStore: new TableStore(),
	}
	@observable mainDialog = {
		title: '',
		visible: false,
		btnMenu: [],
	}
	@observable subDialog = {
		title: '',
		visible: false,
		btnMenu: [],
	}
	@observable mainTab = {
		curKey: '0',
		isPanelShow: false,
		form: new WeaForm(),
		conditions: [],
		loading: true,
		conditionCount: observable.ref(3),
	}
	@observable subTab = {
		curKey: '0',
		shValue: '',
	}
	@observable thirdTab = {
		curKey: '0',
		shValue: '',
	}

	@computed get menu() {
		let topMenu = this.main.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.main.btnMenu.filter(item => !item.isBatch);

		return {
			topMenu,
			rightMenu
		}
	}

	@computed get mainDialogMenu() {
		let topMenu = this.mainDialog.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.mainDialog.btnMenu.filter(item => !item.isBatch);

		return {
			topMenu,
			rightMenu
		}
	}

	@computed get subDialogmenu() {
		let topMenu = this.subDialog.btnMenu.filter(item => item.isTop == '1' || item.isBatch == '1');
		let rightMenu = this.subDialog.btnMenu.filter(item => !item.isBatch);

		return {
			topMenu,
			rightMenu
		}
	}

	@action
	getAuth = () => {
		Api.getAuth().then(cb => {
			const {
				status,
				hasRight
			} = cb;

			if (status == '1') {
				hasRight && this.grantAuth();
				this.stoploading('main');
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getButtonsMenu = () => {
		let func;
		let params;

		if (!this.mainDialog.visible) { //主页
			func = 'getMainButtonsMenu';
			params = {};
		} else {
			params = {
				trainId: this.mainTable.selectedRowKey
			}
			if (!this.subDialog.visible) { //一级弹框
				const {
					index
				} = this.mainTable, {
					curKey
				} = this.subTab;

				if (index == '0' && curKey == '1') func = 'getScheduleButtonsMenu'; //培训日程

				if ((index == '0' && curKey == '2') || index == '3') func = 'getTestButtonsMenu'; //培训考核

				if ((index == '0' && curKey == '3') || index == '4') func = 'getReviewButtonsMenu'; //培训考评
			} else { //二级弹框（只获取“参加情况”列表的ButtonsMenu）
				func = 'getAttendButtonsMenu'; //培训日程 - 参加情况
			}
		}
		Api[func](params).then(cb => {
			const {
				api_status,
				btnMenu,
			} = cb;

			if (api_status) {
				if (!this.mainDialog.visible) {
					this.main.btnMenu = btnMenu;
				} else {
					if (!this.subDialog.visible) {
						this.mainDialog.btnMenu = btnMenu;
					} else {
						this.subDialog.btnMenu = btnMenu;
					}
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getTableInfo = () => {
		let func;
		let params;

		const {
			visible
		} = this.mainDialog;

		if (!visible) {
			const {
				form,
				curKey
			} = this.mainTab;

			func = 'getActivityTableInfo';
			params = {
				...form.getFormParams(),
				typeTab: curKey
			}
		} else {
			const {
				index
			} = this.mainTable, {
				curKey
			} = this.subTab, {
				visible
			} = this.subDialog;

			params = {
				trainId: this.mainTable.selectedRowKey
			}

			if (index == '0' && curKey == '1' && !visible) { //培训日程
				func = 'getScheduleTableInfo';
				params = {
					...params,
					trainDayContent: this.subTab.shValue,
				}
			}
			if ((index == '0' && curKey == '2') || index == '3') { //培训考核
				func = 'getTestTableInfo';
				params = {
					...params,
					lastName: this.subTab.shValue
				}
			}
			if ((index == '0' && curKey == '3') || index == '4') { //培训考评
				func = 'getReviewTableInfo';
				params = {
					...params,
					lastName: this.subTab.shValue
				}
			}
			if (index == '0' && curKey == '1' && visible && this.thirdTab.curKey == '1') { //培训日程--参加情况
				func = 'getAttendTableInfo';
				params = {
					trainId: this.mainTable.selectedRowKey,
					trainDayId: this.subTable.selectedRowKey,
					lastName: this.thirdTab.shValue
				}
			}
		}

		Api[func](params).then(cb => {
			const {
				api_status,
				sessionkey,
			} = cb;

			if (api_status) {
				const {
					visible
				} = this.mainDialog;

				if (!visible) { //主表
					const {
						tableStore
					} = this.mainTable;

					sessionkey && tableStore.getDatas(sessionkey, 1);
				} else {
					const {
						tableStore
					} = this.subTable, {
						visible
					} = this.subDialog;

					if (!visible) { //次表
						sessionkey && tableStore.getDatas(sessionkey, 1);
					} else { //第三级列表
						const {
							tableStore
						} = this.thirdTable;

						sessionkey && tableStore.getDatas(sessionkey, 1);
					}
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getFormInfo = (type) => {
		let func;
		let params;

		const {
			index
		} = this.mainTable;

		if (!this.mainDialog.visible) { //高级搜索
			func = 'getSearchItems';
			params = {};
		} else {
			if (!this.subDialog.visible) { //只打开了主弹框
				const {
					selectedRowKey
				} = this.mainTable;

				if (!selectedRowKey) { //新建
					func = 'getActivityForm';
					params = {}
				} else { //主表右侧功能菜单中表单

					params = {
						trainId: selectedRowKey
					}

					if (index == '0') func = 'getActivityForm'; //培训活动

					if (index == '1') func = 'getTraineeForm'; //培训人

					if (index == '2') func = 'getScheduleForm'; //培训日程

					if (index == '5' || index == '6') func = 'getActivityEndForm'; //结束活动、结束信息

				}
			} else { //打开二级弹框
				const {
					curKey
				} = this.subTab;

				params = {
					trainId: this.mainTable.selectedRowKey
				}

				if (index == '0' && curKey == '1') { //培训日程
					func = 'getScheduleForm';
					const {
						selectedRowKey
					} = this.subTable;

					if (selectedRowKey) { //编辑
						params = {
							...params,
							trainDayId: selectedRowKey
						}
					}
				}

				if ((index == '0' && curKey == '2') || index == '3') {
					const {
						selectedRowKey
					} = this.subTable;

					func = 'getTestForm'; //培训考核

					if (selectedRowKey) {
						params = {
							...params,
							trainTestId: selectedRowKey
						}
					}
				}

				if ((index == '0' && curKey == '3') || index == '4') {
					const {
						selectedRowKey
					} = this.subTable;

					func = 'getReviewForm'; //培训考评

					if (selectedRowKey) {
						params = {
							...params,
							trainAssessId: selectedRowKey
						}
					}
				}

			}
		}

		Api[func](params).then(cb => {
			const {
				api_status,
				conditions,
			} = cb;

			if (api_status) {
				// this.deleteParams(conditions)
				if (!this.mainDialog.visible) { //高级搜索
					this.mainTab.conditions = conditions;
					this.mainTab.form.initFormFields(conditions);
					this.stoploading('search');
				} else {
					if (index == '6') this.setDefaultValue(conditions);
					this.formInfo.conditions = conditions;
					this.formInfo.form.initFormFields(conditions);
					this.stoploading('form');
				}
			} else {
				message.error(cb.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	setDefaultValue = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				const key = item.domkey[0];
				if (key == 'trainAdvice' && item.viewAttr == 1) {
					item.conditionType = 'INPUT';
				}
			})
		})
	}

	@action
	deleteParams = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				if (item.conditionType == 'INPUT' && item.viewAttr !== 1) {
					if (item.otherParams) delete item.otherParams
				}
			})
		})
	}

	@action
	deleteTableInfo = () => {
		const {
			visible
		} = this.mainDialog;

		let func;
		let params;
		if (!visible) { //删除主表内容
			func = 'deleteActivity';

			const {
				tableStore,
				selectedRowKey
			} = this.mainTable;

			params = {
				trainIds: tableStore.selectedRowKeys.length > 0 ? tableStore.selectedRowKeys.toString() : selectedRowKey
			}
		} else {
			func = 'deleteSchedule';

			const {
				tableStore,
				selectedRowKey
			} = this.subTable;

			params = {
				trainDayIds: tableStore.selectedRowKeys.length > 0 ? tableStore.selectedRowKeys.toString() : selectedRowKey
			}
		}

		Api[func](params).then(cb => {
			const {
				api_status,
				sign
			} = cb;

			if (api_status) {
				if (sign == '1') {
					message.success(cb.message);
					this.getTableInfo();
					!visible ? this.refreshTable('main') : this.refreshTable('sub');
				} else {
					message.warning(cb.message)
				}
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	getTrainArrangeContent = (id) => {
		const params = {
			trainPlanId: id
		};

		Api.getTrainArrangeContent(params).then(cb => {
			const {
				api_status,
			} = cb;

			if (api_status) {
				this.updateTrainActivityForm(cb);
			} else {
				message.warning(cb.message)
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	updateTrainActivityForm = (cb) => {
		const {
			planEndDate,
			planStartDate,
			trainPlanAim,
			trainPlanContent,
			trainPlanOrganizer,
			planOrganizerName,
		} = cb;

		const {
			form
		} = this.formInfo;

		let valueObj = [];

		const names = planOrganizerName.split(',')

		trainPlanOrganizer.split(',').map((id, index) => {
			valueObj.push({
				id: id,
				name: names[index]
			});
		});

		form.updateFields({
			trainStartDate: {
				value: planStartDate
			},
			trainEndDate: {
				value: planEndDate
			},
			trainOrganizer: {
				value: trainPlanOrganizer,
				valueSpan: planOrganizerName,
				valueObj: valueObj,
			},
			trainContent: {
				value: trainPlanContent
			},
			trainAim: {
				value: trainPlanAim
			}
		});
	}

	@action
	clickTab = (key, type) => {
		if (type == 'main') {
			this.mainTab.curKey = key;
			this.resetSearch('main');
			this.mainTable.tableStore = new TableStore();
			this.getTableInfo();
		}

		if (type == 'sub') {
			this.subTab.curKey = key;

			this.resetSearch('sub');

			if (key == '0') {
				this.getFormInfo();
			} else {
				this.subTable.tableStore = new TableStore();
				this.getTableInfo();
			}

			if (key !== '0') {
				this.refreshFormInfo(); //为了避免公用form导致的问题
				this.getButtonsMenu(); //表单必有保存、更多按钮，所以不需要调用接口；只有列表需要调用ButtonsMenu接口。
			}
		}

		if (type == 'third') {
			this.thirdTab.curKey = key;

			this.resetSearch('third');

			if (key == '0') {
				this.getFormInfo();
			} else {
				this.thirdTable.tableStore = new TableStore();
				this.getTableInfo();
				this.getButtonsMenu();
			}
		}
	}

	@action
	resetTab = (type) => {
		if (type == 'third') this.thirdTab.curKey = '0';
	}

	@action
	save = () => {
		const {
			form
		} = this.formInfo;

		form.validateForm().then(f => {
			if (f.isValid) {
				const {
					selectedRowKey,
					index
				} = this.mainTable, {
					curKey
				} = this.subTab;

				this.recoverloading('form');

				let func;
				let params;

				if (!this.subDialog.visible) { //一级弹框保存
					params = form.getFormParams();

					if (selectedRowKey) { //表格右侧功能保存
						params = {
							...params,
							trainId: this.mainTable.selectedRowKey,
						}
						if (index == '0') func = 'editActivity'; //基本信息

						if (index == '1') func = 'addTrainee'; //添加培训人员

						if (index == '2') func = 'addSchedule'; //添加日程

						if (index == '5') func = 'addEndActivity'; //添加“结束活动”信息

					} else {
						func = 'addActivity';
						params = {
							...params
						};
					}
				} else { //二级弹框
					const {
						selectedRowKey
					} = this.subTable;

					params = {
						trainId: this.mainTable.selectedRowKey,
						...form.getFormParams()
					}

					if (index == '0' && curKey == '1') {
						if (!selectedRowKey) { //添加培训日程
							func = 'addSchedule';
						} else { //编辑培训日程
							func = 'editSchedule';
							params = {
								...params,
								trainDayId: selectedRowKey
							}
						}
					}

					if ((index == '0' && curKey == '2') || index == '3') { //添加、编辑培训考核
						if (!selectedRowKey) {
							func = 'addTest';
						} else {
							func = 'editTest';
							params = {
								...params,
								trainTestId: selectedRowKey
							}
						}
					}

					if ((index == '0' && curKey == '3') || index == '4') { //添加、编辑培训考评
						if (!selectedRowKey) {
							func = 'addReview';
						} else {
							func = 'editReview';
							params = {
								...params,
								trainAssessId: selectedRowKey
							}
						}
					}

				}

				Api[func](params).then(cb => {
					const {
						api_status,
						sign
					} = cb;

					if (api_status) {
						if (sign == '1') {
							const {
								visible
							} = this.subDialog;

							message.success(cb.message);
							visible ? this.closeSubDialog() : this.closeMainDialog();
							this.getTableInfo();
						} else {
							message.warning(cb.message)
						}
						this.stoploading('form');
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.setDate({
					date: new Date()
				});
			}
		});
	}

	@action
	attend = () => {
		const params = {
			trainId: this.mainTable.selectedRowKey,
			trainDayId: this.subTable.selectedRowKey,
			trainActorIds: this.thirdTable.tableStore.selectedRowKeys.toString(),
		}

		Api.editAttendance(params).then(cb => {
			const {
				api_status,
				sign
			} = cb;

			if (api_status) {
				if (sign == '1') {
					message.success(cb.message);
					this.thirdTable.tableStore = new TableStore();
					this.getTableInfo();
				} else {
					message.warning(cb.message)
				}
			} else {
				message.warning(cb.message)
			}
		}).catch(error => {
			message.error(error);
		});
	}

	@action
	resetTableSelectedKeys = () => {
		this.thirdTable.tableStore.selectedRowKeys = [];
	}

	@action
	openMainDialog = () => {
		const {
			index
		} = this.mainTable;

		this.mainDialog.visible = true;

		const {
			selectedRowKey
		} = this.mainTable;

		if (!selectedRowKey) {
			this.mainDialog.title = i18n.label.addTrainActivity();
			this.getFormInfo();
			return;
		}

		switch (index) {
			case '0': //编辑
				this.mainDialog.title = i18n.label.editTrainActivity();
				break;
			case '1': //添加培训人
				this.getFormInfo();
				this.mainDialog.title = i18n.label.addTrainee();
				break;
			case '2': //添加培训日程
				this.mainDialog.title = i18n.label.addTrainSchedule();
				this.getFormInfo();
				break;
			case '3': //培训考核
				this.mainDialog.title = i18n.label.trainTest();
				this.getTableInfo();
				this.getButtonsMenu();
				break;
			case '4': //培训考评
				this.mainDialog.title = i18n.label.trainReview();
				this.getTableInfo();
				this.getButtonsMenu();
				break;
			case '5': //结束活动
				this.mainDialog.title = i18n.label.stopActivity();
				this.getFormInfo();
				break;
			case '6': //结束信息
				this.mainDialog.title = i18n.label.finishInfo();
				this.getFormInfo();
				break;
		}

	}

	@action
	closeMainDialog = () => {
		this.mainDialog.visible = false;

		this.refreshFormInfo();

		this.refreshTable('main');

		this.resetSearch('sub');
	}

	@action
	openSubDialog = () => {
		const {
			index
		} = this.mainTable, {
			curKey
		} = this.subTab;

		this.subDialog.visible = true;

		const {
			selectedRowKey
		} = this.subTable;

		if (index == '0' && curKey == '1') {
			this.subDialog.title = !selectedRowKey ? i18n.label.addTrainSchedule() : i18n.label.editTrainSchedule();
		}
		if ((index == '0' && curKey == '2') || index == '3') {
			this.subDialog.title = !selectedRowKey ? i18n.label.addTestInfo() : i18n.label.editTestInfo();
		}
		if ((index == '0' && curKey == '3') || index == '4') {
			this.subDialog.title = !selectedRowKey ? i18n.label.addReviewInfo() : i18n.label.editReviewInfo();
		}

		this.getFormInfo();
	}

	@action
	closeSubDialog = () => {
		this.subDialog.visible = false;

		this.getButtonsMenu();

		this.refreshFormInfo();

		this.refreshTable('sub');
	}

	@action
	changeSearchPanelStatus = (bool) => {
		this.mainTab.isPanelShow = bool;
	}

	@action
	grantAuth = () => {
		this.main.authorized = true;
	}

	@action
	closeAuth = () => {
		this.main.authorized = false;
	}

	@action
	recoverloading = (type) => {
		if (type == 'main') this.main.loading = true;

		if (type == 'form') this.formInfo.loading = true;
	}

	@action
	stoploading = (type) => {
		if (type == 'main') this.main.loading = false;

		if (type == 'search') this.mainTab.loading = false;

		if (type == 'form') this.formInfo.loading = false;
	}

	@action
	refreshPage = () => {
		this.resetSearch();
		this.clickTab('0', 'main');
	}

	@action
	resetSearch = (type) => {
		if (type == 'main') {
			this.mainTab.isPanelShow = false;
			this.mainTab.form.reset();
		}

		if (type == 'sub') this.subTab.shValue = '';

		if (type == 'third') this.thirdTab.shValue = '';
	}

	@action
	refreshFormInfo = () => {
		this.formInfo.form = new WeaForm();
		this.formInfo.conditions = [];
		this.recoverloading('form');
	}

	@action
	refreshTable = (type) => { //这里的selectedRowKey既是参数，同时也是标识。
		if (type == 'main') this.mainTable.selectedRowKey = '';

		if (type == 'sub') this.subTable.selectedRowKey = '';
	}

	@action
	setSelectRowKey = (id, type) => {
		if (type == 'main') this.mainTable.selectedRowKey = id;

		if (type == 'sub') this.subTable.selectedRowKey = id;
	}

	@action
	setIndex = (index) => {
		this.mainTable.index = index;
	}

	@action
	setSearchValue = (val, type) => {
		if (type == 'sub') this.subTab.shValue = val;

		if (type == 'third') this.thirdTab.shValue = val;
	}

	@action
	setDate = (date) => {
		this.formInfo.date = date;
	}

}