import React from 'react';
import { Search } from '@woowalker/feui'
import TableComponent from '../TableComponent';


class TableListComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      filterList: [],
      tableData: [],
      tableColumns: [],
      jsFile: '',
      cusBotton: [],
      url: '',
      isWindows: true,
      selectedRowKeys: [],
      pageTable: {},
      isfirstLoad: true,
      formData: {},
      loadDataFunc: []
    };
    this.jsonTable = {
      "RuleCount": 0,
      "IncludeCount": 0,
      "OrderCount": 0,
      "PageIndex": 1,
      "Limit": 10,
      "Start": 0,
      "Sort": "CREATE_TIME",
      "Order": 1,
      FilterGroupCount: 0,
    };
  };
  componentDidMount() {
    if (window.navigator.userAgent.indexOf("Windows") < 1) {
      this.setState({
        isWindows: false
      })
    }
  };

  loadData = (params) => {
    if (this.state.loadDataFunc) {
      this.state.loadDataFunc.forEach(item => {
        if (typeof item === 'function') {
          item({ param: params });
        }
      });
    }
  };

  async handleLoadData({ loadFunc, pageCode }) {
    const data = this.state.loadDataFunc ? [...this.state.loadDataFunc] : [];
    data.push(loadFunc);
    await this.setState({
      loadDataFunc: data
    });

    this.refOfSearch && this.refOfSearch.handleSearch()
  };

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

  handleLoadParam = () => {
    return { 'data': this.props.data };
  };

  onRenderTable({ formCode, onClickRow }) {
    return <TableComponent
      {...this.props}
      formCode={formCode}
      onRegLoadDataFunc={this.handleLoadData.bind(this)}
      onLoadParam={this.handleLoadParam}
      onLoadAllData={this.handleLoadAllData}
      onClickRow={onClickRow}
      isQuery={false}
    />
  }


  render() {
    const { formCode, data } = this.props;
    const { onClickRow } = data ? data : {};
    const { isWindows } = this.state;
    return (
      <div>
        {
          isWindows ? <Search
            onRef={ref => this.refOfSearch = ref}
            formCode={formCode}
            onSearch={(value) => this.loadData(value)}
          /> : null
        }

        {
          this.onRenderTable({ formCode, onClickRow })
        }
      </div>
    );
  }
}

export default TableListComponent;
