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 *********************/
	topBtnAndMenu = () => {
		return {
			btns: [
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@kje20j`} type='primary' onClick={this.save}>{i18n.button.save()}</Button>,
				<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@4np2yn`} type='primary' onClick={this.sync}>{i18n.button.immediateSync()}</Button>
			],
			menus: [{
				key: '1',
				content: i18n.button.save(),
				icon: <i className={this.menuIconCollection.save}/>,
				onClick: this.save
			}, {
				key: '2',
				content: i18n.button.immediateSync(),
				icon: <i className={this.menuIconCollection.copy}/>,
				onClick: this.sync
			}, ...this.getBasicMenus()]
		}
	}

	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;
	}

	@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 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();