import {
	observable,
	action,
	computed,
} from 'mobx';
import {
	WeaForm,
} from 'comsMobx';
import {
	WeaLocaleProvider,
} from 'ecCom';
import {
	message,
	Button,
} from 'antd';
import * as api from '../apis/groupAdvice';
const getLabel = WeaLocaleProvider.getLabel;

class HrmGroupAdvice {
	groupId = '';

	footerButtons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bron8q@footBtn`} 
						type="primary" 
						disabled={this.f.loading} 
						onClick={() => this.submit()}
					>
						{getLabel(-1,'提交')}
					</Button>]

	@observable f = {
		form: new WeaForm(),
		loading: false,
	}

	@action getOrgGroupSuggestForm = () => {
		this.f.loading = true;
		api.getOrgGroupSuggestForm({
			groupId: this.groupId
		}).then(data => {
			const {
				conditions
			} = data;

			this.f.form = new WeaForm();
			this.f.form.setCondition(conditions);
			this.f.loading = false;
		}, error => {})
	}

	extractParamsFromURL = (query) => {
		const {
			groupId,
		} = query;

		this.groupId = groupId;
	}

	@action validateForm = (callback) => {
		this.f.form.validateForm().then(f => {
			if (f.isValid) {
				callback && callback();
			} else {
				f.showErrors();
			}
		})
	}

	mail = (type) => {
		const pDialog = top.window.getParentDialog();
		pDialog && pDialog.callback(type);
	}

	saveForm = () => {
		api.saveOrgGroupSuggest({
			groupId: this.groupId,
			...this.f.form.getFormParams()
		}).then(data => {
			const {
				sign
			} = data;

			if (sign === '1') {
				message.success(data.message);
				this.mail('1');
			} else {
				message.warning(data.message);
			}
		}, error => {})
	}

	submit = () => {
		this.validateForm(this.saveForm);
	}
}

export const hrmGroupAdvice = new HrmGroupAdvice();