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

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

@inject("authenticationCenterStore")
@observer
class AuthenticationCenter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visiable:false,
      collectParams: {
        favname: getLabel('516738','Token认证'),
        favouritetype: 1,
        objid: 0,
        link: "wui/engine.html#/integration/token",
        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@integrationAuthToken@integrationauthTokenButtonSaveBtn`} 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.oauth2_isuse;
          delete params.spnego_isuse;
          delete params.dlgt_isuse;
          IntegrationUtil.doConfirm(
            getLabel('512788','保存后将自动重启服务，若有失败，请手动重启服务，以确保系统能正常使用！'),
            authenticationCenterStore.getBaseInfoFormOperation,
            params
          )
        });
      }else if(type == "BTN_TEST") {
        let loginurl = newForm.getFormParams().loginurl;
        let params = {casserverurl: loginurl, method: 'test'};
        authenticationCenterStore.getBaseInfoFormOperation(params);
      }
  };

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

    isFormInit && BaseInfoFormFields && BaseInfoFormFields.map((c, index) =>{
      if(index == 1) {
        let items = [];
        c.items && c.items.map(fields => {
          items.push({
            com:(
              <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@tg605w@integrationauthTokenForm${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@ftxvbp@integrationauthTokenForm${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) === 'get_token_url' && allParams.token_isuse != '1' && (bool = true);
              getKey(fields) === 'check_token_url' && allParams.token_isuse != '1' && (bool = true);
              return bool;
            })
          });
        });
        group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@t6rvl3@integrationauthTokenForm${index}`} center  col={1}  needTigger={false} title={""}  showGroup={c.defaultshow} items={items}/>);
      }
    });
    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@9ck2za`}
              className={"inte-webxml-rightmenu"}
              datas={this.getRightMenu()}
              collectParams={collectParams}
            >
              <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@9h3747`}
                title={getLabel('516738','Token认证')}
                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@jn0yz5`}/>
    );
  }
}

export default AuthenticationCenter;
