import React from 'react';
import { connect } from 'dva';
import { Modal, Form } from 'antd';
import ImportPage from '../../components/common/ImportPage'; 
import { initFilter, extendRule } from '../../utils/common';

class ImportModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      loading: false
    }
  };

  doShow=()=>{
    this.setState({
      visible: true
    },()=>{
      const {key}=this.props;
      this.props.dispatch({
        type: 'custom/save',
        payload: {
          [key]: true
        },
      });
    }); 
  }
  
  showModal = () => { // 显示弹窗
    const { dispatch,login,formCode,click } = this.props; 
    if(!this.isGetConfig&&formCode){
      const json=initFilter(login.OrgId);
      extendRule(json,'EDIT_FORM_CODE',1,formCode);
      extendRule(json,'ENABLE_STATUS',1,0); 
      dispatch({
        type: 'app/getDataByPost',
        payload: json,
        url: 'FM/ImportConfig/Get',
        onComplete: (ret) => {
          if (ret) {
            this.setState({
              importConfigCode:ret.CODE,
            },()=>{
              this.doShow();
            })
          }
          this.isGetConfig=true;
        }
      });
    }
    else if(this.state.importConfigCode){
      this.doShow();
    }
    if (typeof click === 'function') {
      click();
    }
  };
  handleCancel = () => { // 退出弹窗
    this.props.form.resetFields();
    this.setState({
      visible: false
    });
    if(typeof this.destroyCacheDataFunc==='function'){
      this.destroyCacheDataFunc();
    }
    
  };
  onRegDestroyCacheDataFunc=(destroyCacheDataFunc)=>{
    this.destroyCacheDataFunc=destroyCacheDataFunc;
  };


  render() { 
    const {  key, } = this.props;
    const {  importConfigCode, } = this.state; 
    return (
      <div style={{ display: 'inline-block' }}>
        <span onClick={this.showModal}>{this.props.children}</span>
        <Modal
          visible={this.state.visible}
          title={this.props.title ? this.props.title : "导入"}
          maskClosable={false}
          onCancel={this.handleCancel}
          wrapClassName="custom-modal-wrap"
          style={{ top: "10vh", maxHeight: '80vh', overflow: "hidden" }}
          width={"80%"}
          footer={null}
        >
          {
            importConfigCode ? <ImportPage configCode={importConfigCode} clickKey={key}  /> : '请配置导入配置信息'
          }
        </Modal>
      </div>
    );
  }
}
export default connect(({ app, editPage, loading, login }) => ({ app, editPage, loading, login }))(Form.create()(ImportModal));
