/**
 * Created by mar105 on 2019-02-27.
 */
/* eslint-disable object-curly-newline,prefer-destructuring */
import React, { Component } from 'react';
import { List, Flex } from 'antd-mobile-v2';
import ShowType from './CommobileComponent';
import * as commonUtils from '../../utils/utils';
import styles from '../mobile.less'; /* 通用方法 */

export default class CommobileViewMaster extends Component {
  handleMasterFieldScan = (fieldConfig, tbName, record, newValue, cancelValue) => {
    this.props.onMasterFieldScan(fieldConfig, tbName, record, newValue, cancelValue);
  }
  handleMobileScan = (fieldConfig, tbName, record, newValue, cancelValue) => {
    this.props.onMobileScan(fieldConfig, tbName, record, newValue, cancelValue);
  }
  handleFocus = (fieldConfig) => {
    this.props.onFocus(fieldConfig);
  }

  handleBlur = (fieldConfig) => {
    this.props.onBlur(fieldConfig);
  }

  render() {
    const { masterConfig, masterData, sModelsId, enabled, sUseInfo, app, iTag, bOpenKeyboard } = this.props;
    let masterShowConfig;
    if (iTag === 1) {
      masterShowConfig = commonUtils.isNotEmptyObject(masterConfig) ? masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1) : [];
    } else {
      masterShowConfig = commonUtils.isNotEmptyObject(masterConfig) ? masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag !== 1) : [];
    }

    const formItemLayout = { labelCol: { span: 2 }, wrapperCol: { span: 18 } };
    return (
      <div className={iTag === 1 ? styles.onlyWordBg : ''} style={{ overflow: 'hidden', padding: '5px' }}>
        {
          // eslint-disable-next-line array-callback-return
          masterShowConfig.map((child, index) => {
            const sMemo = child.sName.toLowerCase().endsWith('memo');
            // sUseInfo是指被其他单据调用，或者被审核
            let enabledNew = (enabled && !child.bReadonly && !child.specialControl && commonUtils.isEmpty(sUseInfo));
            if (child.iTag === 1) {
              enabledNew = false;
            } else if (child.iTag === 3) {
              enabledNew = true;
            }
            const bNotViewTitle = child.bNotViewTitle; /* 不显示标题 */
            const iRowNum = child.iColValue === 1 ? 24 : 1; /* 每个字段占的网格个数 ，网格总个数是24 */
            const iColValue = sMemo ? 24 : child.iColValue * iRowNum; /* 跨度 */
            // eslint-disable-next-line no-unused-vars
            const iRowValue = commonUtils.isEmptyNumber(child.iRowValue) || child.iRowValue === 0 ? 1 : child.iRowValue; /* 行高 */
            const showTypeProps = {
              app,
              record: masterData,
              sId: masterData.sId, /*   修改当前编号(数据格式:字符串)   */
              name: 'master',
              form: this.props.form,
              formId: sModelsId,
              getSqlDropDownData: this.props.getSqlDropDownData,
              getSqlCondition: this.props.getSqlCondition,
              handleSqlDropDownNewRecord: this.props.handleSqlDropDownNewRecord,
              getFloatNum: this.props.getFloatNum,
              getDateFormat: this.props.getDateFormat,
              onChange: this.props.onChange,
              onFocus: this.handleFocus,
              onBlur: this.handleBlur,
              showConfig: child,
              formItemLayout: sMemo ? formItemLayout : {},
              textArea: sMemo,
              enabled: enabledNew,
              dataValue: masterData[child.sName],
              bTable: false,
              onFilterDropDownData: this.props.onFilterDropDownData,
              onSaveState: this.props.onSaveState,
              onFieldScan: this.handleMasterFieldScan,
              onMobileScan: this.handleMobileScan,
              bOpenKeyboard,
            };
            if (child.iTag === 1) {
              if (index > -1) {
                return (
                  <div className={iColValue === 24 ? 'singleItem' : (iColValue === 12 ? 'doubleItem' : 'treeItem')} style={{ marginBottom: '12px' }}>
                    <Flex style={{ background: '#f37d3d', color: '#f5f5f5', padding: '0 15px 5px 15px', fontSize: '16px', alignItems: 'center' }} wrap="wrap" align="start">
                      {
                        bNotViewTitle ?
                          <><Flex.Item style={{ fontWeight: 'bold', paddingLeft: '0.5px' }}> {masterData[child.sName]}</Flex.Item></>
                          :
                          <><div className="flexTitle">{child.showName}</div><div style={{ fontWeight: 'bold' }}>：</div><Flex.Item style={{ paddingLeft: '0.5px' }}> {masterData[child.sName]}</Flex.Item></>
                      }
                    </Flex>
                  </div>
                );
              }
            } else {
              return (
                <List>
                  <ShowType {...showTypeProps} />
                </List>
              );
            }
          })
        }
      </div>
    );
  }
}
