import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaForm,
} from 'comsMobx';
import {
	Button,
	message,
} from 'antd';
import {
	WeaLocaleProvider
} from 'ecCom';
import {
	fetch,
	getTopButtons,
	getDropMenuDatas,
} from '../util/save-time-util.js';
const getLabel = WeaLocaleProvider.getLabel;

export class HrmBrowseBoxLinkPage {
	@observable asyncFetch = '';
	@observable fetchParams = {};
	@observable hasDirtyId = false;

	@action setDirtyIdFlag = (params) => {
		const {
			hasDirtyId
		} = params;

		this.hasDirtyId = hasDirtyId;
	}
	//**************Top组件****************
	topStaticProps = {
		icon: <i className='icon-coms-hrm'/>,
		iconBgcolor: '#217346',
		showDropIcon: true
	}

	@observable top = {
		title: '',
	}

	@action setTopTitle = (params) => {
		const {
			title
		} = params;

		this.top.title = title;
	}

	get buttons() {
		if (!this.formElement.hasSaveBtn) {
			return [];
		}

		const config = [{
			menuFun: 'save',
			menuName: getLabel('30986', "保存"),
		}];

		return getTopButtons.call(this, {
			datas: config,
			disabled: [this.formElement.loading],
		})
	}

	get dropMenuDatas() {
		if (!this.formElement.hasSaveBtn) {
			return [];
		}

		const config = [{
			menuFun: 'save',
			menuName: getLabel('30986', "保存"),
			menuIcon: 'icon-coms-Preservation'
		}];
		if (this.page == "post") {
			config.push({
				menuFun: 'doLog',
				menuName: getLabel(83, '日志'),
				menuIcon: 'icon-coms-Print-log'
			})
		}

		return getDropMenuDatas.call(this, {
			datas: config,
			disabled: [this.formElement.loading],
		});
	}

	save = () => {
		const {
			form
		} = this.formElement;

		const fetchParams = form.getFormParams();

		Object.assign(fetchParams, this.hasDirtyId ? this.fetchParams : {
			id: Object.values(this.fetchParams)[0],
		});

		this.formElement.countryid && Object.assign(fetchParams, {
			'countryid': this.formElement.countryid
		});

		form.validateForm().then(f => {
			if (f.isValid) {
				fetch({
					asyncFetch: this.asyncFetch,
					name: fetchParams.id ? 'save' : "add",
					fetchParams: fetchParams,
					logic: (cb) => {
						const {
							sign,
						} = cb;

						if (sign === "1") {
							const pDialog = top.window.getParentDialog();

							pDialog && pDialog.callback(cb);
						}
						message[sign === '1' ? 'success' : 'warning'](cb.message);
					}
				});
			} else {
				f.showErrors();
			}
		})
	}

	doLog = () => {
		window.setLogViewProps({
			logSmallType:  '3014',
			targetId:  Object.values(this.fetchParams)[0]
		});
	}

	//**************表单****************
	@observable formElement = {
		form: new WeaForm(),
		condition: [],
		loading: false,
		hasSaveBtn: true,
	}

	getFormElements = (params) => {
		const {
			asyncFetch,
			fetchParams,
		} = params;

		//存储Api
		this.storeApi(asyncFetch);
		//存储fetchParams
		this.storeFetchParams(fetchParams);

		this.processing();

		fetch({
			asyncFetch,
			name: 'getFormElements',
			fetchParams,
			logic: (cb) => {
				const {
					condition
				} = cb;

				this.processCondition(condition);

				['loading', 'condition', 'hasSaveBtn', 'form'].map((v, i) => {
					if (i < 3) {
						this.formElement[v] = (i === 0) ? false : cb[v];
					} else {
						this.formElement[v].initFormFields(condition);
					}
				});

				this.setSaveParams(cb);
			}
		});
	}

	setSaveParams = (cb) => {
		const {
			countryid
		} = cb;

		if (countryid) this.formElement.countryid = countryid;
	}

	processCondition = (condition) => {
		condition.map(c => {
			c.items.map(item => {
				const {
					domkey
				} = item;

				if (['jobgroupid', 'jobactivityid'].includes(domkey[0])) {
					item.browserConditionParam.hasAddBtn = false;
				}
			});
		});
	}

	getCountryDetail = (id) => {
		fetch({
			asyncFetch: this.asyncFetch,
			name: 'getCountryDetail',
			fetchParams: {
				cityid: id
			},
			logic: (cb) => {
				const {
					countryid,
					countryname,
				} = cb;

				const {
					form
				} = this.formElement;

				form.updateFields({
					countryname: {
						value: countryname,
					}
				});

				this.setSaveParams({
					countryid
				});
			}
		});
	}

	@action processing = () => {
		this.formElement.loading = true;
	}

	@action storeApi = (asyncFetch) => {
		this.asyncFetch = asyncFetch;
	}

	@action storeFetchParams = (params) => {
		this.fetchParams = params;
	}

	setPage = (page) => {
		this.page = page;
	}

}