import {
	observable,
	action,
	computed,
	extendObservable
} from 'mobx';

import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';

import {
	message
} from 'antd'

import {
	i18n
} from '../public/i18n';

const {
	TableStore
} = WeaTableNew;

import * as mobx from 'mobx';
import * as Api from '../apis/birthdayRemind';
const toJS = mobx.toJS;

export class HrmBirthdayRemindSet {
	@observable weaTopTitle = i18n.module.letStaffFeelWarm();
	@observable btnMenu = [];
	@observable authorized = false;
	@observable loading = true;
	@observable date = '';
	@observable selectedKey = '0';
	@observable tabOne = {
		loading: true,
		form: new WeaForm(),
		tableStore: new TableStore(),
		auth: {
			loading: true,
			visible: false,
			title: i18n.label.addSysadmin(),
		}
	};
	@observable tabTwo = {
		loading: true,
		form: new WeaForm(),
		imgs: [],
		uploadImages: [],
		selectedPicVal: 0,
		upParams: [],
	}
	@observable tabs = {
		key: '1',
	}
	@observable entryRemind = {
		loading: true,
		form: new WeaForm(),
		imgs: [],
		uploadImages: [],
		selectedPicVal: 0,
		upParams: [],
		birthGreeting: '',
		greetingColor: '',
		font: '',
		fontSize: '',
	}


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

