import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaForm,
	WeaSwitch
} from 'comsMobx';
import {
	Button,
	message
} from 'antd';
import HrmBaseStore from './baseStore';
import * as api from '../apis/paidLeave';
import {
	i18n
} from '../public/i18n';

class HrmPaidLevel extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		return {
			btns: [
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@oxzh0g`} type='primary' onClick={this.save}>{i18n.button.save()}</Button>,
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@8plax6`} type='primary' onClick={this.sync}>{i18n.button.syncToSubDept()}</Button>
			],
			menus: [{
				key: '1',
				content: i18n.button.save(),
				icon: <i className={this.menuIconCollection.save}/>,
				onClick: this.save
			}, {
				key: '2',
				content: i18n.button.syncToSubDept(),
				icon: <i className={this.menuIconCollection.copy}/>,
				onClick: this.sync
			}, ...this.getBasicMenus('HRM_ENGINE_HRM_PAIDLEAVE_SET')]
		}
	}

	orgTreeProps = {
		needSearch: true,
		isLoadAllSub: true,
		isLoadSubDepartment: false,
		topPrefix: 'hrmSearch',
		inputLeftDom: `<b>${i18n.label.organization()}</b>`,
		treeNodeClick: this.treeNodeClick,
		params: {
			rightStr: 'HrmPaidLeave:setting'
		}
	}

	formItemRender = {
		field003: (field, textAreaProps, form, formParams) => {
			return (
				<div className='content'>
					<div className='date'>
	                    <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@k3d8yw`} fieldConfig={field} form={form} formParams={formParams}/>
	                </div>
	                <div className='tips'>{i18n.label.overdue()}</div>
                </div>
			)
		}
	}

	selectedSubCompany;
	formId;
	/********************* unobservable list *********************/

	/********************* observable list *********************/
	@observable refreshData = new Date().getTime();
	/********************* observable list *********************/

	/********************* computed list *********************/

	/********************* computed list *********************/

	/********************* action list *********************/
	initData = () => {
		this.selectedSubCompany = null;
		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}
		this.getForm();
	}

	getForm = (params) => {
		api.getForm(params).then(data => {
			if (data.status === '1') {
				this.formId = data.id;
				data.formField[0].items[0].min = 1;
				data.formField[0].items[0].max = 999999999;
				this.setFormData('validDateForm', data.formField);
			} else {
				message.error(data.message);
			}
			this.refreshData = new Date().getTime();
		}, error => {
			message.error(i18n.message.actionError())
		})
	}

	@action treeNodeClick = (event, ids, nodeids, nodes) => {
		const {
			data: nodeData
		} = event.node.props;
		this.selectedSubCompany = nodeData;
		this.getForm({
			subcompanyid: nodeData.id
		});
	}

	@action save = () => {
		this.formTarget.validDateForm.validateForm().then(f => {
			if (f.isValid) {
				const params = this.formTarget.validDateForm.getFormParams();
				Object.assign(params, {
					field002: '1',
					id: this.formId,
					field001: this.selectedSubCompany == null ? '0' : this.selectedSubCompany.id
				})
				api.save(params).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.saveSuccess());
					} else {
						message.error(data.message);
					}
				}, error => {
					message.error(i18n.message.actionError())
				})
			} else {
				f.showErrors();
				this.showError = new Date().getTime();
			}
		})
	}

	@action sync = () => {
		this.formTarget.validDateForm.validateForm().then(f => {
			if (f.isValid) {
				this.confirmInfo({
					content: i18n.confirm.syncToSubDept(),
					onOk: () => {
						const params = this.formTarget.validDateForm.getFormParams();
						Object.assign(params, {
							field002: '1',
							field001: this.selectedSubCompany == null ? '0' : this.selectedSubCompany.id
						})
						api.sync(params).then(data => {
							if (data.status === '1') {
								message.success(i18n.message.syncSuccess());
							} else {
								message.error(data.message);
							}
						}, error => {
							message.error(i18n.message.actionError())
						})
					}
				});
			} else {
				f.showErrors();
				this.showError = new Date().getTime();
			}
		})
	}
	/********************* action list *********************/
}

export const hrmPaidLevel = new HrmPaidLevel();