import React, { Component } from 'react';
import {
  WeaDialog,
  WeaRightMenu,
  WeaLocaleProvider,
  WeaFormItem,
  WeaCheckbox,
} from "ecCom";
import {
  WeaSwitch
} from "comsMobx";
import {
  inject, 
  observer
} from "mobx-react";
import {toJS} from "mobx";
import {
  Button
} from "antd";
import {onCheck} from "../../util/IntegrationUtil";

const {getLabel} = WeaLocaleProvider;

@inject("authenticationCenterStore")
@observer
export default class NewShare extends Component {
  constructor(props) {
    super(props);
    this.state = {
      collectParams: {
        favname: `${getLabel(381908, "统一认证中心")}`,
        favouritetype: 1,
        objid: 0,
        link: "wui/engine.html#/integration/AuthenticationCenter",
        importantlevel: 1
      }  
    }
  }

  getRightMenus = () => {
    return [{
        type: "primary",
        key: "BTN_SAVE",
        icon: <i className="icon-coms-Preservation" />,
        content: getLabel(30986, '保存'),
        onClick: () => this.onMenuClick("BTN_SAVE")
    }];
  }

  getBtns = () => {
    return [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@u5gp5n@integrationauthAppNewShareSaveBtn`} type="primary" key="BTN_SAVE" onClick={() => this.onMenuClick("BTN_SAVE")}>{getLabel(30986, '保存')}</Button>];
  }

  hideShareField = (params) => {
    const {
        permissiontype,
        jobtitlelevel,
    } = params;

    let temp;

    if (permissiontype == '1') {
        temp = ['sharevalue6', 'sharevalue5', 'sharevalue2', 'sharevalue7', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
    }
    if (permissiontype == '6') {
        temp = ['sharevalue1', 'sharevalue5', 'sharevalue2', 'sharevalue7', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
    }
    if (permissiontype == '3') {
        temp = ['sharevalue1', 'sharevalue6', 'sharevalue5', 'sharevalue2', 'sharevalue7', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
    }
    if (permissiontype == '5') {
        temp = ['sharevalue1', 'sharevalue6', 'sharevalue2', 'sharevalue7', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
    }
    if (permissiontype == '2') {
        temp = ['sharevalue1', 'sharevalue6', 'sharevalue5', 'sharevalue7', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
    }
    if (permissiontype == '7') {
        if (jobtitlelevel == '1') {
            temp = ['sharevalue1', 'sharevalue6', 'sharevalue5', 'sharevalue2', 'jobtitlesharevalue2', 'jobtitlesharevalue3'];
        }
        if (jobtitlelevel == '2') {
            temp = ['sharevalue1', 'sharevalue6', 'sharevalue5', 'sharevalue2', 'jobtitlesharevalue3'];
        }
        if (jobtitlelevel == '3') {
            temp = ['sharevalue1', 'sharevalue6', 'sharevalue5', 'sharevalue2', 'jobtitlesharevalue2'];
        }
    }
    window.e9HideFormFieldKeys = temp;
  }

  onMenuClick = (type="") => {
    const{
        authenticationCenterStore   
    } = this.props, {
        getRegisterAppShareForm,
        addShareForm,
        status,
    } = authenticationCenterStore;
    if(type === "BTN_SAVE") {
      const params = addShareForm.getFormParams();
      this.hideShareField(params);
      onCheck(addShareForm, () => {
        getRegisterAppShareForm({
          operate: "addShare",
          sysid: status.registerAppId,
          ...params
        }) 
      })
    }
  }

  getForm = (form, condition=[]) => {
		const {
			permissiontype,
		} = form.getFormParams(), {
			isFormInit,
		} = form;

		let arr = [];
		isFormInit && condition.forEach(c => {
			c && c.items && c.items.forEach(field => {
				const key = field.domkey[0];
				if (this.filter({
          permissiontype,
          key
        })) {
					return
				}

				arr.push(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kis43z@integrationauthAppNewShareForm${field.domkey[0]}`}
          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@vfqqy0@integrationauthAppNewShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb, key, form)}/>
        </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 == 'seclevel')) {
			return true
		}

		if (permissiontype == '6' && !(key == 'permissiontype' || 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));
		}
		if ((permissiontype == '6' || permissiontype == '1') && key == 'permissiontype') {
			temp.push(this.getContainSubDom(form, permissiontype));
		}
		return temp
  }
  
  getContainSubDom = (form, permissiontype) => {
    const {
			authenticationCenterStore
		} = this.props, {
      shareFormDepFields,
      shareFormSubFields,
    } = toJS(authenticationCenterStore);
    const temp = permissiontype == "1" ? [...shareFormDepFields] : [...shareFormSubFields];
    return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@gtb5o8`}
        label={getLabel('106','对象')}
        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@ngm4as@integrationauthAppNewShareForm${field.domkey[0]}`}
                      label={''}
                      colon={false}
                      labelCol={{span: 0}}
                      error={form.getError(field)}
                      tipPosition="bottom"
                      wrapperCol={{span: (index == 0) ? 24 : 17}}
                      style={{float:'left', width:150, marginLeft:(index == 1) ? 27 : 0}}
            				>
                {
                  index == 0
                    ? <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@d92kju@integrationauthAppNewShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
                    : <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@0sssza@integrationauthAppNewShareForm${field.domkey[0]}`} content={getLabel('125963','含下级')} value={form.getFormParams().has_child} onChange={v => form.updateFields({has_child: v})} />
                }
								
							</WeaFormItem>)
				})}
			</WeaFormItem>
		)
  }

	handleChange = (cb, key, form) => {
		if(key === "permissiontype") {
      if(['1', '6'].includes(form.getFormParams().permissiontype)) {
        form.updateFields({has_child: '0'});
      }
    }
	}

	getRole = (form) => {
		const {
			authenticationCenterStore
		} = this.props, {
			shareFormRole
		} = toJS(authenticationCenterStore);
		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@syb506`}
        label={getLabel(122,'角色')}
        labelCol={{span: 5, offset: 3}}
        wrapperCol={{span: 12}}
        style={{padding: 8}}
      >
				{shareFormRole.map((field, index) => {
					return (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1bhqoa@integrationauthAppNewShareForm${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@yy0iiw@integrationauthAppNewShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
							</WeaFormItem>)
				})}
			</WeaFormItem>
		)
	}

	getPost = (form) => {
    const {
			authenticationCenterStore
		} = this.props, {
			shareFormPost
		} = toJS(authenticationCenterStore), {
      jobtitlelevel
    } = form.getFormParams();

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

		return (
			<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ch9gom`}
        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@v0825u@integrationauthAppNewShareForm${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@bnx468@integrationauthAppNewShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
          </WeaFormItem>)
				})}
			</WeaFormItem>
		)
	}

  render() {
    const{
      authenticationCenterStore   
    } = this.props, {
      status,
      addShareForm,
    } = authenticationCenterStore, {
      showAddShare,
      addShareCondition,
    } = toJS(status), {
      collectParams
    } = this.state;

    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ppi1aa`}
        title={getLabel('383958','添加对象')}
        moduleName="integration"
        hasScroll
        style={{width: 800, height: 600}}
        visible={showAddShare}
        onCancel={v => authenticationCenterStore.setState({showAddShare: false})}
        buttons={this.getBtns()}
        moreBtn={{
          datas: this.getRightMenus(),
          collectParams
        }}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@51mnn9`}
          datas={this.getRightMenus()}
          collectParams={collectParams}
        >
          {this.getForm(addShareForm, addShareCondition)}
        </WeaRightMenu>
      </WeaDialog>
    )
  }
}
