import React, { Component } from 'react';
import {
  WeaDialog,
  WeaRightMenu,
  WeaFormItem,
  WeaLocaleProvider
} 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("ExchangeStore")
@observer
export default class NewShare extends Component {
  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@0v9sxq@integrationExchangeNewShareButtonSave`} 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{
      ExchangeStore   
    } = this.props, {
      doOperationlimit,
      shareForm,
    } = ExchangeStore;
    if(type === "BTN_SAVE") {
      const params = shareForm.getFormParams();
      this.hideShareField(params);
      onCheck(shareForm, () => {
        doOperationlimit({
          operate: "addShare", 
          ...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@8xbxrn@integrationExchangeNewShareForm${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@88h767@integrationExchangeNewShareForm${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) => {
		
	}

	getRole = (form) => {
		const {
			ExchangeStore
		} = this.props, {
			role
		} = toJS(ExchangeStore.status);

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

	getPost = (form) => {
    const {
			ExchangeStore
		} = this.props, {
			post
		} = toJS(ExchangeStore.status), {
      jobtitlelevel
    } = form.getFormParams();

		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@4oytog`}
                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@zxak0z@integrationExchangeNewShareForm${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@zqkedu@integrationExchangeNewShareForm${field.domkey[0]}`} fieldConfig={field} form={form} formParams={form.getFormParams()} onChange={cb => this.handleChange(cb)}/>
							</WeaFormItem>)
				})}
			</WeaFormItem>
		)
	}

  render() {
    const{
      ExchangeStore   
    } = this.props, {
      status,
      shareForm,
    } = ExchangeStore, {
      showShareForm,
      shareFormCondition,
      collectParams
    } = toJS(status);

    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@px8bl6`}
        title={getLabel(19910, '共享范围')}
        moduleName="integration"
        hasScroll={true}
        style={{width: 800, height: 600}}
        visible={showShareForm}
        onCancel={v => ExchangeStore.setState({showShareForm: false})}
        buttons={this.getBtns()}
        moreBtn={{
          datas: this.getRightMenus(),
          collectParams
        }}
      >
        <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@5c84wy`}
          datas={this.getRightMenus()}
          collectParams={collectParams}
        >
          {this.getForm(shareForm, shareFormCondition)}
        </WeaRightMenu>
      </WeaDialog>
    )
  }
}
