import React from 'react'
import { connect } from 'dva'
import { extendRule, initQueryFilter } from "../../../utils/common"
import Edit from '../../Edit/Edit'

class BaseConfigPage extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      formData: {},
      editList: []
    }
  }

  componentDidMount () {
    this.loadData()
  }

  loadData = () => {
    const { formId } = this.props
    const orgId = this.props.login.OrgId
    const OJson = { 'Key': formId, 'orgid': orgId }
    this.props.dispatch({
      type: 'app/getFormByRedis',
      payload: OJson,
      onComplete: (ret) => {
        if (ret) {
          this.setState({
            formData: ret
          }, () => {
            const json = initQueryFilter(orgId, 1, 100, "NUM", 0)
            extendRule(json, "PAGE_FORM_ID", 1, ret.ID)
            this.props.dispatch({
              type: 'app/getDataByPost',
              url: 'PF/PageEdit/OrderEntities',
              payload: json,
              onComplete: (ret) => {
                this.setState({
                  editList: ret
                })
              }
            })
          })
        }
      }
    })
  }

  getEditComponent = () => {
    const { editList, formData } = this.state
    const { data, login } = this.props
    return editList.map(item => {
      const editProps = {
        data,
        formCode: formData.CODE,
        formId: formData.ID,
        id: login.baseConfig ? login.baseConfig.ID : '',
        code: item.CODE,
      }
      return <Edit {...editProps} />
    })
  }

  render () {
    return (
      <div>{this.getEditComponent()}</div>
    )
  }
}

export default connect(({ login }) => ({ login }))(BaseConfigPage)
