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

class HrmPaidLevelTimeSetting extends HrmBaseStore {
	/********************* unobservable list *********************/
	topBtnAndMenu = () => {
		return {
			btns: [],
			menus: [
				// {
				// 	key: '1',
				// 	content: i18n.button.search(),
				// 	icon: <i className={this.menuIconCollection.search}/>,
				// 	onClick: this.search
				// },
				{
					key: '2',
					content: i18n.button.columnVisibleSetting(),
					icon: <i className={this.menuIconCollection.setting}/>,
					onClick: () => this.definedColumn(this.table)
				}, ...this.getBasicMenus()
			]
		}
	}

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

	tabConfig = {
		tabs: [{
			color: '#000000',
			groupId: 'basic',
			viewCondition: '1',
			searchType: ['advanced'],
			doSearch: (params) => this.search(params)
		}],
		keyParam: 'viewCondition',
		activeTabKey: '1'
	}

	selectedSubCompany;
	formId;

	formField = [{
		"title": "",
		"defaultshow": true,
		"items": [{
			"browserConditionParam": {
				"completeParams": {},
				"conditionDataParams": {},
				"dataParams": {},
				"destDataParams": {},
				"hasAddBtn": false,
				"hasAdvanceSerach": true,
				"hasBorder": false,
				"hideVirtualOrg": false,
				"idSeparator": ",",
				"isAutoComplete": 1,
				"isDetail": 0,
				"isMultCheckbox": false,
				"isSingle": true,
				"linkUrl": "",
				"pageSize": 10,
				"quickSearchName": "",
				"replaceDatas": [],
				"showCheckStrictly": true,
				"title": "日期",
				"type": "2",
				"viewAttr": 2
			},
			"colSpan": 2,
			"conditionType": "DATE",
			"detailtype": 1,
			"domkey": [
				"createdateselect",
				"fromdate",
				"enddate"
			],
			"fieldcol": 18,
			"isQuickSearch": false,
			"label": i18n.label.dateRangeCondition,
			"labelcol": 6,
			"options": [{
				key: '0',
				showname: i18n.label.all,
			}, {
				key: '1',
				showname: i18n.label.today
			}, {
				key: '2',
				showname: i18n.label.tswk
			}, {
				key: '3',
				showname: i18n.label.tsmth
			}, {
				key: '7',
				showname: i18n.label.preMonth
			}, {
				key: '4',
				showname: i18n.label.tssn
			}, {
				selected: true,
				key: '5',
				showname: i18n.label.tsyr
			}, {
				key: '8',
				showname: i18n.label.preYear
			}, {
				key: '6',
				showname: i18n.label.dateRange
			}],
			"precision": 0,
			"showOrder": 0,
			"viewAttr": 2,
		}]
	}];
	/********************* unobservable list *********************/

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

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

	/********************* observable list *********************/
	@observable dateRange = {
		fromdate: '',
		enddate: ''
	}
	@observable table = new TableStore();
	@observable refreshData = new Date().getTime();
	@observable refreshTab = new Date().getTime();
	/********************* observable list *********************/

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

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

	/********************* action list *********************/
	initData = () => {
		this.selectedSubCompany = null;
		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}
		this.setFormFieldTitle();
		this.setFormData('conditionForm', this.formField);
		this.refreshTab = new Date().getTime();
		this.search();
	}

	setFormFieldTitle = () => {
		this.formField.map(f => {
			f.items.map(item => {
				if (typeof(item.label) == 'function')
					item.label = item.label();
				item.options.map(o => {
					if (typeof(o.showname) == 'function')
						o.showname = o.showname();
				})
			})
		})
	}

	@action treeNodeClick = (event, ids, nodeids, nodes) => {
		this.formTarget.conditionForm.updateFields({
			createdateselect__fromdate__enddate: {
				value: ['5']
			}
		});
		this.refreshTab = new Date().getTime();
		const {
			data: nodeData
		} = event.node.props;
		this.selectedSubCompany = nodeData;
		this.search();
	}

	search = () => {
		let formParams = this.formTarget.conditionForm.getFormParams();
		if (formParams.createdateselect === '6' && (formParams.fromdate == null || formParams.fromdate === '' || formParams.enddate == null || formParams.enddate === '')) {
			this.formTarget.conditionForm.updateFields({
				createdateselect__fromdate__enddate: {
					value: ['5']
				}
			});
		}
		formParams = this.formTarget.conditionForm.getFormParams();
		const params = {
			...formParams
		}

		if (this.selectedSubCompany != null) {
			if (this.selectedSubCompany.type === '1') {
				Object.assign(params, {
					subcompanyid: this.selectedSubCompany.id
				})
			} else if (this.selectedSubCompany.type === '2') {
				Object.assign(params, {
					subcompanyid: this.selectedSubCompany.pid,
					departmentid: this.selectedSubCompany.id
				})
			}
		}
		api.getList(params).then(data => {
			if (data.status === '1') {
				this.table.getDatas(data.sessionkey, 1);
			} else {
				message.error(data.message);
			}
		}, error => {})
	}
	/********************* action list *********************/
}

export const hrmPaidLevelTimeSetting = new HrmPaidLevelTimeSetting();