// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import ListPage from './Table/ListPage'
import EditListPage from './EditListTable'
import CustomPage from './Custom/CustomPage'
import TreePage from './Tree/TreePage'
import HTreePage from './Tree/HTreePage'
import Edit from './Edit/Edit'
import BatchEdit from './Edit/BatchEdit'
import ChartPage from './Chart'
import PicturePages from './PicturePages/PicturePages'
import CombinationPage from './Combination/CombinationPage'
import PermissionDenyPage from './common/PermissionDenyPage'
// 工具库
import { permissionUtils } from "../utils/common"

export function RenderPageItem({ props, state }) {
  const { formData } = state
  if (formData) {
    //未启用
    if (!formData.ENABLE_STATUS === 1) {
      return <div>当前表单{formData.CODE}已失效，请联系管理员</div>
    }
    const { formParam, data, setData } = props
    const { FORM_TYPE: formType, ID, CODE, IS_IGNORE_PERMISSION } = formData
    // 权限控制
    const hasPermission = props.data?.homeReload||IS_IGNORE_PERMISSION || permissionUtils(props.login).checkForm(ID)

    if (formType === 0) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <ListPage formCode={CODE} data={data} formParam={formParam} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 1) {
      const isBatchEdit = data ? data.isBatchEdit : false
      const tmpData = {
        id: data.id,
        onSave: data.onSave,
        formId: ID,
        formCode: CODE,
        onCancel: data.onCancel,
        homeLoad:props.data?.homeReload
      }
      if (data.IsAdd) {
        tmpData.isAddH = true
      }
      if (isBatchEdit) {
        return (
          <PermissionDenyPage permission={hasPermission}>
            <BatchEdit {...tmpData} data={data} formParam={formParam} />
          </PermissionDenyPage>
        )
      }
      return (
        <PermissionDenyPage permission={hasPermission}>
          <Edit {...tmpData} data={data} formParam={formParam} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 2) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <TreePage formCode={CODE} data={data} formParam={formParam} setData={setData} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 3) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <ChartPage formCode={CODE} data={data} formParam={formParam} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 4) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <PicturePages formCode={CODE} data={data} formParam={formParam} setData={setData} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 5) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <ChartPage formCode={CODE} data={data} formParam={formParam} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 6) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <HTreePage formCode={CODE} data={data} formParam={formParam} setData={setData} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 7) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <CombinationPage formCode={CODE} data={data} formParam={formParam} formData={formData} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 8) {
      return (
        <PermissionDenyPage permission={hasPermission}>
          <EditListPage formCode={CODE} data={data} />
        </PermissionDenyPage>
      )
    }
    else if (formType === 99) {
      // const tmpData = {
      //   id: data?.id,
      //   onSave: data?.onSave,
      //   formId: ID,
      //   formCode: CODE,
      //   onCancel: data?.onCancel,
      // }
      // if (data?.IsAdd) {
      //   tmpData.isAddH = true
      // }
      return (
        // <PermissionDenyPage permission={hasPermission}>
        <CustomPage formCode={CODE} formParam={formParam} data={data} />
        // </PermissionDenyPage>
      )
    }
  }
  return <div />
}

class FormPage extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      formData: {}
    }
  }

  componentDidMount() {
    this.fetch(this.props.formCode)
  }


  componentWillReceiveProps(NextProps) {
    if (NextProps.formCode) {
      if (this.props.formCode != NextProps.formCode) {
        this.fetch(NextProps.formCode)
      }

    } else {
      this.setState({
        formData: {}
      })
    }

  }


  fetch = (formCode) => {
    const { login, dispatch } = this.props

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

  render() {
    return <RenderPageItem props={this.props} state={this.state} />
  }
}

export default connect(({ app, login }) => ({ app, login }))(FormPage)
