import React from "react";
import { WeaDialog, WeaLocaleProvider, WeaNewScroll, WeaRightMenu, WeaFormItem, WeaSearchGroup } from 'ecCom';
import { WeaSwitch } from 'comsMobx'
import { Button } from 'antd';
import { inject, observer } from 'mobx-react'
import { toJS } from 'mobx';
const getLabel = WeaLocaleProvider.getLabel;
import * as API from "../../apis/TodoCenterService";

@inject("todoCenterStore")
@observer
export default class AddObjectDialog extends React.Component {
	getForm = () => {
		const { AddObjectDialogForm, AddObjectDialogFormField } = this.props.todoCenterStore;
		const { isFormInit } = AddObjectDialogForm;
		let group = [];
		isFormInit &&
			AddObjectDialogFormField.map(c => {
				let childCheck;
				c.items.forEach(element => {
					element.domkey == "has_child" && (childCheck = toJS(element));
				});
				let items = [];
				c.items.forEach(field => {
					if (field.domkey[0] == 'sharevalue1' || field.domkey[0] == 'sharevalue6') {
						items.push({
							com: (
								<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@xaoyz4@${field.domkey[0]}`}
									label={`${field.label}`}
									labelCol={{ span: `${field.labelcol}` }}
									ratio1to2
									tipPosition="bottom"
									wrapperCol={{ span: `${field.fieldcol}` }}
									tipPosition={'bottom'}
									error={AddObjectDialogForm.getError(field)}
								>
									<div style={{ float: 'left', width: '150px' }}>
										<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@aqxwno@${field.domkey[0]}`}
											fieldConfig={field}
											form={AddObjectDialogForm}
										/></div>
									<div style={{ float: 'left', paddingLeft: "15px", paddingRight: '10px' }}>
										<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@6ya8k8@${field.domkey[0]}`}
											fieldConfig={childCheck}
											form={AddObjectDialogForm}
										/>
										<span>含下级</span>
									</div>
								</WeaFormItem>
							),
							col: '1',
							hide: this.isHide(field.domkey[0], AddObjectDialogForm.getFormParams())
						});
					} else {
						items.push({
							com: (
								<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@vwc7on@${field.domkey[0]}`}
									label={`${field.label}`}
									labelCol={{ span: `${field.labelcol}` }}
									ratio1to2
									tipPosition="bottom"
									wrapperCol={{ span: `${field.fieldcol}` }}
									tipPosition={'bottom'}
									error={AddObjectDialogForm.getError(field)}
								>
									<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nmuest@${field.domkey[0]}`}
										fieldConfig={field}
										form={AddObjectDialogForm}
									// onChange={this.onChange}
									/>
								</WeaFormItem>
							),
							col: '1',
							hide: this.isHide(field.domkey[0], AddObjectDialogForm.getFormParams())
						});
					}
				});
				group.push(
					<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@gkkzho@${c.title}`}
						needTigger={true}
						title={c.title}
						showGroup={c.defaultshow}
						items={items}
					/>
				);
			});
		return group;
	}
	isHide = (key, formParams) => {
		let hide = true;
		if (key == 'permissiontype') {
			return hide = false;
		}
		//  处理业务显隐
		if (['sharevalue5'].includes(key) && formParams.permissiontype == '5') {
			return hide = false;
		}
		if (['sharevalue6', 'seclevel'].includes(key) && formParams.permissiontype == '6') {
			return hide = false;
		}
		if (['sharevalue1', 'seclevel'].includes(key) && formParams.permissiontype == '1') {
			return hide = false;
		}
		if (['sharevalue2', 'rolelevel', 'seclevel'].includes(key) && formParams.permissiontype == '2') {
			return hide = false;
		}
		if (['sharevalue7', 'jobtitlelevel'].includes(key) && formParams.permissiontype == '7') {
			return hide = false;
		}
		if (['seclevel'].includes(key) && formParams.permissiontype == '3') {
			return hide = false;
		}
		if (['jobtitlesharevalue2'].includes(key) && formParams.jobtitlelevel == '2' && formParams.permissiontype == '7') {
			return hide = false;
		}
		if (['jobtitlesharevalue3'].includes(key) && formParams.jobtitlelevel == '3' && formParams.permissiontype == '7') {
			return hide = false;
		}
		return hide;
	};
	getBtn = () => {
		const { onCancelHandle } = this.props.todoCenterStore
		return ([<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@u46as1@addShareok`} type="primary" onClick={() => this.onRightMenuClick('addShare')}>{getLabel(826, '确定')}</Button>,
		<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@244xfc@addShareno`} onClick={onCancelHandle}>{getLabel(31129, '取消')}</Button>])
	}
	onRightMenuClick = (type) => {
		const { getAddObjectDialogForm } = this.props.todoCenterStore;
		getAddObjectDialogForm(type);
	}
	render () {
		const { todoCenterStore } = this.props;
		const { addObjectVisible, menu } = this.props.todoCenterStore.status;
		const { AddObjectDialogForm, onCancelHandle } = this.props.todoCenterStore
		return (
			<WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@p0s7kz`}
				onCancel={onCancelHandle}
				icon={'icon-coms-integration'}
				iconBgcolor={'#1a57a0'}
				title={`${getLabel(383958, '添加对象')}`}
				visible={addObjectVisible}
				style={{ width: 500, height: 400 }}
				hasScroll
				buttons={this.getBtn()}
			>
				<WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@6c35bf`}
					datas={menu}>
					<WeaNewScroll ecId={`${this && this.props && this.props.ecId || ''}_WeaNewScroll@rywtp9`} height={400} ref="scroll">
						{this.getForm()}
					</WeaNewScroll>
				</WeaRightMenu>
			</WeaDialog>
		);
	}
}