import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaSearchGroup,
	WeaFormItem,
	WeaCheckbox,
	WeaRangePicker,
	WeaHelpfulTip,
	WeaTableEdit
} from 'ecCom';
import {
	WeaForm,
	WeaTableNew,
	WeaSwitch
} 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 HrmAttendanceSyncSetting extends HrmBaseStore {
	/********************* unobservable list *********************/
	editTable = {};
	topBtnAndMenu = () => {
		return {
			btns: [
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4lc4ah`} type='primary' onClick={this.save}>{i18n.label.save()}</Button>
			],
			menus: [{
				key: '1',
				content: i18n.label.save(),
				icon: <i className={this.menuIconCollection.save}/>,
				onClick: this.save
			}, ...this.getBasicMenus("HRM_ENGINE_HRM_SCHEDULESIGN_IMPORT")]
		}
	}

	tableEditConfig = {
		...this.editTableConfig,
		showTitle: true,
		draggable: false,
		showAdd: true,
		showDelete: true,
		showCopy: true,
		columns: [],
		datas: [],
		selectedData: {},
		copyFilterProps: ['datasourceid', 'tablename', 'workcode', 'lastname', 'signdate', 'signtime', 'clientaddress', 'com.lastname'],
		onChange: this.onTableDataChange
	}

	syncDate;
	errorRef;
	tableStateData;
	/********************* unobservable list *********************/

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

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

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

	@observable isVaild = false;
	@observable spinning = false;

	@observable syncSettingFormCondition = {};
	@observable fieldSettingFormCondition = {};
	@observable fieldSettingExplainConditon = {};
	/********************* observable list *********************/

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

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

	/********************* action list *********************/
	initData = () => {
		api.getImportFormSet().then(data => {
			if (data.status === '1') {
				data.group.map((g, i) => {
					switch (i) {
						case 0:
							this.isVaild = g.items[0].isVaild;
							this.syncSettingFormCondition = g;
							break;
						case 1:
							this.fieldSettingFormCondition = g;
							const {
								columns,
								datas
							} = g.items[0].table;
							this.tableEditConfig.columns = columns;
							this.tableEditConfig.datas = datas;
							this.refreshTableData = new Date().getTime();
							break;
						case 2:
							this.fieldSettingExplainConditon = g;
							break;
						case 3:
							break;
						case 4:
							break;
					}
				})
				this.containerInitFinished = {
					...this.containerInitFinished,
					init: true
				}
			}
		})
	}

	setValid = (val) => {
		this.isVaild = val === '0' ? false : true;
	}

	setSyncDate = (date) => {
		this.syncDate = {
			fromdate: date[0],
			enddate: date[1]
		}
	}

	setErrorRef = (ref) => {
		this.errorRef = ref;
	}

	/**
	 * 设置WeaTableEdit的refs对象
	 * @param  {Object} refs [WeaTableEdit]
	 */
	setEditTable = (refs, name) => {
		this.editTable[name] = refs;
	}

	@action sync = () => {
		if (this.syncDate == null || !has(this.syncDate, 'fromdate') || this.syncDate.fromdate === '') {
			this.errorRef.showError();
			return;
		}
		if(this.spinning)
			return;
		this.spinning = true;
		api.syncData(this.syncDate).then(data => {
			if (data.status === '1') {
				message.success(data.message)
			} else {
				message.error(data.message)
			}
			this.spinning = false;
		}, error => {this.spinning = false;})
	}

	@action onTableDataChange = (datas) => {
		this.tableStateData = datas;
		// this.tableEditConfig.datas = datas;
		// this.refreshTableData = new Date().getTime()
	}

	@action
	recordOP = (target, type, callback = null) => {
		switch (type) {
			case 'add':
				target.refs.edit.doAdd()
				break;
			case 'remove':
				target.refs.edit.doDelete()
				break;
			case 'copy':
				target.refs.edit.doCopy()
				break;
			case 'valid':
				return target.refs.edit.doRequiredCheck()
			default:
				break;
		}
	}

	@action save = () => {
		const {
			pass,
			index,
			key
		} = this.recordOP(this.editTable['fieldDef'], 'valid');
		if (!pass || this.tableEditConfig.columns === 0)
			return;
		let obj = {};
		this.tableEditConfig.columns.map(c => {
			Object.assign(obj, {
				[c.dataIndex]: []
			})
			const datas = this.tableStateData == null ? this.tableEditConfig.datas : this.tableStateData;
			datas.map(d => {
				obj[c.dataIndex].push(d[c.dataIndex]);
			})
		})
		const keys = Object.keys(obj);
		keys.map(k => {
			obj[k] = obj[k].join(',');
		})
		const params = {
			...obj,
			isVaild: this.isVaild ? '1' : '0'
		}
		api.saveImportSet(params).then(data => {
			if (data.status === '1') {
				message.success(i18n.message.saveSuccess());
				this.tableEditConfig.selectedData = {};
				this.tableEditConfig.selectedRowKeys = [];
				this.initData();
			} else {
				message.error(data.message);
			}
		}, error => {})
	}
	/********************* action list *********************/
}

export const hrmAttendanceSyncSetting = new HrmAttendanceSyncSetting();