import React from 'react'
import { connect } from 'dva'
import { Modal, Form, message } from 'antd'
// import FormPage from './FormPage'
// import { permissionUtils, getCustomParams } from '../utils/common'
import { permissionUtils } from '../utils/common'

class FormModal extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      visible: false,
      formData: {}
    }
  }

  doShowModel = () => {
    let isReturn = false
    const click = this.props.click
    if (typeof click === 'function') {
      isReturn = click(true)
    }
    // 未启用
    if (this.state.formData.ENABLE_STATUS === 1) {
      message.error('当前表单' + this.state.formData.CODE + '已失效，请联系管理员')
      isReturn = true
    }
    if (isReturn) return
    // 权限控制
    if (!this.state.formData.IS_IGNORE_PERMISSION && !permissionUtils(this.props.login).checkForm(this.state.formData.ID)) {
      message.error('您无浏览表单' + this.state.formData.CODE + '的权限，请联系管理员')
      isReturn = true
    }
    if (isReturn) return
    this.setState({
      visible: true
    }, () => {
      const data = this.props.data
      this.props.dispatch({
        type: 'editPage/save',
        payload: {
          [data ? data.reloadKey : '']: true
        }
      })
    })
  }

  showModal = (e) => { // 显示弹窗
    e.stopPropagation()
    if (!this.state.formData.ID) {
      this.fetch(() => {
        this.doShowModel()
      })
      return
    }
    this.doShowModel()
  }

  handleCancel = () => { // 退出弹窗
    this.props.form.resetFields()
    this.setState({
      visible: false
    })
    const click = this.props.click
    if (typeof click === 'function') {
      click(false)
    }
  }

  fetch = (onComplete) => {
    const { formCode, dispatch } = this.props
    const orgId = this.props.login.OrgId
    const OJson = { 'Key': formCode, 'orgid': orgId }
    dispatch({
      type: 'app/getFormByRedis',
      payload: OJson,
      onComplete: (ret) => {
        if (ret) {
          this.setState({
            formData: ret
          }, () => {
            if (onComplete) {
              onComplete()
            }
          })
        }
      }
    })
  }

  render () {
    // const { id, formCode, formParam, data, customParams, onSave, parentId, copySrcId, isAddH } = this.props
    // const custParams = getCustomParams(customParams)
    // const tmpData = {
    //   formCode,
    //   data: {
    //     ...data,
    //     ...custParams,
    //     customParams,
    //     formParam,
    //     close: this.handleCancel
    //   },
    //   id,
    //   onSave,
    //   parentId,
    //   copySrcId,
    //   isAddH,
    //   onCancel: this.handleCancel
    // }
    return (
      <div style={{ display: 'inline-block' }}>
        <span onClick={this.showModal}>{this.props.children}</span>
        <Modal
          visible={this.state.visible}
          title={this.props.data && this.props.data.isShow ? ('表单[' + this.props.formCode + ']') : ('表单[' + this.props.formCode + ']')}
          maskClosable={false}
          onCancel={this.handleCancel}
          footer={null}
          className='antd-modal-fullscreen'
        >
          {/* <FormPage {...tmpData} /> */}
        </Modal>
      </div>
    )
  }
}
export default connect(({ app, login }) => ({ app, login }))(Form.create()(FormModal))
