// 核心库
import React from 'react'
import { connect } from 'dva'
// 组件库
import { Search } from '@woowalker/feui'
import ListTablePage from './ListTablePage'

class ListPage extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      filterList: [],
      tableData: [],
      tableColumns: [],
      jsFile: '',
      cusBotton: [],
      url: '',
      selectedRowKeys: [],
      pageTable: {},
      isfirstLoad: true,
      isWindows: true,
      formData: {},
      loadDataFunc: []
    }
    this.jsonTable = {
      "RuleCount": 0,
      "IncludeCount": 0,
      "OrderCount": 0,
      "PageIndex": 1,
      "Limit": 10,
      "Start": 0,
      "Sort": "CREATE_TIME",
      "Order": 1,
      FilterGroupCount: 0,
    }
    // 直接搜索被终止，用于 table 的配置接口还未返回情况，这个时候在配置拉取完成时，就需要重新进行搜索
    // 一般用在外部配了个 Search 组件，然后直接通过 ListPage 的 ref 进行 loadData 操作的情况下
    this.directSearchStop = false
  }

  componentDidMount() {
    if (this.props.onRef) {
      this.props.onRef(this)
    }
    if (window.navigator.userAgent.indexOf("Windows") < 1) {
      this.setState({
        isWindows: false
      })
    }
  }

  loadData = (params) => {
    if (this.state.loadDataFunc) {
      if (!this.state.loadDataFunc.length) {
        this.directSearchStop = true
        this.directSearchParams = params
      }
      this.state.loadDataFunc.forEach(item => {
        if (typeof item === 'function') {
          if (this.directSearchStop && this.directSearchParams) {
            item({ param: this.directSearchParams })
          } else {
            item({ param: params })
          }
        }
      })
      if (this.state.loadDataFunc.length) {
        this.directSearchStop = false
        this.directSearchParams = undefined
      }
    }
  }

  handleLoadData({ loadFunc, pageCode }) {
    const data = [...this.state.loadDataFunc]
    data.push(loadFunc)
    this.setState({
      loadDataFunc: data
    }, () => {
      if (!this.props.preventDefaultSearch || this.directSearchStop) {
        this.refOfSearch && this.refOfSearch.handleSearch()
      }
    })
  }

  handleLoadAllData = ({ params }) => {
    this.loadData(params)
  }

  handleLoadParam = () => {
    let data = { ...this.props.data }

    if (!data) {
      data = {}
    }
    if (!data.rules) {
      data.rules = []
    }

    const formParam = this.props.formParam

    if (formParam) {
      for (var val in formParam) {
        let rule = {
          field: val,
          operator: 0,
          value: formParam[val],
          isCustom: true,
          isSysParam: false,
        }
        data.rules.push(rule)
      }
    }

    return { 'data': data }
  }

  render() {
    const { formCode, data, onRowSelectChange, rowSelectedKeys, preventDefaultSearch } = this.props
    const { onClickRow } = data ? data : {}
    const { isWindows } = this.state;
    return (
      <>
        {
          isWindows ? <Search
            onRef={ref => this.refOfSearch = ref}
            preventDefaultSearch={preventDefaultSearch}
            formCode={formCode}
            user={this.props.login.user}
            onSearch={(value) => this.loadData(value)}
          /> : null
        }
        <ListTablePage
          formCode={formCode}
          onRegLoadDataFunc={this.handleLoadData.bind(this)}
          onLoadParam={this.handleLoadParam}
          onLoadAllData={this.handleLoadAllData}
          onClickRow={onClickRow}
          onRowSelectChange={onRowSelectChange}
          rowSelectedKeys={rowSelectedKeys}
          isQuery={this.props.isQuery || false}
          data={this.props.data}
          customParams={this.props.customParams}
        />
      </>
    )
  }
}

export default connect(({ app, loading, listPage, login, search }) => ({ app, loading, listPage, login, search }))(ListPage)