/*
 * Create:      柿子
 * CreateDate:  2023/07/04
 * Describe：   订单列表/汇总订单明细查询
 * */
import React, { Component } from 'react';
import { Modal } from 'antd';
import { Util } from 'tools';
import PubilcTablePagination from 'pages/common/PubilcTablePagination.jsx';
import '../style/index.less';

class OrderDetailsQuery extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      loading: false,
      rowID: '',
      tableData: [],
      columns: [],
      totalWidth: 0,
      propsRecord: {},
      componentName: '', // 组件名
    }
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  }

  // 修改弹窗状态
  modifyVisible = (visible, record) => {
    this.setState({ visible, propsRecord: record }, () => {
      visible && this.getTalbeData();
    })
  }

  // 获取列表数据
  getTalbeData = async () => {
    try {
      const { columns, propsRecord, componentName } = this.state;
      this.setState({ loading: true });
      let data = {
        params: [{
          ...propsRecord,
        }]
      }
      let res = await React.$asyncPost(this, propsRecord?.queryCode || '', data);
      let tableData = Util.addKeyValueToDataSource(res && res.result && 'rows' in res.result ? res.result.rows : (Array.isArray(res?.result || '') ? res.result : []));
      let resComponent = res?.componentName || 'OrderDetailsQuery';
      if ((!(columns && columns.length > 0) || resComponent !== componentName)) { // 获取表头数据
        this.getTableColumns(resComponent);
      }
      this.setState({ tableData, loading: false, rowID: '', componentName: resComponent });
    } catch (error) {
      console.log(error);
      this.getTableColumns();
      this.setState({ loading: false });
    }
  }

  // 获取列表表头数据
  getTableColumns = async (componentName) => {
    try {
      let data = {
        params: [{
          compontName: componentName || 'OrderDetailsQuery',
          type: 'C',
        }]
      }
      let res = await React.$asyncPost(this, '01040073', data);
      this.setState({ columns: res.result?.C || [], totalWidth: res.totalWidth });
    } catch (error) {
      console.log(error);
    }
  }

  // 操作行
  onClickRowPublic = (record) => {
    return {
      // 单击行选中
      onClick: () => {
        if (this.state.rowID === '' || (this.state.rowID && (this.state.rowID !== record.key))) {
          this.setState({ rowID: record.key });
        } else {
          this.setState({ rowID: '' });
        }
      }
    }
  }

  // 选中行操作
  setRowClassNamePublic = (record) => {
    return record.key === this.state.rowID ? 'clickRowStyle' : '';
  }

  render() {
    const { visible, loading, tableData, columns, totalWidth, componentName } = this.state;
    return (
      <Modal
        title="订单列表"
        width="72vw"
        footer={null}
        visible={visible}
        onCancel={() => this.modifyVisible(false)}
      >
        <div className="table-body-height">
          <PubilcTablePagination
            param={{
              loading,
              columns, // 表头配置
              data: tableData, // 表格数据
              x: totalWidth, // 表格的宽度
              y: 500,
              height: 550,
              componentName: componentName || 'OrderDetailsQuery'
            }}
            getColumns={this.getTableColumns}
            onClickRowPublic={this.onClickRowPublic}
            setRowClassNamePublic={this.setRowClassNamePublic}
          />
        </div>
      </Modal>
    )
  }
};

export default OrderDetailsQuery;