import {
	inject,
	observer,
} from 'mobx-react'
import {
	WeaFormItem,
	WeaLocaleProvider,
} from 'ecCom'
import {
	Spin,
} from 'antd'
import {
	WeaSwitch
} from 'comsMobx'
const getLabel = WeaLocaleProvider.getLabel

@inject('intelogin')
@observer
export default class ShareFrom extends React.Component {
	constructor(props) {
		super(props);
	}

	componentDidMount() {
		this.props.intelogin.getShareForm();
	}

	componentWillUnmount() {
		this.props.intelogin.resetShareForm()
	}

	getForm = () => {
		const {
			intelogin
		} = this.props, {
			shareForm,
		} = intelogin, {
			form,
			condition,
			permissiontype,
		} = shareForm, {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && condition.map(c => {
			c.items.map((field) => {
				const key = field.domkey[0];

				if (this.filter({
						permissiontype,
						key
					})) {
					return
				}

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ih1xvg@integrationLoginShareForm${key}`}
                            label={`${field.label}`}
                            labelCol={{span: 5, offset: 3}}
                			wrapperCol={{span: 12}}
                            error={form.getError(field)}
                            tipPosition="bottom"
                            style={{padding: 8}}
                        >
                                <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@kzoa4j@$integrationLoginShareForm${key}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                        </WeaFormItem>)

				arr = [...arr, this.add({
					permissiontype,
					key,
					form,
				})];


			})
		})
		return <div style={{paddingTop: 15}} className='integrated-share-form'>{arr}</div>
	}

	filter = (params) => {
		const {
			permissiontype,
			key
		} = params;

		if (key == 'sharevalue2' || key == 'rolelevel' || key == 'jobtitlelevel' || key == 'jobtitlesharevalue2' || key == 'jobtitlesharevalue3') {
			return true
		}

		if (permissiontype == '1' && !(key == 'permissiontype' || key == 'sharevalue1' || key == 'seclevel')) {
			return true
		}

		if (permissiontype == '6' && !(key == 'permissiontype' || key == 'sharevalue6' || key == 'seclevel')) {
			return true
		}

		if (permissiontype == '3' && !(key == 'permissiontype' || key == 'seclevel')) {
			return true
		}

		if (permissiontype == '5' && !(key == 'permissiontype' || key == 'sharevalue5')) {
			return true
		}

		if (permissiontype == '2' && !(key == 'permissiontype' || key == 'seclevel')) {
			return true
		}

		if (permissiontype == '7' && !(key == 'permissiontype' || key == 'sharevalue7')) {
			return true
		}
	}

	add = (params) => {
		const {
			permissiontype,
			key,
			form,
		} = params;

		let temp = [];
		if (permissiontype == '2' && key == 'permissiontype') {
			temp.push(this.getRole(form));
		}
		if (permissiontype == '7' && key == 'sharevalue7') {
			temp.push(this.getPost(form));
		}

		return temp
	}


	handleChange = (cb) => {
		const {
			intelogin
		} = this.props, {
			exportShSelectedValue
		} = intelogin;

		Object.entries(cb).map(arr => {
			const key = arr[0],
				t = arr[1],
				val = Array.isArray(t.value) ? t.value[0] : t.value;

			exportShSelectedValue({
				val,
				key
			});

		});
	}

	getRole = (form) => {
		const {
			intelogin
		} = this.props, {
			shareForm
		} = intelogin, {
			role
		} = shareForm;

		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@i7kq5t`}
                label={getLabel(122,'角色')}
                labelCol={{span: 5, offset: 3}}
                wrapperCol={{span: 12}}
                style={{padding: 8}}
            >
				{role.map((field, index) => {
					return (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@tqj1fx@integrationLoginShareForm${field.domkey[0]}`}
				                label={index == 0 ? '' : getLabel(139,'级别')}
				                colon={index == 0 ? false : true}
				                labelCol={{span: (index == 0) ? 0 : 7}}
				                error={form.getError(field)}
                            	tipPosition="bottom"
				                wrapperCol={{span: (index == 0) ? 24 : 17}}
				                style={{float:'left', width:150, marginLeft:(index == 1) ? 27 : 0}}
            				>
								<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@gw4kdu@integrationLoginShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
							</WeaFormItem>)
				})}
			</WeaFormItem>
		)
	}

	getPost = (form) => {
		const {
			intelogin
		} = this.props, {
			shareForm
		} = intelogin, {
			post,
			jobtitlelevel,
		} = shareForm;

		let temp;
		if (jobtitlelevel == '1') {
			temp = post.slice(0, 1)
		}
		if (jobtitlelevel == '2') {
			temp = post.slice(0, 2)
		}
		if (jobtitlelevel == '3') {
			temp = post.slice(0, 1).concat(post.slice(2))
		}

		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@cvrrcm`}
                label={getLabel(6086,'岗位')}
                labelCol={{span: 5, offset: 3}}
                wrapperCol={{span: 12}}
                style={{padding: 8}}
            >
				{temp.map((field, index) => {
					return (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@jf1jtx@integrationLoginShareForm${field.domkey[0]}`}
				                label={''}
				                colon={false}
				                labelCol={{span:0}}
				                error={form.getError(field)}
                            	tipPosition="bottom"
				                wrapperCol={{span: 24}}
				                style={{float:'left', width: (index == 0) ? 120 : 197, marginLeft:(index == 1) ? 10 : 0}}
            				>
								<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@1i2oth@integrationLoginShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
							</WeaFormItem>)
				})}
			</WeaFormItem>
		)
	}

	render() {
		const {
			intelogin,
		} = this.props, {
			shareForm,
		} = intelogin, {
			loading,
		} = shareForm;

		if (loading) {
			return (
				<div className='integration-loading-center-small'>
                    <Spin ecId={`${this && this.props && this.props.ecId || ''}_Spin@gds6wo`} spinning={loading}></Spin>
                </div>
			)
		} else {
			return (
				<div className='integrated-login-form'>
				    {this.getForm()}
				</div>
			)
		}
	}
}