import React from "react";
import {inject, observer} from "mobx-react";
import { Button} from "antd";
import {toJS} from "mobx";
import {WeaForm, WeaLogView, WeaNewScroll, WeaSwitch, WeaTableNew} from "comsMobx";
import {WeaDialog, WeaLocaleProvider, WeaTools, WeaSearchGroup, WeaFormItem, WeaRightMenu} from "ecCom";
import * as IntegrationUtil from "../../util/IntegrationUtil";
import {checkUserMappingLoginIdUnique,checkUserMappingAppLoginIdUnique} from "../../apis/AuthenticationCenterService"
const { getKey } = WeaTools;
const getLabel = WeaLocaleProvider.getLabel;

const store = ["oauth2Store"];

@inject(...store)
@observer
export default class AppDataMappingEditDialog extends React.Component {
  render() {
    const {oauth2Store} = this.props;
    const status = toJS(oauth2Store.status);
    return <WeaDialog
      title={oauth2Store.operateDataMappingIndex == '' ? getLabel('31691,381943','注册数据映射') : getLabel('26473,381943','编辑数据映射')}
      onCancel={()=> oauth2Store.setState({appEditFormDetailDataMappingDialogVisible: false})}
      visible={status.appEditFormDetailDataMappingDialogVisible}
      style={{width: 700, height: 400}}
      hasScroll
      moduleName="integration"
      buttons={[
        <Button type="primary" onClick={()=> this.onRightMenuClick('BTN_SAVE')}>{getLabel(86,'保存')}</Button>
      ]}
      moreBtn={{
        datas : this.getRightMenu(),
        collectParams: []
      }}
    >
      <WeaRightMenu
        datas={this.getRightMenu()}
        collectParams={[]}
      >
      {
        oauth2Store.registerAppDataMappingEditForm.render({col: 1},{
        onChange: (v)=> {
        },
        isHide: (key, formParams) => {}

      },true,(c, idx)=>{
        if(idx==0){
          let items = [];
          let bottomDis = 0;
          let form = oauth2Store.registerAppDataMappingEditForm;
          let formParams = form.getFormParams();
          c.items.map(fields => {
            if(getKey(fields) == 'apploginid') {
              bottomDis = 15;
            }else {
              bottomDis = 0;
            }
            items.push({
              com: (
                <div style={{marginBottom: bottomDis}}>
                <WeaFormItem
                  label={`${fields.label}`}
                  labelCol={{span: `${fields.labelcol}`}}
                  wrapperCol={{span: `${fields.fieldcol}`}}
                  error={form.getError(fields)}
                  tipPosition="bottom"
                >
                  <nobr>
                    <WeaSwitch fieldConfig={fields} form={form} formParams={formParams}
                    onChange={(v)=>{
                      if(getKey(fields) === 'userid') {
                        let params={userid:form.getFormParams().userid,operation:'getUserInfo'};
                        oauth2Store.getRegisterAppDataMappingOperation(params,(result)=>{
                          if(result) {
                            if(result.depname) {
                              form.updateFields({depname: result.depname});
                            }
                            if(result.subname) {
                              form.updateFields({subname: result.subname});
                            }
                            if(result.loginid) {
                              form.updateFields({loginid: result.loginid});
                            }
                          }
                        })
                      }
                    }}
                    />
                    {/*{getKey(fields)==='account_rules'&&formParams.account_rules>5?<Button type="primary" style={{marginLeft:5}}>{getLabel(86,'数据映射')}</Button>:""}*/}
                  </nobr>

                </WeaFormItem></div>),
              colSpan: 1,
              hide: form.isHide(fields, (key, formParams) => {
                let hide = false;
                if ('definebroswerType' == key && formParams.account_rules !== '7') {
                  hide = true;
                }
                return hide;
              }),
              onChange: (v)=> {

              }
            })
          });

          return <WeaSearchGroup center={true}  col={1}  needTigger={true} title={c.title} showGroup={c.defaultshow} items={items}/>
        }
      })}
      </WeaRightMenu>
    </WeaDialog>
  }

  getRightMenu() {
    let btnArr = [
      {
        key: 'BTN_SAVE',
        icon: <i className='icon-coms-Preservation'/>,
        content : `${getLabel(86,'保存')}`,
        onClick : () => {this.onRightMenuClick('BTN_SAVE')}
      }
    ];
    return btnArr;
  }
  
  onRightMenuClick = (type) => {
    const {oauth2Store} = this.props;
    const {registerAppEditForm,registerAppDataMappingEditForm, status} = oauth2Store;
    if(type == 'BTN_SAVE'){
      let id=oauth2Store.registerAppDataMappingId||"";
      let appid = registerAppEditForm.getFormParams().appid;
      let loginid = registerAppDataMappingEditForm.getFormParams().loginid;
      let apploginid = registerAppDataMappingEditForm.getFormParams().apploginid;
      checkUserMappingLoginIdUnique({appId: appid, idValue: id, loginId: loginid, appLoginId: apploginid}).then(result=>{
        if(result&&result.isNotUnique) {
          registerAppDataMappingEditForm.showError('userid',getLabel('31053','已存在，请确认'));
          return;
        }else{
          checkUserMappingAppLoginIdUnique({appId: appid, idValue: id, loginId: loginid, appLoginId: apploginid}).then(result=>{
            if(result&&result.isNotUnique) {
              registerAppDataMappingEditForm.showError('apploginid',getLabel('31053','已存在，请确认'));
              return;
            }else{
              IntegrationUtil.onCheck(registerAppDataMappingEditForm,function () {
                let operation = id==""?"adduser":"edit";
                let params = Object.assign({id:id,appid:appid,operation:operation}, registerAppDataMappingEditForm.getFormParams());
                oauth2Store.getRegisterAppDataMappingOperation(params);
              });
            }

          });
        }

      });

    }

  }

}
