import {
	observable,
	action,
	computed
} from 'mobx';
import {
	WeaForm,
	WeaTableNew
} from 'comsMobx';
import {
	WeaHelpfulTip,
	WeaCheckbox
}
from 'ecCom';
import {
	WeaSwitch
} from 'comsMobx';
import {
	Button,
	message
} from 'antd';
import HrmBaseStore from './baseStore';
import * as api from '../apis/authAdjust';
import {
	i18n
} from '../public/i18n';
import {
	cloneDeep,
	has,
	indexOf,
	remove,
	isEmpty,
	dropRight
} from 'lodash';

const {
	TableStore
} = WeaTableNew;

export class HrmAuthAdjust extends HrmBaseStore {
	/********************* unobservable list *********************/
	getMenu = () => {
		const logConfig = {
			key: 'log',
			content: i18n.button.log(),
			icon: <i className='icon-coms-Print-log'/>,
			onClick: () =>{
				window.setLogViewProps({
					logSmallType:3073
				})
			} ,
		}
		if (this.showResult) {
			return [{
				key: '1',
				content: i18n.button.goBack(),
				icon: <i className='icon-coms-go-back'/>,
				onClick: this.resetData,
			},logConfig, ...this.getBasicMenus()]
		}
		return [{
			key: '1',
			content: i18n.button.doExcute(),
			icon: <i className='icon-coms-Browse-box-add-all'/>,
			onClick: this.doExcute,
		},logConfig, ...this.getBasicMenus()]
	}

	tabConfig = {
		tabs: [{
			color: '#000000',
			groupId: '1',
			title: '',
			viewCondition: '1',
			searchType: ['base', 'advanced'],
			searchKey: '',
			doSearch: (params) => this.doSearch(params)
		}],
		tabChangeHandle: this.setActiveTab,
		keyParam: 'viewCondition',
		activeTabKey: '1'
	}


	tableConfig = {
		...this.editTableConfig,
		draggable: false,
		showAdd: false,
		showDelete: false,
		showCopy: false,
		columns: [{
			title: i18n.label.type, //列名
			dataIndex: 'type', //列的id 对应数据
			key: 'type', //前端渲染key值
			colSpan: 1,
			width: '15%',
			com: [{
				label: '',
				type: 'TEXT',
				key: 'fieldlabel',
				viewAttr: '3',
			}]
		}, {
			title: i18n.label.all,
			dataIndex: 'all',
			key: 'all',
			checkType: 'checkbox',
			colSpan: 1,
			width: '15%',
			com: [{
				label: '',
				type: 'TEXT',
				key: 'all',
			}],
		}, {
			title: i18n.label.select,
			dataIndex: 'select',
			key: 'select',
			colSpan: 1,
			width: '15%',
			useRecord: true
		}, {
			title: i18n.label.selectedCount,
			dataIndex: 'selectCount',
			key: 'selectCount',
			colSpan: 1,
			width: '15%',
			com: [{
				label: '',
				type: 'TEXT',
				key: 'selectCount',
				viewAttr: '3',
			}]
		}, {
			title: i18n.label.totalCount,
			dataIndex: 'totalCount',
			key: 'totalCount',
			colSpan: 1,
			width: '40%',
			useRecord: true
		}],
		datas: [],
		selectedData: {},
		onRowSelect: this.onRowSelect,
		getRowSelection: (rowSelection) => {
			return null;
		}
	}

