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

export class HrmOnlineAttendance extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		let btns = [
			<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bfjqkp`} type='primary' op='create' onClick={() => this.save()}>{i18n.button.save()}</Button>,
		]
		let menus = [];
		btns.map((btn, index) => {
			const {
				props
			} = btn;
			menus.push({
				key: index.toString(),
				content: props.children,
				icon: <i className={this.menuIconCollection[props.op]}/>,
				onClick: props.onClick,
				disabled: props.disabled || false
			});
		})
		return {
			btns,
			menus: [...menus, ...this.getBasicMenus('HRM_ENGINE_ONLINE_KQ_SYSTEMSET')]
		}
	}
	signIpScopeTips;
	signTimeScopeTips;

	itemRender = {
		needSign: (field, textAreaProps, form, formParams) => {
			return (
				<div className='horLayout'>
					<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@tt5p6o`} fieldConfig={{...field, ...textAreaProps}} form={form} formParams={formParams}/>
				</div>
			)
		},
		signTimeScope_start: (field, textAreaProps, form, formParams) => {
			return (
				<div>
					<div style={{width: '70%'}}>
						<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@d9v1h6`} fieldConfig={{...field, ...textAreaProps}} form={form} formParams={formParams}/>
					</div>
					<div>{this.signTimeScopeTips}</div>
				</div>
			)
		},
		signIpScope: (field, textAreaProps, form, formParams) => {
			return (
				<div>
					<div style={{width: '70%'}}>
						<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@0lgkvk`} fieldConfig={{...field, ...textAreaProps}} form={form} formParams={formParams}/>
					</div>
					<div>{this.signIpScopeTips}</div>
				</div>
			)
		}
	}
	/********************* unobservable list *********************/

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

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

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

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

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

	getForm = () => {
		api.getForm().then(data => {
			if (data.status === '1') {
				this.signIpScopeTips = data.signIpScopeTips;
				this.signTimeScopeTips = data.signTimeScopeTips;
				let formField = data.formField;
				formField.map(g => {
					g.items.map(item => {
						if(item.domkey[0] === 'signTimeScope_start' && item.value.length > 0){
							item.startValue = item.value[0];
							item.endValue = item.value[1];
						}
					})
				})
				this.setFormData('dataForm', formField);
				this.refreshForm = new Date().getTime();
			} else {
				message.error(data.message);
			}
		}, error => {})
	}

	@action save = () => {
		this.formTarget.dataForm.validateForm().then(f => {
			if (f.isValid) {
				const formParams = this.formTarget.dataForm.getFormParams()
				const params = {
					...formParams,
					signTimeScope: formParams['signTimeScope_start'] + '-' + formParams['signTimeScope_end']
				}
				api.save(params).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.saveSuccess());
					} else {
						message.error(data.message);
					}
				}, error => {})
			} else {
				f.showErrors();
				this.showError = new Date().getTime();
			}
		});
	}

	/********************* action list *********************/
}

export const hrmOnlineAttendance = new HrmOnlineAttendance();