/* eslint-disable */
/* eslint-disable array-callback-return,no-undef */
import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
// // import '@ant-design/compatible/assets/index.css';
import { Layout, Spin } from 'antd-v4';
import styles from '@/index.less';
import Toolbar from '@/components/Common/ToolBar/ToolBarNew';
import CommonBase from '@/components/Common/CommonBase';
import CommonClassifyEvent from '@/components/Common/CommonClassifyEvent';
import StaticEditTable from '@/components/Common/CommonTable';/* 可编辑表格 */
import * as commonBusiness from '@/components/Common/commonBusiness';/* 单据业务功能 */
import SearchComponent from '@/components/Common/SearchComponent';/* 搜索组件 */
import AntdDraggableModal from '@/components/Common/AntdDraggableModal';
import * as commonFunc from '@/components/Common/commonFunc';/* 通用单据方法 */ /* 通用单据方法 */
import * as commonUtils from '@/utils/utils';
import commonConfig from '@/utils/config';

const { Header, Content } = Layout;

class CommonClassify extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }
  handleCancelModal = (modelVisible) => {
    this.props.onSaveState({
      [modelVisible]: false,
    });
  }

  /**   获取过滤树数据数据   */
  handleGetFilterTreeData= (slaveConfig, slaveFilterCondition, page, slaveOrderBy, props) => {
    const { app } = props === undefined ? this.props : props;
    const { conditonValues } = app.currentPane;
    const pageNum = commonUtils.isEmpty(page) ? 1 : page;
    this.props.handleGetTreeDataSet({
      name: 'slave',
      configData: slaveConfig,
      condition: {
        pageNum, pageSize: commonConfig.pageSize, bFilter: slaveFilterCondition, sFilterOrderBy: slaveOrderBy, sSqlCondition: conditonValues,
      },
      flag: true,
      clearSelectData: true,
    });
  };

  render() {
    const { pageLoading } = this.props;
    return (
      <div>
        <Spin spinning={pageLoading}>
          <div>
            <CommonClassifyComponent
              {...this.props}
              {...this.state}
              onCancelModal={this.handleCancelModal}
              onGetFilterTreeData={this.handleGetFilterTreeData}
            />
          </div>
        </Spin>
      </div>
    );
  }
}
const CommonClassifyComponent = Form.create({
  mapPropsToFields(props) {
    const { masterData } = props;
    const obj = commonFunc.mapPropsToFields(masterData, Form);
    return obj;
  },
})((props) => {
  const {
    form, onReturnForm, slavePagination, slaveData, sModelsType, app, slaveConfig,
  } = props;
  /*   回带表单   */
  onReturnForm(form);
  const pagination = {
    pageSize: commonUtils.isNotEmptyNumber(props.iPageSize) && props.iPageSize !== 0 ? props.iPageSize : commonConfig.pageSize,
    ...slavePagination,
    onChange: props.onPaginationChange,
    size: 'large',
    // pageSize: commonUtils.isEmptyArr(slaveData) ? 0 : slaveData.length,
    showQuickJumper: true,
    hideOnSinglePage: true,
  };
  // eslint-disable-next-line no-unused-vars
  const paginationEnabled = {
    pageSize: commonUtils.isNotEmptyNumber(props.iPageSize) && props.iPageSize !== 0 ? props.iPageSize : commonConfig.pageSize,
    total: commonUtils.isEmptyArr(slaveData) ? 0 : slaveData.length,
    current: commonUtils.isEmptyObject(slavePagination) ? 1 : slavePagination.current,
    size: 'large',
    // pageSize: commonUtils.isEmptyArr(slaveData) ? 0 : slaveData.length,
    showQuickJumper: true,
    hideOnSinglePage: true,
  };

  let bTree = false;
  if ((sModelsType === 'commonClassify/materialsClassify' || (commonUtils.isNotEmptyObject(sModelsType) && sModelsType.includes('children'))) && commonUtils.isNotEmptyArr(slaveData)) {
    bTree = true;
  }
  /* 若关联性sSqlCondition配置 则取配置 */
  let keyField = 'sId';
  let keyParentField = 'sParentId';
  let sSqlCondition = '';
  if (commonUtils.isNotEmptyObject(slaveConfig)) {
    sSqlCondition = slaveConfig.sSqlCondition;
    if (commonUtils.isNotEmptyObject(sSqlCondition) && commonUtils.isNotEmptyArr(sSqlCondition.split('.')) && sSqlCondition.split('.').length > 2) {
      keyField = sSqlCondition.split('.')[1];
      keyParentField = sSqlCondition.split('.')[2];
    }
  }
  const slaveTableProps = {
    ...commonBusiness.getTableTypes('slave', props),
    tableProps: { pagination, onChange: props.onTitleChange },
    bRowClick: true, /* 处理多选状态下 修改行字段 选不中问题 */
    // tableProps: commonUtils.isEmpty(props.enabled) ? { pagination, onChange: props.onTitleChange } : { paginationEnabled, onChange: props.onTitleChangeEnabled },
    data: bTree && commonUtils.isNotEmptyArr(slaveData) ? commonUtils.genTreeByArr(slaveData, keyField, keyParentField) : slaveData,
  };
  const sReportParams = commonUtils.isNotEmptyObject(commonFunc.showMessage(app.commonConst, 'sReportParams')) ?
    commonFunc.showMessage(app.commonConst, 'sReportParams') : '上报参数标准';
  const sParamProps = {
    ...commonBusiness.getTableTypes('sParam', props),
    tableProps: {
      rowKey: 'sId',
      pagination,
      onChange: props.onTitleChange,
      sModelsType,
      AutoTableHeight: 350,
      rowSelection: null,
    },
    bRowClick: true,
    enabled: props.enabled,
  };
  const onOkProps = {
    disabled: !props.enabled,
  };
  return (
    <Form>
      <Layout>
        <Header className={styles.header}>
          <Toolbar {...props} />
        </Header>
        <Layout className={styles.clayout}>
          <div className={styles.filterContent}>
            <SearchComponent {...props} />
          </div>
          <Content className="xly-normal-list other-list">
            <StaticEditTable {...slaveTableProps} footer="hidden" noVlist />
          </Content>
        </Layout>
      </Layout>
      {
        props.sParamsModalVisible ?
          <AntdDraggableModal
            width="800px"
            className="paramModal"
            title={sReportParams}
            visible={props.sParamsModalVisible}
            onOk={props.onReportParamsOk}
            okButtonProps={onOkProps}
            onCancel={props.onCancelModal.bind(this, 'sParamsModalVisible')}
            // footer={null}
          >
            <div className="xly-bill-list" >
              <StaticEditTable {...sParamProps} footer="hidden" noVlist />
            </div>
          </AntdDraggableModal> : ''
      }
    </Form>
  );
});

export default CommonBase(CommonClassifyEvent(CommonClassify));

