/**
 * @Date:   2019-05-28T13:23:00+08:00
 * @Last modified time: 2019-05-29T15:33:03+08:00
 */
/**
 * Created by mar105 on 2019-03-27.
 */
import React, { Component } from 'react';
import { Form } from '@ant-design/compatible';
// import '@ant-design/compatible/assets/index.css';
import { Layout, Spin, Button, Row, Col } from 'antd-v4';
import CommonListEvent from './CommonListEvent';/* 继承销售模块业务功能 */
import * as commonFunc from './commonFunc';/* 通用单据方法 */ /* 通用单据方法 */
import StaticEditTable from './CommonTable';/* 可编辑表格 */
import CommonBase from './CommonBase';/* 获取配置及数据 */
import * as commonBusiness from './commonBusiness';/* 单据业务功能 */
import SearchComponent from './SearchComponent';/* 搜索组件 */
// import commonConfig from '../../utils/config';
import config from '../../utils/config';
import * as commonUtils from '../../utils/utils';
import Eject from '../../assets/eject.svg';
import StaticEditTree from './Tree/StaticTree';
import Close from '../../assets/close.svg';

const { Content } = Layout;


class CommonList extends Component {
  constructor(props) {
    super(props);
    this.state = {
    };
  }

  // ----------------------------数据修改回带end  ---------------------------- //
  handleEject = (e) => {
    const filterTree = e.currentTarget.parentNode;/* 获取当前页签下的树组件 */
    if (filterTree.attributes.class.value.indexOf('xly-eject') > -1) {
      // eslint-disable-next-line no-unused-expressions
      filterTree.nextSibling.style.display = 'block';
    } else if (filterTree.attributes.class.value.indexOf('xly-tree-close') > -1) {
      // eslint-disable-next-line no-unused-expressions
      filterTree.parentNode.style.display = 'none';
    }
  };

  /**   树节点选中   */
  handleTreeSelect = (name, checkedKeys, e) => {
    this.props.onSelect(name, checkedKeys, e); /* 调用CommonListEvent通用处理 */
  };

  /**   确认下单   */
  handleSelect = () => {
    const {
      slaveFinalSelectedRowKeys, app, slaveSelectedData, slaveFinalData,
    } = this.props;
    let slaveFinalSelectedData = [];
    if (commonUtils.isNotEmptyArr(slaveFinalSelectedRowKeys)) {
      slaveFinalSelectedData = slaveFinalData.filter(item => slaveFinalSelectedRowKeys.includes(item.sSlaveId));
    }
    this.props.app.currentPane.select(app.currentPane.name, app.currentPane.config, slaveSelectedData, slaveFinalSelectedData);
    this.props.app.currentPane.selectCancel(app.currentPane.name);
  };
  handleDoubleClickSelect = () => {
    const { slaveConfig } = this.props;
    if (commonUtils.isNotEmptyObject(slaveConfig) && !slaveConfig.bMutiSelect) {
      this.handleSelect();
    }
  }
  /**   关闭   */
  handleCancelModal = () => {
    const { app } = this.props;
    this.props.app.currentPane.selectCancel(app.currentPane.name);
  };
  // ----------------------------数据修改回带end  ---------------------------- //

  render() {
    const { pageLoading } = this.props;
    return (
      <div style={{ height: '100%' }}>
        <Spin spinning={pageLoading}>
          <div style={{ height: '100%' }}>
            <CommonListComponent
              {...this.props}
              {...this.state}
              onSelect={this.handleSelect}
              onCancel={this.handleCancelModal}
              onDoubleClick={this.handleDoubleClickSelect}
              onEject={this.handleEject.bind(this)}
              onSelectTree={this.handleTreeSelect}
            />
          </div>
        </Spin>
      </div>
    );
  }
}

