import {inject, observer} from "mobx-react";
import {toJS} from "mobx";
import {Button} from "antd";
import {WeaFormItem, WeaLocaleProvider, WeaRightMenu, WeaSearchGroup, WeaTop, WeaTools} from "ecCom";
import {WeaSwitch} from "comsMobx";
import NoRight from "../../public/NoRight";
import * as IntegrationUtil from "../../util/IntegrationUtil";

const { getKey } = WeaTools;
const getLabel = WeaLocaleProvider.getLabel;

@inject("authenticationCenterStore")
@observer
class AuthenticationSPNEGO extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visiable:false,
      collectParams: {
        favname: getLabel('521386','SPNEGO认证'),
        favouritetype: 1,
        objid: 0,
        link: "wui/engine.html#/integration/spnego",
        importantlevel: 1
      }
    };
  }

  componentDidMount() {
    const { authenticationCenterStore } = this.props;
    authenticationCenterStore.doInit();
  }

  componentWillUnmount() {
    this.props.authenticationCenterStore.clearStore();
  }

  componentWillReceiveProps(nextProps) {
    const { authenticationCenterStore } = this.props;
    if (this.props.location.key !== nextProps.location.key) {
      authenticationCenterStore.doInit();
    }
  }

  /**
   * 获取右键菜单
   * @returns {Array}
   */
  getRightMenu() {
    return [{
      key: "BTN_BASE_SAVE",
      icon: <i className="icon-coms-Preservation" />,
      content: `${getLabel(30986, '保存')}`,
      onClick: () => this.onRightMenuClick("BTN_BASE_SAVE")
    }
   ]
  }

  /**
   * 获取顶部按钮
   * @returns {Array}
   */
  getBtns = () => {
    return [
      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@mtxc5f@integrationauthSPNEGOButtonSaveBtn`} type="primary" onClick={() => this.onRightMenuClick('BTN_BASE_SAVE')}>{getLabel(30986, '保存')}</Button>,
    ]
  };

  /**
   * 右键菜单点击事件
   * @param type
   */
  onRightMenuClick = (type) => {
      const { authenticationCenterStore } = this.props;
      const { newForm } = authenticationCenterStore;
    
      if (type == "BTN_BASE_SAVE") {
        IntegrationUtil.onCheck(newForm,()=>{
          let params = Object.assign({}, newForm.getFormParams(), {method: 'weaversso'});
          delete params.isuse;
		      delete params.token_isuse;
          delete params.oauth2_isuse;
          delete params.dlgt_isuse;
          authenticationCenterStore.getBaseInfoFormOperation(params);
        });
      }
  };

  getBaseInfoForm = () => {
    const {authenticationCenterStore} = this.props;
    const {newForm, status} = authenticationCenterStore;
    const {BaseInfoFormFields, spnegoDescription=""} = toJS(status);
    const {isFormInit} = newForm;
    let group = [];
    const formParams = newForm.getFormParams();

    isFormInit && BaseInfoFormFields && BaseInfoFormFields.forEach((c, index) =>{
      if(index == 3) {
        let items = [];
        c.items && c.items.forEach(fields => {
          if(fields.domkey[0] != "spnego_desc_") {
            items.push({
              com:(
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1eypgg@integrationauthSPNEGOForm${fields.domkey[0]}`}
                    label={`${fields.label}`}
                    labelCol={{span: `${fields.labelcol}`}}
                    wrapperCol={{span: `${fields.fieldcol}`}}
                    error={newForm.getError(fields)}
                    tipPosition="bottom"
                >
                    <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@nx88fn@integrationauthSPNEGOForm${fields.domkey[0]}`} fieldConfig={fields} form={newForm} formParams={formParams}  onChange={v=> {}} />
                </WeaFormItem>),
              colSpan:1,
              hide: newForm.isHide(fields, (keys, allParams) => {
                let bool = false;
                getKey(fields) === 'spnego_ds' && allParams.spnego_isuse != '1' && (bool = true);
                return bool;
              })
            });
          }
        });
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@xul6xr@integrationauthSPNEGOForm${index}`} center  col={1}  needTigger={true} title={getLabel('82743','基础信息')}  showGroup={c.defaultshow} items={items}/>);
      }
    });
    group.push(
      <WeaSearchGroup 
        ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@xulxr2@integrationauthSPNEGOForm${2}`} 
        col={1}  
        needTigger={true} 
        title={getLabel('25734','说明')}  
        showGroup={true} 
      >
        <div style={{margin: "0 10px"}} dangerouslySetInnerHTML={{__html: spnegoDescription}} />
      </WeaSearchGroup>  
    );
    return group;
  }

  render() {
    const {authenticationCenterStore} = this.props;
    const {hasRight, rightLoading} = authenticationCenterStore;
    const {collectParams} = this.state;

    return (
      rightLoading 
        ? null 
        : hasRight
          ? <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@r8im6g`}
              datas={this.getRightMenu()}
              collectParams={collectParams}
            >
              <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@yz2cz5`}
                title={getLabel('521386','SPNEGO认证')}
                icon={<i className="icon-coms-integration" />}
                iconBgcolor="#1a57a0"
                buttons={this.getBtns()}
                showDropIcon={true}
                dropMenuDatas={this.getRightMenu()}
                dropMenuProps={{collectParams}}
              >
                {this.getBaseInfoForm()}
              </WeaTop>
            </WeaRightMenu>
          : <NoRight ecId={`${this && this.props && this.props.ecId || ''}_NoRight@l5plw4`}/>
    );
  }
}

export default AuthenticationSPNEGO;
