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

class HrmImportExternalAttendance extends HrmBaseStore {
	/********************* unobservable list *********************/
	hrmImportResource;
	setImportResource = (hrmImportResource) => {
		this.hrmImportResource = hrmImportResource;
	}

	topBtnAndMenu = () => {
		return {
			btns: [
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mcf50m`} type='primary' onClick={this.submit}>{i18n.button.submit()}</Button>
			],
			menus: [{
				key: '1',
				content: i18n.button.submit(),
				icon: <i className={this.menuIconCollection.save}/>,
				onClick: this.submit
			}, ...this.getBasicMenus('HRM_ENGINE_HRM_SCHEDULESIGN_IMPORT')]
		}
	}
	/********************* unobservable list *********************/

	/********************* dialog info setting *********************/

	/********************* dialog info setting *********************/

	/********************* observable list *********************/
	@observable message = [];
	/********************* observable list *********************/

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

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

	/********************* action list *********************/
	initData = () => {
		api.getImportForm().then(data => {
			if (data.status === '1') {
				this.hrmImportResource.condition = data.condition;
				this.hrmImportResource.excelfile = '';
				this.hrmImportResource.filelist.length = 0;
				this.message.length = 0;
				this.hrmImportResource.single = true;
				this.hrmImportResource.visible = true;
				this.containerInitFinished = {
					...this.containerInitFinished,
					init: true
				}
			}
		})
	}

	@action submit = () => {
		this.message.length = 0;
		const keyField = this.hrmImportResource.importParams.keyField;
		const excelfile = this.hrmImportResource.excelfile || '';
		if (excelfile === '') {
			message.warning(i18n.message.selectImportTemplete());
			return;
		}
		api.importExternalData({
			keyField,
			excelfile
		}).then(data => {
			if (data.status === '1') {
				if (has(data, 'errorInfo')) {
					this.message = data.errorInfo;
				} else
					this.message = [data.message];
			}
		}, error => {})
	}

	resetImportResource = () => {
		this.hrmImportResource.single = false;
		this.hrmImportResource.visible = false;
	}
	/********************* action list *********************/
}

export const hrmImportExternalAttendance = new HrmImportExternalAttendance();