const CommonListComponent = Form.create({
  mapPropsToFields(props) {
    const { masterData } = props;
    const obj = commonFunc.mapPropsToFields(masterData, Form);
    return obj;
  },
})((props) => {
  const {
    form, onReturnForm, slavePagination, sModelsType,
  } = props;
  /*   回带表单   */
  onReturnForm(form);
  const pagination = {
    ...slavePagination,
    size: 'large',
    pageSize: sModelsType === 'modal/logView' ? 9999 : config.pageSize,
    showQuickJumper: true,
    hideOnSinglePage: true,
  };
  const tableProps = {
    ...commonBusiness.getTableTypes('slave', props),
    tableProps: {
      rowKey: 'sSlaveId', pagination, onChange: props.onTitleChange, AutoTableHeight: 350,
    },
  };

  const name = 'filterTree';
  const treeProps = {
    ...commonBusiness.getTreeTypes('tree', props),
    isSearch: false,
    checkable: false,
    disabled: false,
    checkedAll: false,
    unChecked: false,
    [`${name}Column`]: props[`${name}Column`],
    [`${name}Config`]: props[`${name}Config`],
    [`${name}Data`]: props[`${name}Data`],
    getFloatNum: props.getFloatNum,
    getSqlDropDownData: props.getSqlDropDownData,
    getSqlCondition: props.getSqlCondition,
    handleSqlDropDownNewRecord: props.handleSqlDropDownNewRecord,
    getDateFormat: props.getDateFormat,
    onDoubleClick: props.onDoubleClick,
    onSelect: props.onSelectTree,
    expandedKeys: props.expandedKeys,
  };
  const realizeHeight = commonUtils.isNotEmptyNumber(props.realizeHeight) ? props.realizeHeight : 0;
  const tableFilterProps = {
    ...commonBusiness.getTableTypes('slaveFinal', props),
    rowSelectionType: 'checkbox',
    tableProps: { rowKey: 'sSlaveId', onChange: props.onTitleChange, AutoTableHeight: 350 },
  };
  return (
    <Form >
      <Layout>
        <Layout>
          <div id="modalChooseProductMaterial">
            <div style={{ maxHeight: '100%', zIndex: 20 }}>
              <SearchComponent {...props} />
            </div>
            <Content
              className={realizeHeight <= 0 ? 'xly-normal-list' : 'xly-normal-list resize-container-select'}
              style={{ height: realizeHeight > 0 ? `${500 + realizeHeight}px` : 'auto', paddingLeft: '10px' }}
            >
              <Row gutter={[8, 0]}>
                <Col span={15}>
                  <StaticEditTable {...tableProps} footer="hidden" tableBelone="list" showConfig={props.showConfig} />
                </Col>
                <Col span={9}>
                  <StaticEditTable {...tableFilterProps} footer="hidden" tableBelone="list" showConfig={props.showConfig} />
                </Col>
              </Row>
            </Content>
          </div>
        </Layout>
        {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
        {commonUtils.isEmptyArr(treeProps.treeData) || !commonUtils.isEmpty(props.routing) ? '' : <div className="xly-eject" style={{ position: 'absolute', right: '0px' }}> <a onClick={props.onEject}> <img src={Eject} alt="eject" /></a> </div>}
        {commonUtils.isEmptyArr(treeProps.treeData) || !commonUtils.isEmpty(props.routing) ? '' :
        <div className="xly-filter-tree">
          <StaticEditTree {...treeProps} />
          <div className="xly-tree-close" >
            {/* eslint-disable-next-line jsx-a11y/anchor-is-valid */}
            <a onClick={props.onEject}> <img src={Close} alt="close" /></a>
          </div>
        </div>
        }
        {sModelsType !== 'search/workSchedule' ?
          <div style={{ textAlign: 'right', marginRight: '9px', marginBottom: '9px' }}>
            <Button key="back" style={{ marginRight: '8px' }} onClick={props.onCancel}>取消</Button>
            <Button type="primary" onClick={props.onSelect}>确认</Button>
          </div> : ''
        }
      </Layout>
    </Form>
  );
});

export default CommonBase(CommonListEvent(CommonList));
