// 核心库
import React from 'react'
// 组件库
import { Row, Col, Icon } from 'antd'
import TreeBaseComponent from "../TreeBaseComponent"
import Edit from '../../components/Edit/Edit'
// 工具库
import { extend, initFilter } from '../../utils/common'
// 样式
import styles from '../Component.css'
//import { extend } from 'immutability-helper'

class TreeComponent extends TreeBaseComponent {
  constructor(props) {
    super(props)
    this.state = {
      data: {},
      treeConfig: {},
      treeLoadParam: {},
      treeSelectedNodeId: {},
      treeExpandedKeys: {}
    }
  }

  onLoadData () {
    super.onLoadData()
    this.getTreeConfigByPage()
  }

  getTreeConfigByPage () {
    const { formCode } = this.props
    const orgId = this.props.login.OrgId
    const json = initFilter(orgId, formCode)
    if (this.props.setData) {
      this.setState({
        setData: this.props.setData,
      })
    }
    this.props.dispatch({
      type: 'treePage/getTreePageConfigInfo',
      payload: json
    }).then(res => {
      res && this.doGetTreeConfigByPage(res)
    })
  }

  getTreeConfigByTreePage (ret) {
    const isShow = this.props.data ? this.props.data.isShow : false
    const isPermit = this.props.data ? this.props.data.isPermit : false
    const treeConfig = {
      formCode: ret.Nav_Form.CODE,
      formId: ret.Nav_Form.ID,
      api: ret.Nav_PageTree.QUERY_API_URL,
      deleteApi: ret.Nav_PageTree.BATCH_DEL_API_URL,
      relationField: ret.Nav_PageTree.RELATION_FIELD,
      relationId: this.props.data ? this.props.data.id : null,
      sort: ret.Nav_PageTree.SORT,
      order: ret.Nav_PageTree.ORDER,
      orgType: ret.Nav_PageTree.FILTER_ORG_TYPE,
      ignoreOrgRule: ret.Nav_PageTree.IGNORE_ORG_RULE,
      titleColumns: [],
      btns: [],
    }
    if (ret.Nav_Columns && ret.Nav_Columns.length) {
      ret.Nav_Columns.forEach(t => {
        treeConfig.titleColumns.push({
          field: t.FIELD_NAME,
        })
      })
    }
    if (ret.Nav_Btns && ret.Nav_Btns.length) {
      ret.Nav_Btns.forEach(t => {
        if (t.ENABLE_STATUS === 1) return//不启用
        treeConfig.btns.push({
          btnType: t.BTN_TYPE,
          id: t.ID,
          isRule: t.IS_RULE,
          disabled: isShow || isPermit,
          btn_condition: t.BTN_CONDITION || '',
          btn: t,
        })
      })
    }
    return treeConfig
  }

  doGetTreeConfigByPage = (ret) => {
    const treeConfig = this.getTreeConfigByTreePage(ret)
    this.setState({
      treePage: ret.Nav_PageTree ? ret.Nav_PageTree : {},
      formData: ret.Nav_Form ? ret.Nav_Form : {},
    }, () => {
      this.setTreeConfig({ treeConfig })
    })
  }

  onAfterLoadTreeData () {
    if (this.state.treePage && !this.state.treePage.RELATION_FIELD && this.props.data && this.props.data.id) {
      this.selectTreeNode({
        id: this.props.data.id,
      })
    }
  }

  updateTreeDataBySave ({ treeCode, data }) {
    data.isAdd = false
    const treeData = this.getTreeData({ treeCode })
    this.updateTreeDataById(treeData, data.ID, data)
    this.setTreeData({
      treeCode,
      data: treeData,
    })
  }

  onAfterAddTreeNode ({ newData, parentId }) {
    // debugger;
    if(this.props.customParams){
      extend(newData,this.props.customParams);
    }
    this.setState({
      newData: { newData },
      parentId
    })
  }

  onRegDestroyCacheDataFunc = (destroyCacheDataFunc) => {
    this.destroyCacheDataFunc = destroyCacheDataFunc
  }

  onSelectedTreeNode (params) {
    super.onSelectedTreeNode(params)
    if (typeof this.destroyCacheDataFunc === 'function') {
      this.destroyCacheDataFunc()
    }
  }

  render () {
    const data = { ...this.props.data, ...this.state.newData }
    const selectId = this.getSelectedNodeId()
    const treeData = this.getTreeData()
    let treeNodeData = null
    selectId && this.getTreeDataById(treeData, selectId, (ret) => {
      treeNodeData = ret
    })
    return (
      <Row gutter={16} className={styles.treePageWrap}>
        <Col span={7} style={{ height: '100%' }}>{this.getRenderTreeByConfig()}</Col>
        <Col span={17}>
          {
            selectId
              ? (
                <Edit
                  data={data}
                  formCode={this.props.formCode}
                  id={treeNodeData && !treeNodeData.isAdd ? treeNodeData.ID : null}
                  isAdd={treeNodeData ? treeNodeData.isAdd : false}
                  copySrcId={this.state.copySrcId}
                  onSave={(saveData) => { this.updateTreeDataBySave({ data: saveData }) }}
                  parentId={this.state.parentId}
                  setData={this.state.setData}
                  onRegDestroyCacheDataFunc={this.onRegDestroyCacheDataFunc}
                />
              )
              : (
                <div className={styles.nullPage}>
                  <Icon type="form" />
                  <p>无数据，请选择</p>
                </div>
              )
          }
        </Col>
      </Row>
    )
  }
}

export default TreeComponent
