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

export class HrmBatchUploadPhoto extends HrmBaseStore {
	rootStore;
	router;
	hrmImportResource;

	setRootStore = (rootStore, router) => {
		this.rootStore = rootStore;
		this.router = router;
		rootStore.multiUpload = this.multiUpload;
		rootStore.multiDelete = this.multiDelete;
		rootStore.definedColumnVisible = this.definedColumnVisible;
		rootStore.showOpLog = this.showOpLog;
		// this.menus = this.rootStore.topMenuDef().edit[this.rootStore.routeProps.params].menus;
		this.refreshMenu = new Date().getTime();
		this.initData();
	}

	setImportResource = (hrmImportResource) => {
		this.hrmImportResource = hrmImportResource;
	}
	/********************* unobservable list *********************/
	orgTreeProps = {
		needSearch: true,
		noCache: true,
		isLoadSubDepartment: true,
		topPrefix: 'hrmSearch',
		inputLeftDom: `<b>${i18n.label.organization()}</b>`,
		treeNodeClick: this.treeNodeClick,
		params: {
			rightStr: 'PersonnelOrganization:Batchmaintenance'
		}
	}

	tabConfig = {
		tabs: [{
			color: '#000000',
			groupId: 'key',
			title: '',
			viewCondition: '1',
			searchType: ['base', 'advanced'],
			searchKey: 'lastname',
			doSearch: () => this.generateTableSelectedData(this.generateParams(), false)
		}],
		keyParam: 'viewCondition',
		activeTabKey: '1'
	}

	targetInfo = {};
	treeRef;
	selectedSubCompany;
	/********************* unobservable list *********************/

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

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

	/********************* observable list *********************/
	@observable table = new TableStore();

	dialogParams = {
		...this.dialogPropsDef,
		onCancel: () => {
			this.dialogParams.visible = false;
		}
	}
	/********************* observable list *********************/

	/********************* computed list *********************/
	@computed get multFiOp(){
		return this.table.selectedRowKeys.length == 0;
	}

	monitorMenuStats = () => {
		this.multFiOp;
		this.rootStore && this.rootStore.setMultiOpDisable(this.multFiOp);
	}

	monitorSpinning = () => {
		this.spinning;
		this.rootStore && this.rootStore.setSpinning(this.spinning);
	}

	monitor1 = autorun(this.monitorMenuStats)
	monitor2 = autorun(this.monitorSpinning)
	/********************* computed list *********************/

	/********************* action list *********************/
	setTreeRef = (ref) => {
		this.treeRef = ref;
	}

	initData = () => {
		this.selectedSubCompany = null;
		api.getCondition(null, this.containerType).then(data => {
			if(data.status === '1'){
				this.setFormData('conditionForm', data.conditions);
				if(this.formTarget.conditionForm.isFormInit){
					this.generateTableSelectedData();
				}
			}else{
				message.error(data.message);
			}
		}, error => {})
	}

	@action treeNodeClick = (event, ids, nodeids, nodes) => {
		const {
			data: nodeData
		} = event.node.props;
		this.selectedSubCompany = nodeData;
		this.formTarget.conditionForm.resetConditionValue();
		this.generateTableSelectedData(this.generateParams(), true);
	}

	generateParams = () => {
		const params = {
		}
		if (this.selectedSubCompany != null) {
			if (this.selectedSubCompany.type === '1') {
				Object.assign(params, {
					subcompanyid1: this.selectedSubCompany.id
				})
			} else if (this.selectedSubCompany.type === '2') {
				Object.assign(params, {
					subcompanyid1: this.selectedSubCompany.psubcompanyid,
					departmentid: this.selectedSubCompany.id
				})
			}
		}
		return params;
	}

	generateTableSelectedData = (params = {}, onTree = true) => {
		const formParams = this.formTarget.conditionForm.getFormParams();
		onTree && Object.assign(formParams, {...params});
		api.getList(formParams, this.containerType).then(data => {
			this.table = new TableStore();
			if(data.status === '1'){
				this.table.getDatas(data.sessionkey, 1);
			}else{
				message.error(data.message);
			}
		}, error => {})
	}

	@action multiUpload = () => {
		api.getForm().then(data => {
			if(data.status === '1'){
				this.hrmImportResource.importCallback = this.importCallback;
				this.hrmImportResource.condition = data.condition;
				this.hrmImportResource.excelfile = '';
				this.hrmImportResource.filelist.length = 0;
				this.hrmImportResource.importType = 'photo';
				this.hrmImportResource.operateType = 'update';
				this.hrmImportResource.otherParams = {
					logSmallType: 20024
				};
				this.hrmImportResource.visible = true;
			}else{

			}
		}, error => {

		})
	}

	@action importCallback = () => {
		this.generateTableSelectedData(this.generateParams(), true);
	}

	@action multiDelete = (params) => {
		let content = '';
		if (!has(params, 'id')) {
			params = {
				id: this.table.selectedRowKeys
			}
			content = i18n.confirm.multiDeletePhoto();
		}else{
			content = i18n.confirm.deletePhoto();
		}

		this.confirmInfo({
			content,
			onOk: () => {
				api.deletePhoto(params).then(data => {
					if (data.status === '1') {
						message.success(i18n.message.deleteSuccess());
						this.generateTableSelectedData(this.generateParams(), true);
					} else
						message.error(data.message);
				}, error => {
					message.error(i18n.message.actionError());
				});
			}
		});
	}

	@action setTargetId = (id) => {
		this.targetInfo.id = id;
	}

	@action onUploadChange = (ids, list) => {
		this.targetInfo.fileId = ids[0];
	}

	@action onUploading = (status) => {
		if(status == 'uploaded' && this.targetInfo.id != null){
			api.changePhoto({id: this.targetInfo.id, resourceimageid: this.targetInfo.fileId}).then(data => {
				if(data.status === '1'){
					message.success(i18n.message.saveSuccess())
					this.generateTableSelectedData(this.generateParams(), true);
				}else{
					message.error(data.message);
				}
			}, error => {})
		}
	}

	@action definedColumnVisible = () => {
		this.definedColumn(this.table)
	}

	@action showOpLog = () => {
		this.showLog({
			logType: '4',
            logSmallType: 12
		});
	}
	/********************* action list *********************/
}