// 组件库
import TreeBaseComponent from "../TreeBaseComponent/HIndex"
// 工具库
import { initFilter } from '../../utils/common'

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

  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,
      canAddNode: ret.Nav_PageTree.CAN_ADD_NODE,
      expandLevel: ret.Nav_PageTree.LEVEL,
      showModel: ret.Nav_PageTree.SHOW_MODEL,
      multiCheck: ret.Nav_PageTree.MULTI_CHECK,
      multiCheckCount: ret.Nav_PageTree.MULTI_CHECK_COUNT,
      ignoreOrgRule: ret.Nav_PageTree.IGNORE_ORG_RULE,
      labelName: ret.Nav_PageTree.LABEL_NAME,
      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,
          code: t.CODE,
          label: t.LABEL,
          clickEventFunName: t.CLICK_EVENT,
          css: t.CSS,
          icon: t.ICON,
          formCode: t.FORM_CODE,
          isSameLevel: t.IS_SAME_LEVEL,
          customParams: t.CUSTOM_PARAMS,
          checkSelected: t.SELECT_SHOW,
          isConfirm: t.IS_COMFIRM,
          api: t.API_URL,
          num: t.NUM,
          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 : {},
      picFilter: ret.Nav_PicFilter ? ret.Nav_PicFilter : []
    }, () => {
      this.setTreeConfig({
        treeConfig
      })
    })
  }

  render () {
    return this.getRenderTreeByConfig()
  }
}

export default TreeComponent
