import React from 'react';
import { connect } from 'dva';
import { Modal, Form } from 'antd';
import ListPage from './ListPage';

class TableModal extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      loading: false
    }
  };
  showModal = () => { // 显示弹窗
    this.setState({
      visible: true
    }, () => {
      const { data } = this.props;
      this.props.dispatch({
        type: 'editPage/save',
        payload: {
          [data ? data.reloadKey : '']: true
        },
      });
    });
    const click = this.props.click;
    if (typeof click === 'function') {
      click(true);
    }
  };
  handleCancel = () => { // 退出弹窗
    this.props.form.resetFields();
    this.setState({
      visible: false
    });
    if (typeof this.destroyCacheDataFunc === 'function') {
      this.destroyCacheDataFunc();
    }
    const click = this.props.click;
    if (typeof click === 'function') {
      click(false);
    }
  };
  onRegDestroyCacheDataFunc = (destroyCacheDataFunc) => {
    this.destroyCacheDataFunc = destroyCacheDataFunc;
  };
  handleLoadParam = () => {
    //this.searchFormRef.handleSearch();
    let rule = [
      {
        field: this.props.customParams,
        operator: 1,
        value: this.props.id
      }
    ];
    return {
      data: {
        rules: [...rule],
      }
    };
  };

  render() {
    const tmpData = {
      id: this.props.id,
      onSave: this.props.onSave,
      formCode: this.props.formCode,
      parentId: this.props.parentId,
      data: this.props.data,
      copySrcId: this.props.copySrcId,
      onCancel: this.handleCancel,
      isAddH: this.props.isAddH,
      onRegDestroyCacheDataFunc: this.onRegDestroyCacheDataFunc,
    };
    //console.log(tmpData);

    let rule = [
      {
        field: this.props.customParams,
        operator: 1,
        value: this.props.id
      }
    ];
    let params = {
      rules: [...rule],
    }
      ;
    let customJson = {
    };
    customJson[this.props.customParams] = this.props.id;
    //console.log(params);
    //const isEdit=tmpData.data?!tmpData.data.isBatchEdit:true;
    return (
      <div style={{ display: 'inline-block' }}>
        <span onClick={this.showModal}>{this.props.children}</span>
        <Modal
          visible={this.state.visible}
          title={this.props.title ? (this.props.title + '[' + tmpData.formCode + ']') : ("自定义" + '[' + tmpData.formCode + ']')}
          maskClosable={false}
          onCancel={this.handleCancel}
          style={{ top: "10vh", maxHeight: '80vh', overflow: "hidden" }}
          bodyStyle={{ backgroundColor: '#f2f2f2', padding: 0, minHeight: '40vh' }}
          width={"80%"}
          footer={null}
        >
          {
            //isEdit ? <Edit  {...tmpData}  /> : <BatchEdit {...tmpData}/>

            // <ListTablePageBase
            //  key="ID"
            //  tableId={tmpData.id}
            //  //formData={tmpData.data}
            //  formCode={tmpData.formCode}
            //  //onRegLoadDataFunc={this.handleLoadDataFunc}
            //  onLoadParam={this.handleLoadParam}
            //  //formId={this.props.formId}
            //  //SetRowClass={this.setRowClass}
            //  //onClickRow={this.handleOrderRowClick}
            //  isQuery={true}
            //  //onTableData={this.onOrderTableData}
            //  />
            <ListPage formCode={tmpData.formCode} data={params} isQuery={true} customParams={customJson} />
          }

        </Modal>
      </div>
    );
  }
}
export default connect(({ app, editPage, loading, login }) => ({ app, editPage, loading, login }))(Form.create()(TableModal));