		return {
			topMenu,
			rightMenu
		}
	}

	@computed get totalImages() {
		let arr = [...this.tabTwo.imgs];
		arr.push(...this.tabTwo.uploadImages);
		return arr;
	}

	@computed get erTotalImages() {
		let arr = [...this.entryRemind.imgs];
		arr.push(...this.entryRemind.uploadImages);
		return arr;
	}

	@computed get picRelatedParams() {
		let arr = this.tabTwo.upParams.map(p => toJS(p));
		let pic = {};
		let len = arr.length;
		arr.map((item, index) => {
			for (let key in item) {
				pic[key + index] = item[key];
			}
		});
		return {
			pic,
			len
		}
	}

	@computed get erPicRelatedParams() {
		let arr = this.entryRemind.upParams.map(p => toJS(p));
		let pic = {};
		let len = arr.length;
		arr.map((item, index) => {
			for (let key in item) {
				pic[key + index] = item[key];
			}
		});
		return {
			pic,
			len
		}
	}

	@action
	getRight = () => {
		this.loading = true;
		Api.getRight().then(res => {
			let {
				api_status,
				btnMenu = [],
				hasRight
			} = res;

			if (api_status) {
				if (hasRight) {
					this.authorized = true;
					this.btnMenu = btnMenu;
					this.mobxDataReset();
				} else {
					this.authorized = false;
				}
				this.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getTabOneDatas = () => {
		this.tabOne.loading = true;

		Api.getTabOneDatas().then(res => {
			let {
				api_status,
				sessionkey,
				condition,
				explain
			} = res;

			if (api_status) {
				this.tabOne.form.initFormFields(condition);
				extendObservable(this.tabOne, {
					condition: condition,
					explanation: explain
				});
				this.tabOne.tableStore.getDatas(sessionkey, 1);
				this.tabOne.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getTable = () => {
		Api.getTable().then(res => {
			let {
				api_status,
				sessionkey
			} = res;

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

	getAuthForm = () => {
		this.tabOne.auth.loading = true;

		Api.getAuthForm().then(res => {
			let {
				api_status,
				conditions
			} = res;

			if (api_status) {
				extendObservable(this.tabOne.auth, {
					conditions: conditions
				})
				this.tabOne.auth.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getTabTwoForm = () => {
		this.tabTwo.loading = true;

		Api.getTabTwoForm().then(res => {
			let {
				api_status,
				condition,
				imglist,
				isE9 = false
			} = res, {
				index,
				imgs = []
			} = imglist;

			if (api_status) {
				if (condition) {
					if (isE9) {
						this.adjustOrder(condition);
					} else {
						let arr = condition[0].items;
						let i3 = arr.findIndex(item => item.domkey[0] == 'showtitle2');
						arr[i3].options[0].disabled = true;
					}
					this.tabTwo.form.initFormFields(condition);
					extendObservable(this.tabTwo, {
						condition: condition,
						imgList: imglist,
						selectedPicVal: index ? index : 0,
						imgs: imgs
					})
				}
				this.tabTwo.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getEntryDatas = () => {
		this.entryRemind.loading = true;

		Api.getEntryDatas().then(res => {
			let {
				api_status,
				conditions,
				imglist,
				isE9 = false
			} = res, {
				index,
				imgs = []
			} = imglist;

			if (api_status) {
				if (conditions) {
					if (isE9) {
						this.adjustOrder(conditions);
					}
					this.entryRemind.form.initFormFields(conditions);
					this.setDefaultValues(conditions);
					extendObservable(this.entryRemind, {
						condition: conditions,
						imgList: imglist,
						selectedPicVal: index ? index : 0,
						imgs: imgs
					})
				}
				this.entryRemind.loading = false;
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}


	setDefaultValues = (conditions) => {
		conditions.map(c => {
			c.items.map(item => {
				const key = item.domkey[0];
				if (key == 'entryCongrats') this.entryRemind.birthGreeting = item.value;
				if (key == 'entryCongratsColor') this.entryRemind.greetingColor = item.value;
				if (key == 'entryFont') this.entryRemind.font = item.options[parseInt(item.value)].showname;
				if (key == 'entryFontSize') this.entryRemind.fontSize = item.value;
			})
		});
	}

	adjustOrder = (condition = []) => {
		let arr = condition[0].items;
		let i1 = arr.findIndex(item => item.domkey[0] == 'colColor'),
			o1 = arr.splice(i1, 1),
			i3 = arr.findIndex(item => item.domkey[0] == 'showtitle2');
		arr.splice(i3 + 1, 0, o1[0]);

		let i2 = arr.findIndex(item => item.domkey[0] == 'greColor'),
			o2 = arr.splice(i2, 1),
			i4 = arr.findIndex(item => item.domkey[0] == 'greetings2');
		arr.splice(i4 + 1, 0, o2[0]);


		arr[i3].options[0].disabled = true;
		let item = arr.find(item => item.domkey[0] == 'showtitle');
		item.options[0].disabled = true;
	}

	add = () => {
		let params = this.tabOne.auth.authParams;
		Api.authSave(params).then(res => {
			let {
				api_status,
				sign
			} = res;

			if (api_status) {
				sign == '1' ? message.success(res.message) && this.getTable() : message.warning(res.message);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	tabOneSave = () => {
		this.tabOne.form.validateForm().then(f => {
			if (f.isValid) {
				let params = this.tabOne.form.getFormParams();
				Api.tabOneSave(params).then(res => {
					let {
						api_status,
						sign
					} = res;
					if (api_status) {
						sign == '1' ? message.success(res.message) : message.warning(res.message)
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.date = new Date()
			}
		});

	}

	tabTwoSave = () => {
		this.tabTwo.form.validateForm().then(f => {
			if (f.isValid) {
				let params = {
					...this.tabTwo.form.getFormParams(),
					index: this.tabTwo.selectedPicVal
				};
				Api.tabTwoFormSave(params).then(res => {
					let {
						api_status,
						sign
					} = res;

					if (api_status) {
						sign == '1' ? message.success(res.message) : message.warning(res.message)
					} else {
						message.error(rs.message);
					}
				}).catch(error => {
					message.error(error);
				});
			} else {
				f.showErrors();
				this.date = new Date()
			}
		});
	}

	entryRemindSave = () => {
		let params = {
			...this.entryRemind.form.getFormParams(),
			index: this.entryRemind.selectedPicVal
		};
		Api.entryRemindSave(params).then(res => {
			let {
				api_status,
				sign
			} = res;

			if (api_status) {
				sign == '1' ? message.success(res.message) : message.warning(res.message)
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	partSave = (type) => {
		let params;
		let func;
		if (type == 'entryRemind') {
			func = 'saveEntryRemind';
			params = {
				cmd: 'add',
				...this.erPicRelatedParams.pic,
				len: this.erPicRelatedParams.len
			};
		} else {
			func = 'saveImg';
			params = {
				type: 'add',
				...this.picRelatedParams.pic,
				len: this.picRelatedParams.len
			};
		}
		Api[func](params).then(res => {
			let {
				api_status,
				sign
			} = res;

			if (api_status) {
				// sign == '1' ? message.success(res.message) : message.warning(res.message)
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	doDel = (id, index, type) => {
		let params = {
			type: 'delete',
			imgid: id,
			index: this.getSelectedIndex(index, type),
		};
		Api.saveImg(params).then(res => {
			let {
				api_status,
				sign
			} = res;

			if (api_status) {
				sign == '1' ? message.success(res.message) : message.warning(res.message);
				this.delImages(index, type); //前端删除
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	delRows = () => {
		let params = {
			ids: this.tabOne.tableStore.selectedRowKeys.toString(),
		};
		Api.delRows(params).then(res => {
			let {
				api_status,
				sign
			} = res;

			if (api_status) {
				sign == '1' ? message.success(res.message) && this.getTable() : message.warning(res.message);
			} else {
				message.error(rs.message);
			}
		}).catch(error => {
			message.error(error);
		});
	}

	getSelectedIndex = (index, type) => {

		let selectedPicVal = (type == 'entryRemind') ? this.entryRemind.selectedPicVal : this.tabTwo.selectedPicVal

		let selectedIndex;
		if (selectedPicVal == index + 1) {
			selectedIndex = 0;
			if (type == 'entryRemind') {
				this.entryRemind.selectedPicVal = selectedIndex;
			} else {
				this.tabTwo.selectedPicVal = selectedIndex;
			}
		} else {
			selectedIndex = selectedPicVal > index + 1 ? selectedPicVal - 1 : selectedPicVal;
		}
		return selectedIndex;
	}

	setSelectedKey = (key) => {
		this.selectedKey = key;
		this.entryRemind.uploadImages = [];
		if (key == '0') {
			this.clickTabs('0');
		} else {
			this.getEntryDatas();
		}
	}

	clickTabs = (key) => {
		this.tabs.key = key;
		this.tabTwo.uploadImages = [];
		if (key == '0') {
			// extendObservable(this.tabTwo, {
			// 	remindType: '',
			// 	condition: [],
			// });
			this.getTabOneDatas();
		} else if (key == '1') {
			this.getTabTwoForm();
		}
	}

	resetTabTwo = () => {
		this.tabTwo.remindType = '';
		this.tabTwo.condition = [];
	}

	setAuthParams = (dates) => {
		extendObservable(this.tabOne.auth, {
			authParams: dates
		});
	}

	setVisible = (bool) => {
		this.tabOne.auth.visible = bool;
		bool && this.getAuthForm();
	}

	setRemindType = (val, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				remindType: val
			});
		} else {
			extendObservable(this.tabTwo, {
				remindType: val
			});
		}
	}

	setUploadIds = (ids = [], type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				uploadIds: ids
			});
		} else {
			extendObservable(this.tabTwo, {
				uploadIds: ids
			});
		}
	}

	setUploadImages = (lists = [], type) => {
		let arr = [];
		lists.map(list => {
			arr.push({
				id: list.fileid,
				name: list.filename
			});
		});
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				uploadImages: this.entryRemind.uploadImages.concat(arr),
				upParams: arr
			});
		} else {
			extendObservable(this.tabTwo, {
				uploadImages: this.tabTwo.uploadImages.concat(arr),
				upParams: arr
			});
		}


	}

	setSelectedRadio = (val, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				selectedPicVal: val
			});
		} else {
			extendObservable(this.tabTwo, {
				selectedPicVal: val
			});
		}
	}

	setBirthGreeting = (word, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				birthGreeting: word ? word : 'Happy!'
			});
		} else {
			extendObservable(this.tabTwo, {
				birthGreeting: word ? word : 'Happy Birthday!'
			});
		}
	}

	setMultiple = (str, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				multiple: [...str].filter(item => item !== ',')
			});
		} else {
			extendObservable(this.tabTwo, {
				multiple: [...str].filter(item => item !== ',')
			});
		}
	}

	setMultipleColor = (color, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				multipleColor: color
			});
		} else {
			extendObservable(this.tabTwo, {
				multipleColor: color
			});
		}

	}

	setGreetingColor = (color, type) => {
		if (type == 'entryRemind') {
			extendObservable(this.entryRemind, {
				greetingColor: color
			});
		} else {
			extendObservable(this.tabTwo, {
				greetingColor: color
			});
		}
	}

	setFont = (font) => {
		extendObservable(this.entryRemind, {
			font: font
		});
	}

	setFontSize = (fontSize) => {
		extendObservable(this.entryRemind, {
			fontSize: fontSize
		});
	}


	delImages = (index, type) => {
		let selectedPicVal = (type == 'entryRemind') ? this.entryRemind.selectedPicVal : this.tabTwo.selectedPicVal;
		let imgs = (type == 'entryRemind') ? this.entryRemind.imgs : this.tabTwo.imgs;
		let uploadImages = (type == 'entryRemind') ? this.entryRemind.uploadImages : this.tabTwo.uploadImages;

		index < imgs.length ? imgs.splice(index, 1) : uploadImages.splice(index - imgs.length, 1)

		if (selectedPicVal == index + 1) {
			if (type == 'entryRemind') {
				this.entryRemind.selectedPicVal = 0;
			} else {
				this.tabTwo.selectedPicVal = 0;
			}
		} else {
			if (type == 'entryRemind') {
				this.entryRemind.selectedPicVal = selectedPicVal > index + 1 ? selectedPicVal - 1 : selectedPicVal;
			} else {
				this.tabTwo.selectedPicVal = selectedPicVal > index + 1 ? selectedPicVal - 1 : selectedPicVal;
			}
		}
	}

	mobxDataReset = () => {
		this.tabTwo.uploadImages = [];
		this.setSelectedKey('0');
	}
}