import React from 'react'
import { connect } from 'dva'
import { Modal, Form } from 'antd'
import Edit from './Edit'
import BatchEdit from './BatchEdit'

class EditPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      visible: false,
      loading: false
    }
    this.formName=this.props.app.currActivatedMenu.NAME;//this.props.app.currActivatedMenu.Nav_MenuForm.NAME
  };
  showModal = () => { // 显示弹窗
    this.setState({
      visible: true
    }, () => {
      const { data } = this.props
      this.props.dispatch({
        type: 'editPage/save',
        payload: {
          [data ? data.reloadKey : '']: true
        },
      })
    })
    const click = this.props.click
    if (typeof click === 'function') {
      click(true)
    }
  };
  handleCancel = () => { // 退出弹窗
    this.props.form.resetFields()
    this.setState({
      visible: false
    })
    if (typeof this.destroyCacheDataFunc === 'function') {
      this.destroyCacheDataFunc()
    }
    const click = this.props.click
    if (typeof click === 'function') {
      click(false)
    }
  };
  onRegDestroyCacheDataFunc = (destroyCacheDataFunc) => {
    this.destroyCacheDataFunc = destroyCacheDataFunc
  };


  render () {
    const tmpData = {
      id: this.props.id,
      onSave: this.props.onSave,
      formCode: this.props.formCode,
      parentId: this.props.parentId,
      data: this.props.data,
      extraSaveData: this.props.extraSaveData,
      copySrcId: this.props.copySrcId,
      onCancel: this.handleCancel,
      isAddH: this.props.isAddH,
      customParams: this.props.customParams,
      onRegDestroyCacheDataFunc: this.onRegDestroyCacheDataFunc,
    }
    //console.log(this.props.title);
    const isEdit = tmpData.data ? !tmpData.data.isBatchEdit : true
    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 +'-'+ this.formName + '[' + tmpData.formCode + ']') : ('自定义[' + tmpData.formCode + ']')}
          maskClosable={false}
          onCancel={this.handleCancel}
          wrapClassName="custom-modal-wrap"
          style={{ top: "10vh", maxHeight: '80vh', overflow: "hidden" }}
          width={"80%"}
          footer={null}
        >
          {
            isEdit ? <Edit  {...tmpData} /> : <BatchEdit {...tmpData} />
          }
        </Modal>
      </div>
    )
  }
}
export default connect(({ app, editPage, loading, login }) => ({ app, editPage, loading, login }))(Form.create()(EditPage))