	fromFormItemRender = {
		transferType: (field, textAreaProps, form, formParams) => {
			return (
				<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@qua11k`} fieldConfig={{...field, ...textAreaProps, hasBorder: field.viewAttr === '1'}} form={form} formParams={formParams} onChange={this.onChangeTransferType}/>
			)
		},
		resourceid: (field, textAreaProps, form, formParams) => {
			const domKey = `${formParams.transferType}id`;
			if (domKey === field.domkey[0]) {
				return (
					<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@w7sci6`} key={new Date().getTime()} fieldConfig={{...field, ...textAreaProps, hasBorder: field.viewAttr === '1'}} form={form} formParams={formParams} onChange={obj => this.onBrowserChangeHandle(obj, 'from')}/>
				)
			}
			return;
		},
		departmentid: (field, textAreaProps, form, formParams) => this.fromFormItemRender.resourceid(field, textAreaProps, form, formParams),
		subcompanyid: (field, textAreaProps, form, formParams) => this.fromFormItemRender.resourceid(field, textAreaProps, form, formParams),
		jobtitleid: (field, textAreaProps, form, formParams) => this.fromFormItemRender.resourceid(field, textAreaProps, form, formParams),
		roleid: (field, textAreaProps, form, formParams) => this.fromFormItemRender.resourceid(field, textAreaProps, form, formParams)
	}

	toFormItemRender = {
		authorityTag: (field, textAreaProps, form, formParams) => {
			return (
				<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@yt3dtf`} fieldConfig={{...field, ...textAreaProps, hasBorder: field.viewAttr === '1'}} form={form} formParams={formParams}  onChange={this.onChangeAuthorityTag}/>
			)
		},
		resourceid: (field, textAreaProps, form, formParams) => {
			if (`${this.formTarget.fromForm.getFormParams().transferType}id` === field.domkey[0] &&
				this.formTarget.toForm.getFormParams().authorityTag != 'delete') {
				return (
					<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@shurou`} fieldConfig={{...field, ...textAreaProps, hasBorder: field.viewAttr === '1'}} form={form} formParams={formParams} onChange={obj => this.onBrowserChangeHandle(obj, 'to')}/>
				)
			}
			return;
		},
		departmentid: (field, textAreaProps, form, formParams) => this.toFormItemRender.resourceid(field, textAreaProps, form, formParams),
		subcompanyid: (field, textAreaProps, form, formParams) => this.toFormItemRender.resourceid(field, textAreaProps, form, formParams),
		jobtitleid: (field, textAreaProps, form, formParams) => this.toFormItemRender.resourceid(field, textAreaProps, form, formParams),
		roleid: (field, textAreaProps, form, formParams) => this.toFormItemRender.resourceid(field, textAreaProps, form, formParams)
	}

	otherListCondition = {};
	opImg = 'authAdjust';
	sqlCollection = {}
	fields = [];
	resultMessage = {};
	selectedTableItem = {
		record: {},
		index: -1
	}
	refreshDatInterval;
	processFinished = false;
	transferInfo = {
		fromid: '',
		fromName: '',
		toid: '',
		toName: '',
		transferType: '',
		authorityTag: '',
		clear: () => {
			Object.keys(this.transferInfo).map(key => {
				if (typeof this.transferInfo[key] != 'function') {
					if (key.indexOf('All') >= 0 || key.indexOf('IdStr') >= 0)
						delete this.transferInfo[key];
					else
						this.transferInfo[key] = '';
				}
			})
			this.clearTableData();
			this.resetState();
		},
		clearSelectData: () => {
			Object.keys(this.transferInfo).map(key => {
				if (typeof this.transferInfo[key] != 'function') {
					if (key.indexOf('All') >= 0 || key.indexOf('IdStr') >= 0)
						delete this.transferInfo[key];
				}
			})
		},
		transferInfoChange: () => {
			this.transferInfo.fromid = '';
			this.transferInfo.fromName = '';
			this.transferInfo.toid = '';
			this.transferInfo.toName = '';
			this.clearTableData();
		}
	}

	clearTableData = () => {
		this.tableConfig.datas.length = 0;
		this.tableConfig.selectedData = {};
		this.refreshTable = new Date().getTime();
		this.fromCount = 0;
		this.toCount = 0;
	}

	clearFormData = () => {
		const d = {
			resourceid: {
				value: ''
			},
			departmentid: {
				value: ''
			},
			subcompanyid: {
				value: ''
			},
			jobtitleid: {
				value: ''
			},
			roleid: {
				value: ''
			}
		}
		this.formTarget.fromForm.updateFields(d, false);
		this.formTarget.toForm.updateFields(d, false);
	}
	/********************* unobservable list *********************/
	/********************* observable list *********************/
	@observable refreshTable = new Date().getTime();
	@observable refreshDialog = new Date().getTime();
	@observable refreshForm = new Date().getTime();
	@observable refreshResult = new Date().getTime();

	@observable fromCount = 0;
	@observable toCount = 0;
	@observable showSpin = false;
	@observable showResult = false;
	@observable timer = 0;

	@observable loading = false;

	@observable tableList = {}
	@observable treeProps = {
		count: 0,
		checkedKeys: []
	}

	dialogParams = { //override baseStore.dialogParams
		...this.dialogPropsDef,
		style: {
			width: 820,
			height: 650
		},
		buttons: [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@m5c6f0`} type="primary" onClick={() => this.done(this.dialogParams.table)}>{i18n.button.done()}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@5b9bno`} type="primary" onClick={() => this.done(this.dialogParams.table, true)}>{i18n.button.selectAll()}</Button>)
		],
		onCancel: () => {
			this.dialogParams.visible = false;
			this.treeProps.checkedKeys.length = 0;
			this.dialogParams.errorMsg = null;
		},
		table: new TableStore(),
		treeData:  [],
		cType: 'table'
	}
	/********************* observable list *********************/
	/********************* action list *********************/
	initData = () => {
		this.containerInitFinished = {
			...this.containerInitFinished,
			init: true
		}

		this.loading = false;
		this.transferInfo.clearSelectData();
		this.getFormInfo();
	}

	setTableColTitle = () => {
		this.tableConfig.columns.map(c => {
			if (typeof(c.title) == 'function')
				c.title = c.title();
		})
	}

	resetState = () => {
		this.showResult = false;
		this.processFinished = false;
		this.fromCount = 0;
		this.toCount = 0;
		clearInterval(this.refreshDatInterval);
	}

	getFormInfo = () => {
		api.getFormInfo().then(data => {
			if (data.status === '1') {
				if (has(data, 'formField') && data.formField.length > 0) {
					this.fields = data.formField[0].items;
					this.initForm();
					this.resetState();
				}
			}
		}, error => {

		})
	}

	initForm = (disabled = false) => {
		if (!disabled) this.transferInfo.clear();
		if (this.fields.length > 0) {
			const formArr = ['fromForm', 'toForm'];
			formArr.map(name => {
				const isFrom = name === 'fromForm';
				const cloneObj = cloneDeep(this.fields);
				let f = [];
				const transferTypeField = cloneObj[0],
					authorityTagField = cloneObj[6];
				if (disabled) {
					authorityTagField.options.map(item => item.selected = item.key === this.transferInfo.authorityTag)
					Object.assign(transferTypeField, {
						viewAttr: '1'
					});
					Object.assign(authorityTagField, {
						viewAttr: '1'
					});
				}
				isFrom ? f.push(transferTypeField) : f.push(authorityTagField);
				for (let i = 0; i < cloneObj[0].options.length; i++) {
					const browserField = cloneObj[i + 1];
					if (browserField != null) {
						disabled && Object.assign(browserField, {
							viewAttr: '1'
						});
						f.push(browserField);
					}
				}
				const formFields = [{
					title: '基本信息',
					defaultshow: true,
					items: f
				}]
				this.setFormData(name, formFields);
				if (this.formTarget[name].isFormInit) {
					const formParams = this.formTarget[name].getFormParams();
					if (isFrom) {
						this.transferInfo.transferType === '' && Object.assign(this.transferInfo, {
							transferType: this.formTarget[name].getFormParams().transferType
						})
						this.formTarget[name].updateFields({
							transferType: {
								value: this.transferInfo.transferType
							},
							[`${this.transferInfo.transferType}id`]: {
								value: this.transferInfo.fromid,
								valueSpan: this.transferInfo.fromName,
								valueObj: [{
									id: this.transferInfo.fromid,
									name: this.transferInfo.fromName
								}]
							}
						})
					} else {
						this.transferInfo.authorityTag === '' && Object.assign(this.transferInfo, {
							authorityTag: this.formTarget.toForm.getFormParams().authorityTag
						})
						this.formTarget[name].updateFields({
							authorityTag: {
								value: this.transferInfo.authorityTag
							},
							[`${this.transferInfo.transferType}id`]: {
								value: this.transferInfo.toid,
								valueSpan: this.transferInfo.toName,
								valueObj: [{
									id: this.transferInfo.toid,
									name: this.transferInfo.toName
								}]
							}
						})
					}
				}
			})
		}

		this.refreshForm = new Date().getTime();
	}

	onBrowserChangeHandle = (obj, source) => {
		const keys = Object.keys(obj);
		const fk = keys[0],
			value = obj[fk].value,
			valueSpan = obj[fk].valueSpan;
		this.transferInfo[`${source}id`] = value;
		this.transferInfo[`${source}Name`] = valueSpan;
		if (source === 'from') {
			this.sqlCollection = {};
			this.transferInfo.clearSelectData();
			this.clearTableData();
			if (value != '')
				this.initTable(this.transferInfo);
			else
				this.clearFormData();
		}

	}

	@action onChangeTransferType = (obj) => {
		this.clearFormData();
		this.sqlCollection = {};
		this.transferInfo.clearSelectData();
		this.refreshForm = new Date().getTime();
		this.transferInfo.transferType = obj.transferType.value;
		this.transferInfo.transferInfoChange();
	}

	@action onChangeAuthorityTag = (obj) => {
		this.sqlCollection = {};
		this.transferInfo.clearSelectData();
		this.refreshForm = new Date().getTime();
		this.transferInfo.authorityTag = obj.authorityTag.value;
		this.opImg = 'authAdjust';
		if (this.transferInfo.authorityTag === 'delete') {
			this.transferInfo.toid = '';
			this.transferInfo.toName = '';
			this.opImg = 'authRemove';
			this.formTarget.toForm.updateFields({
				[`${this.transferInfo.transferType}id`]: {
					value: this.transferInfo.toid
				}
			})
		} else if (this.transferInfo.fromid === '') {
			return;
		}

		this.clearTableData();
		this.initTable(this.transferInfo);
	}

	initTable = (transferInfo) => {
		api.checkFromId({
			fromid: transferInfo.fromid
		}).then(data => {
			if (data.status === '1') {
				const excutable = data.result === '0';
				if (excutable)
					this.callTableData(transferInfo);
				else
					message.warning(`${transferInfo.fromName}${i18n.label.userDatas()}${i18n.message.serverProcessing()}`);
			} else {
				message.error(data.message);
			}
		})
	}

	callTableData = (transferInfo) => {
		this.loading = true;
		this.setTableColTitle();
		api.getTableData({
			fromid: transferInfo.fromid,
			authorityTag: transferInfo.authorityTag,
			transferType: transferInfo.transferType
		}).then(data => {
			if (data.status === '1') {
				let source = [];
				data.datas.map((d, index) => {
					let obj = {
						...d,
						all: i18n.label.all(),
						id: this.tableConfig.datas.length + 1
					}
					let com = {
						totalCount: [
							<div className='total'>
								<div className='countNum'>
									{obj.totalCount}
								</div>
								<div className='helper'>
									<WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@471zj4@${index}`} title={obj.tips}/>
								</div>
							</div>
						]
					}
					const hasCanSelectProp = has(d, 'canSelect');
					if (!hasCanSelectProp || (hasCanSelectProp && d.canSelect))
						Object.assign(com, {
							select: [
								<a onClick={() => this.onSelectHandle(obj, index)}>{i18n.label.select()}</a>
							]
						})
					obj.com = com;
					source.push(obj);
				})
				this.tableConfig.datas = source;
				this.refreshTable = new Date().getTime();
			}
			this.loading = false;
		}, error => {
			this.loading = false;
		})
	}

	calculateFromCountCount = () => {
		let count = 0;
		this.tableConfig.datas.map((data, index) => {
			count += data.selectCount;
		})
		this.fromCount = count;
	}

	@action onRowSelect = (sRowKeys, rows, dataIndex, selectedDatas) => {
		if (selectedDatas != null && selectedDatas.hasOwnProperty(dataIndex)) {
			const arr = selectedDatas[dataIndex];
			let count = 0;
			this.tableConfig.datas.map((data, index) => {
				const oldVal = this.transferInfo[`${data.codeName}All`];
				const newVal = indexOf(arr, index) >= 0 ? '1' : '0';
				this.transferInfo[`${data.codeName}All`] = newVal;
				if (newVal === '1') {
					data.selectCount = data.totalCount;
				} else if (oldVal != null && oldVal != newVal) {
					data.selectCount = 0;
					delete this.transferInfo[`${data.codeName}IdStr`];
					delete this.sqlCollection[data.codeName];
				}
				count += data.selectCount;
			})

			this.fromCount = count;
			this.tableConfig.selectedData = selectedDatas;
		}
		this.refreshTable = new Date().getTime();
	}

	getTableSqlParams = (tableName) => {
		if (!has(this.sqlCollection, tableName)) {
			this.sqlCollection[tableName] = {
				njson: '',
				ojson: '',
				jsonSql: ''
			}
		}
		return this.sqlCollection[tableName];
	}

	convertTreeData = (datas, pid = 0) => {
		let newDatas = [];
		datas && !isEmpty(datas) && datas.map((item, index) => {
		for (let key in datas[index]) {
			let v = datas[index][key];
			let canClick = true;
			if(v.canClick != null)
				canClick = v.canClick;
			try{
				newDatas.push({
					childs: this.convertTreeData(v.submenus, String(v.categoryid)),
					domid: String(v.categoryid),
					haschild: v.hasChildren,
					isopen: v.isOpen,
					key: String(v.categoryid),
					name: v.name,
					pid: pid,
					canClick
				})
			}catch(e){
			}
		}
		})
		return newDatas;
	}

	setTreeCheckedKeys = (ids) => {
		this.treeProps.checkedKeys = ids;
	}

	@action setActiveTab = (key) => {
		this.tabConfig.activeTabKey = key || this.tabConfig.tabs[0].viewCondition;
		const tabIndex = this.getTabIndex(this.tabConfig.tabs, key);
		if (tabIndex < 0) return;
		this.refreshDialog = new Date().getTime();
		const formParams = this.formTarget.conditionForm.getFormParams();
		this.doSearch({...formParams});
	}

	onCheckBoxChange = (val, key) => {
		this.otherListCondition[key] = val;
		const formParams = this.formTarget.conditionForm.getFormParams();
		this.doSearch({...formParams});
	}

	@action onSelectHandle = (record, index) => {
		this.selectedTableItem = {
			record,
			index
		};
		const tableName = record.codeName;
		const params = {
			fromid: this.transferInfo.fromid,
			transferType:this.transferInfo.transferType
		}
		let tableSqlParams = this.getTableSqlParams(tableName);
		Object.assign(params, {
			jsonSql: this.sqlCollection[tableName].jsonSql,
			isAll: has(this.transferInfo, `${tableName}All`) && this.transferInfo[`${tableName}All`] === '1'
		});
		if(record.codeName === 'Temail001'){
			Object.assign(params, {
				folderid: 0
			})
		}else if(record.codeName === 'Temail002'){
			Object.assign(params, {
				folderid: -1
			})
		}
		delete this.formTarget.conditionForm;
		delete this.formTarget.conditionFormFields;
		this.otherListCondition = {};
		api.initModuleData(record.codeName, params).then(rs => {
			try {
				rs.map((result, index) => {
					if (result.status === '1') {
						switch (index) {
							case 0:
								if(this.tabConfig.tabs.length > 1)
									this.tabConfig.tabs = dropRight(this.tabConfig.tabs, this.tabConfig.tabs.length - 1);
								this.tabConfig.tabs[0].groupId = '1';
								this.tabConfig.tabs[0].title = '';
								this.tabConfig.activeTabKey = '1';
								this.tabConfig.tabSearchKey = '';
								this.tabConfig.buttons = [];
								this.setFormData('conditionForm', result.condition);
								if(Array.isArray(result.condition) && result.condition[0].items != null){
									this.tabConfig.tabs[0].searchType = ['base', 'advanced']
									this.tabConfig.tabs[0].searchKey = result.condition[0].items[0].domkey[0];
								}else{
									this.tabConfig.tabs[0].searchType = ['base'];
									this.tabConfig.tabs[0].searchKey = 'categoryname'
								}
								if(has(result, 'dialogProps')){
									this.dialogParams = {...this.dialogParams, title: `${i18n.label.select()}${record.type}`, ...result.dialogProps}
								}else{
									this.dialogParams = {...this.dialogParams, ...this.dialogPropsDef, title: `${i18n.label.select()}${record.type}`}
								}
								if(has(result, 'tab')){
									result.tab.map((tab, i) => {
										if(i === 0){
											Object.assign(this.tabConfig.tabs[0], {
												title: tab.title,
												viewCondition: tab.key,
												groupId: tab.key
											})
										}else{
											const t = cloneDeep(this.tabConfig.tabs[0]);
											Object.assign(t, {
												title: tab.title,
												viewCondition: tab.key,
												groupId: tab.key
											})
											this.tabConfig.tabs.push(t);
										}
									})
									this.tabConfig.tabSearchKey = result.tabSearchKey;
								}
								if(has(result, 'tabBtns')){
									let btns = [];
									result.tabBtns.map((def, index) => {
										btns.push(<WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@strvqh@${index}`} {...def} onChange={(val) => this.onCheckBoxChange(val, def.id)} />);
									})
									this.tabConfig.buttons = btns;
								}
								break;
							case 1:
								tableSqlParams = { ...tableSqlParams,
									njson: result.njson,
									ojson: result.ojson
								}
								if (has(result, 'sessionkey')) {
									this.sqlCollection[tableName] = tableSqlParams;
									this.tableList[tableName] = new TableStore();
									this.tableList[tableName].getDatas(result.sessionkey, 1);
									this.dialogParams.cType = 'table';
									this.dialogParams.table = this.tableList[tableName];
									if (result.selectedstrs.length > 0) {
										this.dialogParams.table.setSelectedRowKeys(result.selectedstrs.split(','));
									}else if (this.transferInfo[`${record.codeName}IdStr`] && this.transferInfo[`${record.codeName}IdStr`].length > 0) {
										this.dialogParams.table.setSelectedRowKeys(this.transferInfo[`${record.codeName}IdStr`]);
									}
								} else {
									this.dialogParams.cType = 'tree';
									this.dialogParams.errorMsg = null;
									try{
										this.dialogParams.treeData = this.convertTreeData(JSON.parse(result.treeDataJson));
									}catch(e){
										this.dialogParams.treeData = [];
										this.dialogParams.errorMsg = i18n.message.dataConstructError();
									}
									this.treeProps.count = result.count;
									this.treeProps.checkedKeys = (result.selectedstrs === '' ? [] : result.selectedstrs.split(','));
								}
								this.dialogParams.buttons = [
									(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@42mdoj`} type="primary" op='done' onClick={() => this.done(this.dialogParams.table, false, tableName)}>{i18n.button.done()}</Button>),
									(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@p8h00m`} type="primary" op='selectAll' onClick={() => this.done(this.dialogParams.table, true, tableName)}>{i18n.button.selectAll()}</Button>)
								];
								this.dialogParams.rightMenu = [];
								this.dialogParams.buttons.map((btn, index) => {
									const {
										props
									} = btn;
									this.dialogParams.rightMenu.push({
										key: index.toString(),
										content: props.children,
										icon: <i className={this.menuIconCollection[props.op]}/>,
										onClick: props.onClick
									});
								})
								this.dialogParams.moreBtn.datas = [...this.dialogParams.rightMenu];
								this.dialogParams.visible = true;
								break;
							default:
								break;
						}
					}
				});
			} catch (e) {}
		}, error => {

		})
	}

	@action monitorProcess = (num, numberstr, isFinished = false) => {
		this.showResult = true;
		this.toCount = this.resultMessage.totalCount;
		this.fromCount = 0;
		if (isFinished) {
			this.showSpin = false;
			// this.resetData();
		}
		this.refreshDatInterval = setInterval(() => {
			num--;
			// if (num <= 10)
			// 	this.timer = num;
			if (num === 0) {
				clearInterval(this.refreshDatInterval);
				// if (isFinished) {
				// 	this.showSpin = false;
				//  this.resetData();
				// } else {
				api.getProcessLog({
					numberstr,
					transferType: this.transferInfo.transferType,
					authorityTag: this.transferInfo.authorityTag,
					fromid: this.transferInfo.fromid,
					toid: this.transferInfo.toid
				}).then(data => {
					if (data.status === '1') {
						if (has(data.message, 'isFinished')) {
							this.resultMessage = data.message;
							this.refreshResult = new Date().getTime();
							this.monitorProcess(30, numberstr, this.resultMessage.isFinished);
						} else {
							this.monitorProcess(30, numberstr);
						}
					}
				}, error => {})
				// }
			}
		}, 1000);
	}

	@action doExcute = () => {
		if (this.transferInfo.fromid === '') {
			this.formTarget.fromForm.validateForm().then(f => {
				f.showError(`${this.transferInfo.transferType}id`, i18n.message.dataEmpty());
				this.showError = new Date().getTime();
			})
			return;
		} else if (this.transferInfo.authorityTag != 'delete' && this.transferInfo.toid === '') {
			this.formTarget.toForm.validateForm().then(f => {
				f.showError(`${this.transferInfo.transferType}id`, i18n.message.dataEmpty());
				this.showError = new Date().getTime();
			})
			return;
		}

		const params = {
			submitJson: []
		}
		this.tableConfig.datas.map(d => {
			if (has(this.transferInfo, `${d.codeName}All`) && this.transferInfo[`${d.codeName}All`] === '1') {
				Object.assign(params, {
					[`${d.codeName}All`]: this.transferInfo[`${d.codeName}All`]
				})
				this.sqlCollection[d.codeName] && params.submitJson.push(this.sqlCollection[d.codeName].jsonSql);
			}
			if (has(this.transferInfo, `${d.codeName}AllId`) && this.transferInfo[`${d.codeName}AllId`] == '1') {
				Object.assign(params, {
					[`${d.codeName}IdStr`]: []
				})
				this.sqlCollection[d.codeName] && params.submitJson.push(this.sqlCollection[d.codeName].jsonSql);
			}
			if (has(this.transferInfo, `${d.codeName}IdStr`)) {
				Object.assign(params, {
					[`${d.codeName}IdStr`]: this.transferInfo[`${d.codeName}IdStr`]
				})
			}
			Object.assign(params, {
				[`${d.codeName}AllNum`]: d.totalCount
			})
		})
		if (Object.keys(params).length == this.tableConfig.datas.length + 1) {
			message.error(i18n.confirm.selectedEmpty());
			return;
		}

		let c = i18n.confirm.operationConfirm();
		c = c.replace('{param}', i18n.button[this.transferInfo.authorityTag]());
		this.confirmInfo({
			content: c,
			onOk: () => {
				this.showSpin = true;
				api.processData({
					fromid: this.transferInfo.fromid,
					toid: this.transferInfo.toid,
					transferType: this.transferInfo.transferType,
					authorityTag: this.transferInfo.authorityTag,
					...params
				}).then(data => {
					if (data.status === '1') {
						this.initForm(true);
						this.resultMessage = data.message;
						this.monitorProcess(10, data.numberstr, this.resultMessage.isFinished);
					}else{
						this.showSpin = false;
						message.error(data.message);
					}
				}, error => {

				})
			}
		});
	}

	@action calcSelectCount = (tableName, tableStore, selectAll, selectedKeys, v) => {
		if(['T601', 'C601', 'D601'].indexOf(tableName) > -1){
			let count = 0;
			const datas = this.toJS(tableStore.datas);
			datas.map(d => {
				if(selectAll || selectedKeys.indexOf(d.id) > -1)
					count += Number(d.idspan);
			})
			return count;
		}
		return v;
	}

	@action done = (tableStore, selectAll = false, tableName) => {
		const {
			record,
			index
		} = this.selectedTableItem;
		const {
			codeName
		} = record;
		let totalCount = 0;
		let selectedKeys = [];
		if (!has(this.tableConfig.selectedData, 'all')) 
			this.tableConfig.selectedData.all = [];
		if(this.dialogParams.cType === 'table'){
			remove(this.tableConfig.selectedData.all, n => n === index);
			totalCount = tableStore.count;
			selectedKeys = this.toJS(tableStore.selectedRowKeysAllPages);
		}else{
			totalCount = this.treeProps.count;
			selectedKeys = this.treeProps.checkedKeys;
		}
		delete this.transferInfo[`${codeName}All`];
		delete this.transferInfo[`${codeName}IdStr`];
		if (selectAll) {
			this.tableConfig.datas[index].selectCount = this.calcSelectCount(tableName, tableStore, selectAll, null, totalCount);//totalCount;
			if (this.tableConfig.datas[index].selectCount == this.tableConfig.datas[index].totalCount) {
				this.transferInfo[`${codeName}All`] = '1';
				this.tableConfig.selectedData.all.push(index);
			} else {
				// this.transferInfo[`${codeName}IdStr`] = selectedKeys;
				this.transferInfo[`${codeName}AllId`] = '1';
			}
			this.sqlCollection[codeName].jsonSql = this.sqlCollection[codeName].ojson;
		} else {
			this.transferInfo[`${codeName}IdStr`] = selectedKeys;
			if (this.transferInfo[`${codeName}IdStr`].length === 0) {
				message.error(i18n.confirm.selectedEmpty());
				return;
				// delete this.transferInfo[`${codeName}IdStr`];
			}
			this.tableConfig.datas[index].selectCount = this.calcSelectCount(tableName, tableStore, selectAll, selectedKeys, selectedKeys.length);//selectedKeys.length;
			if (this.tableConfig.datas[index].selectCount == this.tableConfig.datas[index].totalCount) {
				this.transferInfo[`${codeName}All`] = '1';
				this.tableConfig.selectedData.all.push(index);
				delete this.transferInfo[`${codeName}IdStr`];
			}
			this.sqlCollection[codeName].jsonSql = this.sqlCollection[codeName].njson;
		}

		this.calculateFromCountCount();
		this.refreshTable = new Date().getTime();
		this.dialogParams.visible = false;
	}

	@action doSearch = (params) => {
		params = params || {};
		if(this.tabConfig.tabSearchKey != '')
			Object.assign(params, {[this.tabConfig.tabSearchKey]: this.tabConfig.activeTabKey});
		[...Object.keys(this.otherListCondition)].map(k => {
			Object.assign(params, {[k]: this.otherListCondition[k]});
		});
		api.getList(this.selectedTableItem.record.codeName, { ...params,
			transferType: this.transferInfo.transferType,
			fromid: this.transferInfo.fromid
		}).then(data => {
			if (data.status === '1') {
				Object.assign(this.sqlCollection[this.selectedTableItem.record.codeName], {
					njson: data.njson,
					ojson: data.ojson
				})
				if (has(data, 'sessionkey')) {
					this.dialogParams.table.getDatas(data.sessionkey, 1);
					if (data.selectedstrs.length > 0) {
						this.dialogParams.table.setSelectedRowKeys(data.selectedstrs.split(','));
					}else if (this.transferInfo[`${this.selectedTableItem.record.codeName}IdStr`] && this.transferInfo[`${this.selectedTableItem.record.codeName}IdStr`].length > 0) {
						this.dialogParams.table.setSelectedRowKeys(this.transferInfo[`${this.selectedTableItem.record.codeName}IdStr`]);
					}
				} else {
					try{
						this.dialogParams.treeData = this.convertTreeData(JSON.parse(data.treeDataJson));
					}catch(e){
						this.dialogParams.treeData = [];
					}
				}
			}
		}, error => {

		})
	}

	@action resetData = () => {
		this.formTarget.fromForm.resetConditionValue();
		this.formTarget.toForm.resetConditionValue();
		// this.showSpin = false;
		// this.timer = 0;
		// 
		this.opImg = 'authAdjust';
		this.getFormInfo();
	}
	/********************* action list *********************/
}