/* eslint-disable */
import React from 'react';
import weixin from 'weixin-js-sdk';
import { Flex, Button, ListView, Toast } from 'antd-mobile-v2';
import { createForm } from 'rc-form';
import 'antd-mobile-v2/dist/antd-mobile.css';
import CommobileBase from './CommobileBase';
import CommobileBillEvent from './CommobileBillEvent';
import CommobileViewMaster from './CommobileViewMaster';
import commonConfig from '../../utils/config';
import * as commonUtils from '../../utils/utils';
import ShowType from './CommobileComponent';
import CommobileToolBar from './CommobileToolBar';
import styles from '../mobile.less';
import add from '../../assets/mobile/add-select.svg';
import scan from '../../assets/mobile/scanning.svg';
import close from '../../assets/mobile/close.svg';
import * as commonFunc from '../../components/Common/commonFunc';
import BarCodeUtil from './BarCodeUtil';
import preView from '../components/preView';
import * as commonServices from "../../services/services";

const dataSource = new ListView.DataSource({
  rowHasChanged: (row1, row2) => row2,
});

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

  // eslint-disable-next-line react/sort-comp
  handleClick = async (name, type, sId) => {
    if (name === 'BtnSave') {
      this.props.onSubmit();
    } else if (name === 'BtnUpd') {
      this.props.onEdit(name);
    } else if (type === 'add') {
      this.props.onDataRowAdd(name);
    } else if (type === 'del') {
      const { app, [`${name}Data`]: tableData } = this.props;
      let { [`${name}DelData`]: tableDelData } = this.props;
      tableDelData = commonUtils.isEmptyArr(tableDelData) ? [] : tableDelData;
      if (sId !== '') {
        const tableDataIndex = tableData.findIndex(item => item.sId === sId);
        if (tableDataIndex > -1) {
          const slaveDataFilter = tableData[tableDataIndex];
          tableData.splice(tableDataIndex, 1);
          slaveDataFilter.handleType = 'del';
          tableDelData.push(slaveDataFilter);
        }
        this.props.onSaveState({
          [`${name}Data`]: tableData,
          [`${name}DelData`]: tableDelData,
          [`${name}SelectedRowKeys`]: [],
        });
      } else {
        Toast.fail(commonFunc.showMessage(app.commonConst, 'pleaseChooseDelData')); // 请选择删除数据
      }
    } else if (type === 'scan' || name === 'BtnScan') {
      BarCodeUtil.startScan(async (type, code) => {
        const {
          slaveConfig, sModelsId, masterData, slaveData, sModelsType, masterConfig,
        } = this.props;
        /* 规则定义
        材料
        sDefineNo: codeArr[0],  --唯一码
        sMaterialsName: codeArr[1], --材料名称
        sMaterialsStyle: codeArr[2], --材料规格
        dAuxiliaryQty: codeArr[3],  --数量
        sWareHouseName: codeArr[4], --仓库名称
        sLocationName: codeArr[5], --名称
        sDefineNo2: codeArr[6], --二维码
        tInStoreDate: codeArr[7], --入库日期
        sMaterialsId: codeArr[8], --材料ID
        sWareHouseId: codeArr[9], --仓库ID
        sLocationId: codeArr[10], --sLocationId
        成品
        sDefineNo: codeArr[0],  --唯一码
        sProductName: codeArr[1], --产品名称
        sProductStyle: codeArr[2], --产品规格
        dProductQty: codeArr[3], --产品数量
        sWarehouseName: codeArr[4], --仓库名称
        sLocationName: codeArr[5], --仓库名称
        sDefineNo2: codeArr[6], --二维码
        tInStoreDate: codeArr[7], --入库日期
        sProductId: codeArr[8], --产品ID
        sWareHouseId: codeArr[9], --仓库ID
        sLocationId: codeArr[10],
        sCustomerName: codeArr[11], --客户名称
        sCustomerShort: codeArr[12],
        sOrderNo: codeArr[13], --订单号
        sWorkOrderNo: codeArr[14], --工单号
        sCustomerOrderNo: codeArr[15], --客户编号
        sCustomerProductNo: codeArr[16], --客户产品编号
        */
        // const slaveData = [];
        let slaveDataObj = {};
        let masterdataObj = {};
        let slaveDataObjAdd = {};
        let masterDataObjAdd = {};
        if (commonUtils.isEmptyObject(code)) {
          Toast.fail('二维码扫码失败！', 1);
          return;
        }
        const codeArr = code.split('|');
        let models = '';
        if (codeArr.length === 12) { /* 材料 */
          models = 'Materials';
        } else if (codeArr.length === 17) { /* 成品 */
          models = 'Product';
        } else {
          models = 'Single'; /* 单一的 */
        }
        if (models === 'Single') { /* sModelsType === 'purchase/purchaseInstore' 扫码入库、扫码领料(唯一码) */
          /* 取第一条码 */
          const sDefineNo = codeArr.length > 0 ? codeArr[0] : '';
          /* 如果条码已存在，提示 */
          // const iIndex = slaveData.findIndex(item => commonUtils.isNotEmptyObject(item.sDefineNo) && item.sDefineNo === sDefineNo);
          // if (iIndex > -1) {
          //   Toast.fail('条码已存在!!!', 1);
          //   return;
          // }
          /* 读取扫码配置 */
          const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.sControlName.indexOf('BtnScan') > -1);
          const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
          const sDefineNoName = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sActiveKey : 'sDefineNo';

          masterdataObj = {
            ...masterData, [sDefineNoName] : sDefineNo
          };
          /* 唯一码调接口 */
          let sqlDataRow ={};
          if(commonUtils.isNotEmptyArr(btnScanConfigs)) {
            const sqlDropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', btnScanConfigs[0], masterdataObj);
            if(commonUtils.isNotEmptyArr(sqlDropDownData)) {
              sqlDataRow = sqlDropDownData[0];
            }
          }
          slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, masterdataObj) }; // 取主表赋值字段
          if(commonUtils.isNotEmptyObject(sqlDataRow)) {
            slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, sqlDataRow) }; // 取接口数据源赋值字段
          }
          slaveDataObj.sParentId = masterdataObj.sId;
          if (commonUtils.isNotEmptyArr(slaveData)) {
            const len = slaveData.length;
            const sDefineNoNew = slaveData[len - 1][sDefineNoName];
            const { handleType } = slaveData[len - 1];
            /* 若slaveData 最后一条数据没有条形码，则赋值条形码；否则新增一条数据 */
            if (commonUtils.isEmptyObject(sDefineNoNew)) {
              slaveDataObj.handleType = commonUtils.isEmpty(handleType) ? 'update' : handleType;
               // slaveData[len - 1] = slaveDataObj;
              slaveData[len - 1] = { ...slaveData[len-1], ...slaveDataObj};
            } else {
              slaveDataObj.sId = commonUtils.createSid();
              slaveDataObj.handleType = 'add';
              slaveData.unshift(slaveDataObj);
            }
          } else {
            slaveDataObj.sId = commonUtils.createSid();
            slaveDataObj.handleType = 'add';
            slaveData.unshift(slaveDataObj);
          }
          if (sModelsType === 'scan/productionmaterials') { /* 只有扫码领料连续扫码 */
            this.handleClick('slave', 'scan'); /* 连续扫码 */
          }
        } else if (models === 'Materials') { /* sModelsType === 'materialsStock/productionmaterials' */
          /* 取第一条码 */
          const sDefineNo = codeArr.length > 0 ? codeArr[0] : '';
          /* 如果条码已存在，提示 */
          // const iIndex = commonUtils.isNotEmptyArr(slaveData) ? slaveData.findIndex(item => commonUtils.isNotEmptyObject(item.sDefineNo) && item.sDefineNo === sDefineNo) : -1;
          // if (iIndex > -1) {
          //   Toast.fail('条码已存在!!!', 1);
          //   return;
          // }
          const dateStr = codeArr.length > 7 && commonUtils.isNotEmptyObject(codeArr[7])? codeArr[7].replace(/-/g, '/') : undefined;
          // const date = new Date(dataStr);
          slaveDataObjAdd = {
            sDefineNo: codeArr.length > 0 ? codeArr[0] : '',
            sMaterialsName: codeArr.length > 1 ? codeArr[1] : '',
            sMaterialsStyle: codeArr.length > 2 ? codeArr[2] : '',
            dAuxiliaryQty: codeArr.length > 3 ? commonUtils.isNull(Number(codeArr[3]), 0) : '',
            sDefineNo2: codeArr.length > 6 ? codeArr[6] : '',
            sMaterialsId: codeArr.length > 8 ? codeArr[8] : '',
            sWareHouseId: codeArr.length > 9 ? codeArr[9] : '',
            sLocationId: codeArr.length > 10 ? codeArr[10] : '',
            dMaterialsQty: codeArr.length > 11 ? commonUtils.isNull(Number(codeArr[11]), 0) : '',
            tInStoreDate: dateStr,
          };
          masterdataObj = {
            ...masterData, ...slaveDataObjAdd,
          };
          /* 读取扫码配置 */
          const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.sControlName.indexOf('BtnScan.slave') > -1);
          // if(commonUtils.isEmptyArr(btnScanConfigs)) {
          //   Toast.fail('请配置扫码按钮!!!', 1);
          //   return;
          // }
          const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
          let sqlDataRow ={};
          if(commonUtils.isNotEmptyArr(btnScanConfigs)) {
            const sqlDropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', btnScanConfigs[0], masterdataObj);
            if(commonUtils.isNotEmptyArr(sqlDropDownData)) {
              sqlDataRow = sqlDropDownData[0];
            }
          }
          if (commonUtils.isNotEmptyArr(slaveData) && slaveData.length > 0) { /* 拿原始从表值 */
            const len = slaveData.length;
            const sDefineNoNew = slaveData[len - 1].sDefineNo;
            if (commonUtils.isEmptyArr(sDefineNoNew)) {
              slaveDataObj = { ...slaveDataObj, ...slaveData[len - 1] };
            }
          }
          slaveDataObj = { ...slaveDataObjAdd, ...commonFunc.getAssignFieldValue(AssignField, masterData) }; // 取赋值字段
          if(commonUtils.isNotEmptyObject(sqlDataRow)) {
            slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, sqlDataRow) }; // 取接口数据源赋值字段
          }

          // slaveDataObj.sDefineNo = sDefineNo;
          slaveDataObj.sId = commonUtils.createSid();
          slaveDataObj.iOrder = 1;
          console.log('slaveDataObj1111', slaveDataObj);
          const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
          const materialsNameConfig = slaveDataConfig.filter(item => item.sName === 'sMaterialsId');
          const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId');
          const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');
          const onDataRowAdd = this.props.onDataRowAdd(name, true);
          const materialseAssignField = commonUtils.isNotEmptyArr(materialsNameConfig) ? materialsNameConfig[0].sAssignField : '';
          const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
          const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';

          let materialsNameDropDownData = [];
          let sWareHouseNamedropDownData = [];
          let sLocationNamedropDownData = [];
          if (commonUtils.isNotEmptyObject(materialsNameConfig[0]) && materialsNameConfig[0].sDropDownType === 'sql') {
            materialsNameDropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', materialsNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
            sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
            sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
          }
          // }
          let materialsNameDataOne;
          let sWareHouseNameDataOne;
          let sLocationNameDataOne;
          if (!commonUtils.isEmpty(materialsNameDropDownData)) {
            if (codeArr.length > 1) {
              materialsNameDataOne = materialsNameDropDownData.filter(item => item.sMaterialsName === codeArr[1]);
            }
          }
          if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
            if (codeArr.length > 4) {
              sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[4]);
            }
          }
          if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
            if (codeArr.length > 5) {
              sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[5]);
            }
          }
          const newCopyTo = {};
          newCopyTo.master = masterdataObj;
          newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
          let materialsDataDetiale = {};
          let sWareHouseNameDataDetiale = {};
          let sLocationNameDataDetiale = {};
          materialsDataDetiale = { ...commonFunc.getAssignFieldValue(materialseAssignField, materialsNameDataOne[0], newCopyTo) };
          sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
          sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
          slaveDataObj = {
            ...slaveDataObj,
            ...onDataRowAdd,
            ...materialsDataDetiale,
            ...sWareHouseNameDataDetiale,
            ...slaveDataObjAdd,
            ...sLocationNameDataDetiale,
          };
          masterdataObj = { ...masterdataObj, ...sLocationNameDataDetiale, ...sWareHouseNameDataDetiale };
        } else if (sModelsType === 'Product') { // 扫码待发货 sales/salesSgoods
          /* 取第一条码 */
          const sDefineNo = codeArr.length > 0 ? codeArr[0] : '';
          /* 如果条码已存在，提示 */
          // const iIndex = commonUtils.isNotEmptyArr(slaveData) ? slaveData.findIndex(item => commonUtils.isNotEmptyObject(item.sDefineNo) && item.sDefineNo === sDefineNo) : -1;
          // if (iIndex > -1) {
          //   Toast.fail('条码已存在!!!', 1);
          //   return;
          // }
          const dateStr = codeArr.length > 7 && commonUtils.isNotEmptyObject(codeArr[7])? codeArr[7].replace(/-/g, '/') : undefined;
          slaveDataObjAdd = {
            sCustomerShort: codeArr.length > 12 ? codeArr[12] : '',
            sOrderNo: codeArr.length > 13 ? codeArr[13] : '',
            sWorkOrderNo: codeArr.length > 14 ? codeArr[14] : '',
            sCustomerOrderNo: codeArr.length > 15 ? codeArr[15] : '',
            sCustomerProductNo: codeArr.length > 13 ? codeArr[16] : '',
            sDefineNo: codeArr.length > 0 ? codeArr[0] : '',
            sDefineNo2: codeArr.length > 6 ? codeArr[6] : '',
            dProductQty: codeArr.length > 3 ? commonUtils.isNull(Number(codeArr[3]), 0): '',
            tInStoreDate: dateStr,
          };
          masterdataObj = {
            ...masterData, ...slaveDataObjAdd,
          };
          // masterDataObjAdd = {
          //   sCustomerShort: codeArr.length > 12 ? codeArr[12] : '', sDefineNo: sDefineNo,
          // };
          /* 读取多条扫码配置 */
          const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.sControlName.indexOf('BtnScan.slave') > -1);
          const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
          if (commonUtils.isNotEmptyArr(slaveData) && slaveData.length > 0) { /* 拿原始从表值 */
            const len = slaveData.length;
            const sDefineNoNew = slaveData[len - 1].sDefineNo;
            if (commonUtils.isEmptyArr(sDefineNoNew)) {
              slaveDataObj = { ...slaveDataObj, ...slaveData[len - 1] };
            }
          }
          /* 唯一码调接口 */
          let sqlDataRow ={};
          if(commonUtils.isNotEmptyArr(btnScanConfigs)) {
            const sqlDropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', btnScanConfigs[0], masterdataObj);
            if(commonUtils.isNotEmptyArr(sqlDropDownData)) {
              sqlDataRow = sqlDropDownData[0];
            }
          }
          slaveDataObj = { ...slaveDataObjAdd, ...commonFunc.getAssignFieldValue(AssignField, masterdataObj) }; // 取赋值字段
          if(commonUtils.isNotEmptyObject(sqlDataRow)) {
            slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, sqlDataRow) }; // 取接口数据源赋值字段
          }
          // slaveDataObj.sDefineNo = sDefineNo;
          slaveDataObj.sId = commonUtils.createSid();
          slaveDataObj.iOrder = 1;
          const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
          const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId'); // 仓库名称
          const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');// 库区库位
          const sProductNameConfig = slaveDataConfig.filter(item => item.sName === 'sProductId');// 产品名称


          const onDataRowAdd = this.props.onDataRowAdd(name, true);
          const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
          const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';
          const sProductNameAssignField = commonUtils.isNotEmptyArr(sProductNameConfig) ? sProductNameConfig[0].sAssignField : '';


          let sWareHouseNamedropDownData = [];
          let sLocationNamedropDownData = [];
          let sProductNamedropDownData = [];
          if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
            sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
            sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
          }
          if (commonUtils.isNotEmptyObject(sProductNameConfig[0]) && sProductNameConfig[0].sDropDownType === 'sql') {
            sProductNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sProductNameConfig[0], slaveData);
          }
          let sWareHouseNameDataOne;
          let sLocationNameDataOne;
          let sProductNameDataOne;
          if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
            if (codeArr.length > 4) {
              sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[4]);
            }
          }
          if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
            if (codeArr.length > 5) {
              sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[5]);
            }
          }
          if (!commonUtils.isEmpty(sProductNamedropDownData)) {
            if (codeArr.length > 1) {
              sProductNameDataOne = sProductNamedropDownData.filter(item => item.sProductName === codeArr[1]);
            }
          }
          const newCopyTo = {};
          newCopyTo.master = masterData;
          newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
          let sWareHouseNameDataDetiale = {};
          let sLocationNameDataDetiale = {};
          let sProductNameDataDetiale = {};
          sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
          sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
          sProductNameDataDetiale = { ...commonFunc.getAssignFieldValue(sProductNameAssignField, sProductNameDataOne[0], newCopyTo) };
          slaveDataObj = {
            ...slaveDataObj,
            ...onDataRowAdd,
            ...sWareHouseNameDataDetiale,
            ...slaveDataObjAdd,
            ...sProductNameDataDetiale,
            ...sLocationNameDataDetiale,
          };
          masterdataObj = {
            ...masterData,
            ...sLocationNameDataDetiale,
            ...masterDataObjAdd,
            ...sWareHouseNameDataDetiale,
          };
        } else {
          Toast.fail('二维码不存在!!!', 1);
        }
        if (models !== 'Single') {
          slaveData.unshift(slaveDataObj);
        }
        this.props.onSaveState({ slaveData, masterData: masterdataObj });
      });
    } else if (type === 'scan1') {
      BarCodeUtil.startScan(async (type, code) => {
        const {
          slaveConfig, sModelsId, masterData, slaveData, sModelsType,
        } = this.props;
        // const slaveData = [];
        let slaveDataObj = {};
        let masterdataObj = {};
        let slaveDataObjAdd = {};
        let masterDataObjAdd = {};
        const codeArr = code.split('|');
        if (sModelsType === 'materialsStock/productionmaterials') {
          const dataStr = codeArr[10].replace(/-/g, '/');
          const date = new Date(dataStr);
          slaveDataObjAdd = {
            sMaterialsStyle: codeArr[1],
            sDefineNo: codeArr[4],
            sDefineNo2: codeArr[5],
            dAuxiliaryQty: codeArr[7],
            tInStoreDate: date,
          };
          const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
          const materialsNameConfig = slaveDataConfig.filter(item => item.sName === 'sMaterialsId');
          const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId');
          const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');
          const onDataRowAdd = this.props.onDataRowAdd(name, true);
          const materialseAssignField = commonUtils.isNotEmptyArr(materialsNameConfig) ? materialsNameConfig[0].sAssignField : '';
          const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
          const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';

          let materialsNameDropDownData = [];
          let sWareHouseNamedropDownData = [];
          let sLocationNamedropDownData = [];
          if (commonUtils.isNotEmptyObject(materialsNameConfig[0]) && materialsNameConfig[0].sDropDownType === 'sql') {
            materialsNameDropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', materialsNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
            sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
            sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
          }
          // }
          let materialsNameDataOne;
          let sWareHouseNameDataOne;
          let sLocationNameDataOne;
          if (!commonUtils.isEmpty(materialsNameDropDownData)) {
            materialsNameDataOne = materialsNameDropDownData.filter(item => item.sMaterialsName === codeArr[6]);
          }
          if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
            sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[2]);
          }
          if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
            sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[3]);
          }
          const newCopyTo = {};
          newCopyTo.master = masterData;
          newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
          let materialsDataDetiale = {};
          let sWareHouseNameDataDetiale = {};
          let sLocationNameDataDetiale = {};
          materialsDataDetiale = { ...commonFunc.getAssignFieldValue(materialseAssignField, materialsNameDataOne[0], newCopyTo) };
          sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
          sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
          slaveDataObj = {
            ...onDataRowAdd,
            ...materialsDataDetiale,
            ...sWareHouseNameDataDetiale,
            ...slaveDataObjAdd,
            ...sLocationNameDataDetiale,
          };
          masterdataObj = { ...masterData, ...sLocationNameDataDetiale, ...sWareHouseNameDataDetiale };
        } else if (sModelsType === 'sales/salesSgoods') { // 扫码待发货
          const dataStr = codeArr[12].replace(/-/g, '/');
          const date = new Date(dataStr);
          slaveDataObjAdd = {
            sOrderNo: codeArr[9],
            sWorkOrderNo: codeArr[8],
            sCustomerOrderNo: codeArr[10],
            sCustomerProductNo: codeArr[11],
            sDefineNo: codeArr[3],
            sDefineNo2: codeArr[4],
            dProductQty: codeArr[6],
            tInStoreDate: date,
          };
          masterDataObjAdd = {
            sCustomerShort: codeArr[7],
          };
          const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
          const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId'); // 仓库名称
          const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');// 库区库位
          const sProductNameConfig = slaveDataConfig.filter(item => item.sName === 'sProductId');// 产品名称


          const onDataRowAdd = this.props.onDataRowAdd(name, true);
          const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
          const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';
          const sProductNameAssignField = commonUtils.isNotEmptyArr(sProductNameConfig) ? sProductNameConfig[0].sAssignField : '';


          let sWareHouseNamedropDownData = [];
          let sLocationNamedropDownData = [];
          let sProductNamedropDownData = [];
          if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
            sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
          }
          if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
            sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
          }
          if (commonUtils.isNotEmptyObject(sProductNameConfig[0]) && sProductNameConfig[0].sDropDownType === 'sql') {
            sProductNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sProductNameConfig[0], slaveData);
          }
          let sWareHouseNameDataOne;
          let sLocationNameDataOne;
          let sProductNameDataOne;
          if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
            sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[1]);
          }
          if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
            sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[2]);
          }
          if (!commonUtils.isEmpty(sProductNamedropDownData)) {
            sProductNameDataOne = sProductNamedropDownData.filter(item => item.sProductName === codeArr[5]);
          }
          const newCopyTo = {};
          newCopyTo.master = masterData;
          newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
          let sWareHouseNameDataDetiale = {};
          let sLocationNameDataDetiale = {};
          let sProductNameDataDetiale = {};
          sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
          sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
          sProductNameDataDetiale = { ...commonFunc.getAssignFieldValue(sProductNameAssignField, sProductNameDataOne[0], newCopyTo) };
          slaveDataObj = {
            ...onDataRowAdd,
            ...sWareHouseNameDataDetiale,
            ...slaveDataObjAdd,
            ...sProductNameDataDetiale,
            ...sLocationNameDataDetiale,
          };
          masterdataObj = {
            ...masterData,
            ...sLocationNameDataDetiale,
            ...masterDataObjAdd,
            ...sWareHouseNameDataDetiale,
          };
        } else if (sModelsType === 'purchase/purchaseInstore' || sModelsType === 'scan/productionmaterials') { /* 扫码入库、扫码领料(唯一码) */
          const { masterConfig } = this.props;
          /* 取第一条码 */
          const sDefineNo = codeArr[0];
          /* 如果条码已存在，提示 */
          const iIndex = slaveData.findIndex(item => commonUtils.isNotEmptyObject(item.sDefineNo) && item.sDefineNo === sDefineNo);
          if (iIndex > -1) {
            Toast.fail('条码已存在!!!', 1);
            return;
          }
          masterdataObj = {
            ...masterData,
          };
          /* 读取扫码配置 */
          const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.sControlName.indexOf('BtnScan') > -1);
          const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
          if (commonUtils.isNotEmptyArr(slaveData) && slaveData.length > 0) { /* 拿原始从表值 */
            const len = slaveData.length;
            const sDefineNoNew = slaveData[len - 1].sDefineNo;
            if (commonUtils.isEmptyArr(sDefineNoNew)) {
              slaveDataObj = { ...slaveDataObj, ...slaveData[len - 1] };
            }
          }
          slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, masterData) }; // 取赋值字段
          slaveDataObj.sDefineNo = sDefineNo;
          slaveDataObj.sId = commonUtils.createSid();
          if (commonUtils.isNotEmptyArr(slaveData)) {
            const len = slaveData.length;
            const sDefineNoNew = slaveData[len - 1].sDefineNo;
            const { handleType } = slaveData[len - 1];
            /* 若slaveData 最后一条数据没有条形码，则赋值条形码；否则新增一条数据 */
            if (commonUtils.isEmptyArr(sDefineNoNew)) {
              slaveDataObj.handleType = commonUtils.isEmpty(handleType) ? 'update' : handleType;
              slaveData[len - 1] = slaveDataObj;
            } else {
              slaveDataObj.handleType = 'add';
              slaveData.unshift(slaveDataObj);
            }
          } else {
            slaveDataObj.handleType = 'add';
            slaveData.unshift(slaveDataObj);
          }
          if (sModelsType === 'scan/productionmaterials') { /* 只有扫码领料连续扫码 */
            this.handleClick('slave', 'scan'); /* 连续扫码 */
          }
        } else {
          Toast.fail('二维码不存在!!!', 1);
        }
        if (sModelsType !== 'purchase/purchaseInstore' && sModelsType !== 'scan/productionmaterials') {
          slaveData.unshift(slaveDataObj);
        }
        this.props.onSaveState({ slaveData, masterData: masterdataObj });
      });
    } else if (commonUtils.isNotEmptyObject(name) && name.includes('BtnScan')) { /* 主表 扫流转码 */
      BarCodeUtil.startScan(async (type, code) => {
        const { masterConfig } = this.props;
        let { masterData } = this.props;
        let masterdataObj = {};
        const codeArr = code.split('|');
        if (commonUtils.isNotEmptyObject(codeArr)) { /* 扫流转码功能 */
          /* 取第一条码 */
          const sDefineNo = codeArr[0];
          /* 如果条码已存在，提示 */
          masterData.sDefineNo = sDefineNo;
          masterdataObj = {
            ...masterData,
          };
          /* 读取扫码配置 */
          const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.bVisible && item.sControlName.indexOf('BtnScan') > -1);
          const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
          masterData = { ...masterdataObj, ...commonFunc.getAssignFieldValue(AssignField, masterData) }; // 取赋值字段
          masterData.handleType = commonUtils.isEmpty(masterData.handleType) ? 'update' : masterData.handleType;
          // this.handleClick('slave', 'scan'); /* 连续扫码 */
        } else {
          Toast.fail('二维码不存在!!!', 1);
        }
        this.props.onSaveState({ masterData });
      });
    } else if (name === 'BtnCamera') { /* 手机调动摄像头拍照 */
      this.handleCamera();
    } else if (name === 'BtnPhoto') { /* 手机相册选择图片 */
      this.handleGetPhoto();
    } else if (name === 'BtnCancel') {
      window.history.back(-1);
    } else if (name === 'BtnExamine') {
      this.props.onAudit(1);
    } else if (name === 'BtnCancelExamine') {
      /* 销审 */
      this.props.onAudit(0);
    } else if (name && name.includes('BtnCalc')) { /* 实现通用计算功能 */
      const { masterConfig, masterData, sModelsId, } = this.props;
      const btnConfig = commonUtils.isNotEmptyArr(masterConfig.gdsconfigformslave.filter(item => (item.sControlName === name))) ?
        masterConfig.gdsconfigformslave.filter(item => (item.sControlName === name))[0] : {};// sButtonEnabled  sButtonParam
      if(commonUtils.isNotEmptyObject(btnConfig)) {
        /* 组装allTableData */
        const allTableMap ={};
        const allReturnMap ={};
        const  slaveNameList = [];
        /* 从props找到 所有的Config */
        if(commonUtils.isNotEmptyArr(this.props)) {
          for (const key of Object.keys(this.props)) {
            if(key.includes('Config') && !key.includes('onGet') && !key.includes('report')) {
              const tablename =  key.replace('Config', '').trim();
              slaveNameList.push(tablename);
            }
          }
        }

        if (commonUtils.isNotEmptyArr(slaveNameList)) {
          slaveNameList.forEach((name, index) => {
            const tableConfig = this.props[name +'Config']; /* 动态配置 */
            const tableData = this.props[name +'Data']; /* 动态配置 */
            const tableSelectedRowKeys  = this.props[name + 'SelectedRowKeys']; /* 选中Key */
            if(commonUtils.isNotEmptyObject(tableConfig)) {
              allTableMap[name +'.'+ tableConfig.sTbName]  = tableData
            }

          })
          const masterTbName = masterConfig.sTbName;
          allTableMap['master.'+ masterTbName] = masterData;
        }
        const sButtonParam = btnConfig.sButtonParam;
        const btn = commonUtils.isJSON(sButtonParam) ?  JSON.parse(sButtonParam) : '';
        const sProName = btn.sproName;
        const url = `${commonConfig.server_host}calcprocedure/calc?sModelsId=${sModelsId}`;
        const value = {
          sProName,
          sButtonParam,
          allTableMap
        };
        const dataReturn = (await commonServices.postValueService(this.props.app.token, value, url)).data;
        if (dataReturn.code === 1) {
          /*   数据操作 数据回传页面   */
          message.success('操作成功！');
          const returnData = dataReturn.dataset.rows[0];
          if (commonUtils.isNotEmptyArr(slaveNameList) && commonUtils.isNotEmptyObject(returnData)) {
            slaveNameList.forEach((name, index) => {
              const tableConfig = this.props[name +'Config']; /* 动态配置 */
              let tableDelData =  this.props[name +'DelData'];
              if(commonUtils.isEmptyArr(tableDelData)) {
                tableDelData = [];
              }
              let tableDelDataNew = []
              let tableData = [];
              if(commonUtils.isNotEmptyObject(tableConfig)) {
                tableData = returnData[name +'.'+ tableConfig.sTbName]; /* 动态配置 */
                if(commonUtils.isNotEmptyArr(tableData) && Array.isArray(tableData)) {
                  tableDelDataNew = tableData.filter(item => item.handleType === 'del');
                  tableData = tableData.filter(item => item.handleType !== 'del');
                  tableDelData = tableDelData.concat(tableDelDataNew);
                }

              }
              allReturnMap[name +'Data'] = tableData;
              allReturnMap[name +'DelData'] = tableDelData;
            });
            const masterTbName = masterConfig.sTbName;
            allReturnMap.masterData = returnData['master.'+ masterTbName];
            allReturnMap.masterData.enabled = true;
          }
          this.props.onSaveState({ ...allReturnMap });
        }else if (dataReturn.code === -8 || dataReturn.code === 2) {
          Modal.info({
            title: '温馨提示:',
            content: (
              <div>
                {this.handleGetMsg(dataReturn.msg)}
              </div>
            ),
            okText: '确认',
            onOk() {},
          });
        } else { /*   失败   */
          this.props.getServiceError(dataReturn);
        }

      }
    }
  }
  handleCamera = () => {
    BarCodeUtil.getImage();
  }
  handleGetPhoto = () => {
    BarCodeUtil.getPhoto();
  }

  handleGetWxCode  = async (successHandler) => {
    let scanResult = '';
    const { app } = this.props;
    // eslint-disable-next-line prefer-destructuring,no-unused-vars
    const userinfo = app.userinfo;
    const myUrl = location.href;
    const configUrl = `${commonConfig.server_host}wechat/getWxToken`;
    const values = {
      url: myUrl,
      sBrandsId: userinfo.sBrandsId,
      sSubsidiaryId: userinfo.sSubsidiaryId,
    };
    // const values = {
    //   url: myUrl,
    //   sBrandsId: '1111111111',
    //   sSubsidiaryId: '1111111111',
    // };
    const configReturn = (await commonServices.postValueService(null, values, configUrl)).data;
    console.log('configReturn::', configReturn, values);
    if (configReturn.code === 1) {
      const { rows } = configReturn.dataset;/* 获取费用分摊单据配置 */
      if (commonUtils.isNotEmptyArr(rows)) {
        const appId = rows[0].appId.toString();
        const nonceStr = rows[0].nonceStr.toString();
        const signature = rows[0].signature.toString();
        const timestamp = rows[0].timestamp.toString();
        weixin.config({
          beta: true, // 必须这么写，否则wx.invoke调用形式的jsapi会有问题
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId, // 必填，公众号的唯一标识
          timestamp, // 必填，生成签名的时间戳    <%= Html.Encode(ViewData["timestamp" ]) %>
          nonceStr, // 必填，生成签名的随机串
          signature, // 必填，签名
          jsApiList: ['checkJsApi', 'scanQRCode'], // 必填，需要使用的JS接口列表, 这里只需要调用扫一扫
        });
        weixin.ready(() => {
          weixin.checkJsApi({ // 判断当前客户端版本是否支持指定JS接口
            jsApiList: ['scanQRCode'],
            success(res) { // 以键值对的形式返回，可用true，不可用false。如：{"checkResult":{"scanQRCode":true},"errMsg":"checkJsApi:ok"}
              console.log(res);
              if (res.checkResult.scanQRCode !== true) {
                Toast.fail(`URL不对，当前URL：${myUrl}请使用正式地址！`);
              }
            },
            // eslint-disable-next-line no-unused-vars
            fail(res) { // 检测getNetworkType该功能失败时处理
              Toast.fail('checkJsApi error');
            },


          }); // wx.ready结束
          weixin.scanQRCode({
            desc: 'scanQRCode desc',
            needResult: 0, // 默认为0，扫描结果由企业微信处理，1则直接返回扫描结果，
            scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
            success(res) {
              // console.log("调用扫描成功",res);
              scanResult = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
              if (commonUtils.isNotEmptyObject(scanResult)) {
                const codeResult = scanResult.split(',');
                if (codeResult.length > 1) {
                  scanResult = codeResult[1];
                }
              }
              successHandler(scanResult);
              // console.log('微信扫码结果：', scanResult);
            },
            error(res) {
              // console.log(res);
              if (res.errMsg.indexOf('function_not_exist') > 0) {
                Toast.fail('版本过低请升级');
              }
            },
          });
        });
      } else {
        Toast.fail(configReturn.msg);
      }
    } else {
      Toast.fail(configReturn.msg);
    }
    return scanResult;
  }

  handleWxScan = async () => {
    this.handleGetWxCode((code) => {
      if (!commonUtils.isEmpty(code)) {
        this.handleWxBusiness(code);
      }
    });
  }

  handleWxBusiness = async (code) => {
    const {
      slaveConfig, sModelsId, masterData, slaveData, sModelsType,
    } = this.props;
    // const slaveData = [];
    let slaveDataObj = {};
    let masterdataObj = {};
    let slaveDataObjAdd = {};
    let masterDataObjAdd = {};
    const codeArr = code.split('|');
    if (sModelsType === 'materialsStock/productionmaterials') {
      const dataStr = codeArr[10].replace(/-/g, '/');
      const date = new Date(dataStr);
      slaveDataObjAdd = {
        sMaterialsStyle: codeArr[1],
        sDefineNo: codeArr[4],
        sDefineNo2: codeArr[5],
        dAuxiliaryQty: codeArr[7],
        tInStoreDate: date,
      };
      const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
      const materialsNameConfig = slaveDataConfig.filter(item => item.sName === 'sMaterialsId');
      const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId');
      const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');
      const onDataRowAdd = this.props.onDataRowAdd(name, true);
      const materialseAssignField = commonUtils.isNotEmptyArr(materialsNameConfig) ? materialsNameConfig[0].sAssignField : '';
      const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
      const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';

      let materialsNameDropDownData = [];
      let sWareHouseNamedropDownData = [];
      let sLocationNamedropDownData = [];
      if (commonUtils.isNotEmptyObject(materialsNameConfig[0]) && materialsNameConfig[0].sDropDownType === 'sql') {
        materialsNameDropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', materialsNameConfig[0], slaveData);
      }
      if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
        sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
      }
      if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
        sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
      }
      // }
      let materialsNameDataOne;
      let sWareHouseNameDataOne;
      let sLocationNameDataOne;
      if (!commonUtils.isEmpty(materialsNameDropDownData)) {
        materialsNameDataOne = materialsNameDropDownData.filter(item => item.sMaterialsName === codeArr[6]);
      }
      if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
        sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[2]);
      }
      if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
        sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[3]);
      }
      const newCopyTo = {};
      newCopyTo.master = masterData;
      newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
      let materialsDataDetiale = {};
      let sWareHouseNameDataDetiale = {};
      let sLocationNameDataDetiale = {};
      materialsDataDetiale = { ...commonFunc.getAssignFieldValue(materialseAssignField, materialsNameDataOne[0], newCopyTo) };
      sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
      sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
      slaveDataObj = {
        ...onDataRowAdd,
        ...materialsDataDetiale,
        ...sWareHouseNameDataDetiale,
        ...slaveDataObjAdd,
        ...sLocationNameDataDetiale,
      };
      masterdataObj = { ...masterData, ...sLocationNameDataDetiale, ...sWareHouseNameDataDetiale };
    } else if (sModelsType === 'sales/salesSgoods') { // 扫码待发货
      const dataStr = codeArr[12].replace(/-/g, '/');
      const date = new Date(dataStr);
      slaveDataObjAdd = {
        sOrderNo: codeArr[9],
        sWorkOrderNo: codeArr[8],
        sCustomerOrderNo: codeArr[10],
        sCustomerProductNo: codeArr[11],
        sDefineNo: codeArr[3],
        sDefineNo2: codeArr[4],
        dProductQty: codeArr[6],
        tInStoreDate: date,
      };
      masterDataObjAdd = {
        sCustomerShort: codeArr[7],
      };
      const slaveDataConfig = commonUtils.isNotEmptyObject(slaveConfig) ? slaveConfig.gdsconfigformslave : '';
      const sWareHouseNameConfig = slaveDataConfig.filter(item => item.sName === 'sWareHouseId'); // 仓库名称
      const sLocationNameConfig = slaveDataConfig.filter(item => item.sName === 'sLocationId');// 库区库位
      const sProductNameConfig = slaveDataConfig.filter(item => item.sName === 'sProductId');// 产品名称


      const onDataRowAdd = this.props.onDataRowAdd(name, true);
      const sWareHouseNameAssignField = commonUtils.isNotEmptyArr(sWareHouseNameConfig) ? sWareHouseNameConfig[0].sAssignField : '';
      const sLocationNameAssignField = commonUtils.isNotEmptyArr(sLocationNameConfig) ? sLocationNameConfig[0].sAssignField : '';
      const sProductNameAssignField = commonUtils.isNotEmptyArr(sProductNameConfig) ? sProductNameConfig[0].sAssignField : '';


      let sWareHouseNamedropDownData = [];
      let sLocationNamedropDownData = [];
      let sProductNamedropDownData = [];
      if (commonUtils.isNotEmptyObject(sWareHouseNameConfig[0]) && sWareHouseNameConfig[0].sDropDownType === 'sql') {
        sWareHouseNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sWareHouseNameConfig[0], slaveData);
      }
      if (commonUtils.isNotEmptyObject(sLocationNameConfig[0]) && sLocationNameConfig[0].sDropDownType === 'sql') {
        sLocationNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'master', sLocationNameConfig[0], masterData);
      }
      if (commonUtils.isNotEmptyObject(sProductNameConfig[0]) && sProductNameConfig[0].sDropDownType === 'sql') {
        sProductNamedropDownData = await this.props.getSqlDropDownData(sModelsId, 'slave', sProductNameConfig[0], slaveData);
      }
      let sWareHouseNameDataOne;
      let sLocationNameDataOne;
      let sProductNameDataOne;
      if (!commonUtils.isEmpty(sWareHouseNamedropDownData)) {
        sWareHouseNameDataOne = sWareHouseNamedropDownData.filter(item => item.sName === codeArr[1]);
      }
      if (!commonUtils.isEmpty(sLocationNamedropDownData)) {
        sLocationNameDataOne = sLocationNamedropDownData.filter(item => item.sName === codeArr[2]);
      }
      if (!commonUtils.isEmpty(sProductNamedropDownData)) {
        sProductNameDataOne = sProductNamedropDownData.filter(item => item.sProductName === codeArr[5]);
      }
      const newCopyTo = {};
      newCopyTo.master = masterData;
      newCopyTo.slave = commonUtils.isEmpty(slaveData) ? [] : slaveData[0];
      let sWareHouseNameDataDetiale = {};
      let sLocationNameDataDetiale = {};
      let sProductNameDataDetiale = {};
      sWareHouseNameDataDetiale = { ...commonFunc.getAssignFieldValue(sWareHouseNameAssignField, sWareHouseNameDataOne[0], newCopyTo) };
      sLocationNameDataDetiale = { ...commonFunc.getAssignFieldValue(sLocationNameAssignField, sLocationNameDataOne[0], newCopyTo) };
      sProductNameDataDetiale = { ...commonFunc.getAssignFieldValue(sProductNameAssignField, sProductNameDataOne[0], newCopyTo) };
      slaveDataObj = {
        ...onDataRowAdd,
        ...sWareHouseNameDataDetiale,
        ...slaveDataObjAdd,
        ...sProductNameDataDetiale,
        ...sLocationNameDataDetiale,
      };
      masterdataObj = {
        ...masterData,
        ...sLocationNameDataDetiale,
        ...masterDataObjAdd,
        ...sWareHouseNameDataDetiale,
      };
    } else if (sModelsType === 'purchase/purchaseInstore' || sModelsType === 'scan/productionmaterials') { /* 扫码入库、扫码领料(唯一码) */
      const { masterConfig } = this.props;
      /* 取第一条码 */
      const sDefineNo = codeArr[0];
      /* 如果条码已存在，提示 */
      const iIndex = slaveData.findIndex(item => commonUtils.isNotEmptyObject(item.sDefineNo) && item.sDefineNo === sDefineNo);
      if (iIndex > -1) {
        Toast.fail('条码已存在!!!', 1);
        return;
      }
      masterdataObj = {
        ...masterData,
      };
      /* 读取扫码配置 */
      const btnScanConfigs = masterConfig.gdsconfigformslave.filter(item => item.sName === '' && item.bVisible && item.sControlName.indexOf('BtnScan') > -1);
      const AssignField = commonUtils.isNotEmptyArr(btnScanConfigs) ? btnScanConfigs[0].sAssignField : '';
      slaveDataObj = { ...slaveDataObj, ...commonFunc.getAssignFieldValue(AssignField, masterData) }; // 取赋值字段
      slaveDataObj.sDefineNo = sDefineNo;
      slaveDataObj.sId = commonUtils.createSid();
      if (commonUtils.isNotEmptyArr(slaveData)) {
        const len = slaveData.length;
        const sDefineNoNew = slaveData[len - 1].sDefineNo;
        const { handleType } = slaveData[len - 1];
        /* 若slaveData 最后一条数据没有条形码，则赋值条形码；否则新增一条数据 */
        if (commonUtils.isEmptyArr(sDefineNoNew)) {
          slaveDataObj.handleType = commonUtils.isEmpty(handleType) ? 'update' : handleType;
          slaveData[len - 1] = slaveDataObj;
        } else {
          slaveDataObj.handleType = 'add';
          slaveData.push(slaveDataObj);
        }
      } else {
        slaveDataObj.handleType = 'add';
        slaveData.push(slaveDataObj);
      }
      this.handleClick('slave', 'scan'); /* 连续扫码 */
    } else {
      Toast.fail('二维码不存在!!!', 1);
    }
    if (sModelsType !== 'purchase/purchaseInstore' && sModelsType !== 'scan/productionmaterials') {
      slaveData.push(slaveDataObj);
    }
    this.props.onSaveState({ slaveData, masterData: masterdataObj });
  }

  getViewValue = (showConfig, record) => {
    let sValue = record === undefined ? undefined : record[showConfig.sName];
    if (showConfig.sDropDownType === 'const') {
      let showDropDown = [];
      if (typeof showConfig.showDropDown === 'object') {
        // eslint-disable-next-line prefer-destructuring
        showDropDown = showConfig.showDropDown;
      } else {
        showDropDown = commonUtils.objectToArr(commonUtils.convertStrToObj(showConfig.showDropDown));
      }
      let iIndex = commonUtils.isEmpty(record[showConfig.sName]) ? -1 : showDropDown.findIndex(item => item.sId === record[showConfig.sName].toString());
      if (iIndex === -1 && record[showConfig.sName] === '') {
        iIndex = showDropDown.findIndex(item => item.sId === '');
      }
      sValue = iIndex > -1 ? showDropDown[iIndex].value : record[showConfig.sName];
    }
    return sValue;
  };

  render() {
    const {
      slaveConfig, slaveData: slaveDataOld, sModelsId, enabled, app, sUseInfo, onReturnForm, masterData, masterConfig,
    } = this.props;
    onReturnForm(this.props.form);
    const slaveDataOld2 = slaveDataOld === undefined ? [] : slaveDataOld;
    const slaveData = dataSource.cloneWithRows(slaveDataOld2);
    const f1 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[0];
    const f2 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[1];
    const f3 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[2];
    const f4 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[3];
    const f5 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[4];
    const f6 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[5];
    const f7 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[6];
    const f8 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[7];
    const f9 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[8];
    const f10 = commonUtils.isEmptyObject(masterConfig) ? {} : masterConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '' && item.iTag === 1)[9];

    const dWidth = document.documentElement.clientWidth || document.body.clientWidth; /* 获取手机视窗宽度 */
    const dHeight = document.documentElement.clientHeight || document.body.clientHeight; /* 获取手机视窗宽度 */
    const formItemLayout = { labelCol: { span: 2 }, wrapperCol: { span: 18 } };
    const style = {
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
    };
    const btnStyle = {
      fontSize: '15px',
      backgroundColor: '#f0f0f0',
      display: 'flex',
      justifyContent: 'center',
      alignItems: 'center',
    };
    const BtnStyle = {
      type: 'default',
      color: 'red',
      border: 'none',
      backgroundColor: '#f0f0f0',
      fontSize: '15px',
    };
    const divStyle = {
      display: 'flex',
      justifyContent: 'space-between',
      background: ' #f0f0f0',
    };
    const row = (rowData, sectionID, rowID) => {
      return (
        <div style={{ backgroundColor: '#f0f0f0' }} key={rowID}>
          <Flex className={styles.ProcessReportMobile}>
            {/* eslint-disable-next-line react/jsx-no-duplicate-props */}
            {
              slaveConfig.gdsconfigformslave.filter(item => item.sControlName === 'BtnChildrenAdd' && item.bVisible).length ?
                <Flex.Item style={style}>
                  <Button type="button" inline disabled={!enabled} onClick={this.handleClick.bind(this, 'slave', 'add')} style={btnStyle}>
                    增加
                    <img src={add} className={styles.iconStyle} style={!enabled ? { opacity: 0.3 } : { opacity: 1 }} alt="add" />
                  </Button>
                </Flex.Item> : ''
            }
            {
              slaveConfig && slaveConfig.gdsconfigformslave.filter(item => item.sControlName === 'BtnChildrenScan' && item.bVisible).length ?
                <Flex.Item style={style}>
                  <Button type="button" inline disabled={!enabled} onClick={this.handleClick.bind(this, 'slave', 'scan')} style={btnStyle}>
                    扫码
                    <img src={scan} className={styles.iconStyle} style={!enabled ? { opacity: 0.3 } : { opacity: 1 }} alt="scan" />
                  </Button>
                </Flex.Item> : ''
            }
            {
              slaveConfig && slaveConfig.gdsconfigformslave.filter(item => item.sControlName === 'BtnChildrenDel' && item.bVisible).length ?
                <Flex.Item style={style}>
                  <Button type="button" inline disabled={!enabled} onClick={this.handleClick.bind(this, 'slave', 'del', rowData.sId)} style={btnStyle}>
                    删除
                    <img src={close} className={styles.iconStyle} style={!enabled ? { opacity: 0.3 } : { opacity: 1 }} alt="close" />
                  </Button>
                </Flex.Item> : ''
            }
          </Flex>
          {commonUtils.isEmptyObject(slaveConfig) ? '' : slaveConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName !== '').map((child) => {
            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 showTypeProps = {
              app,
              masterData,
              record: rowData,
              sId: rowData.sId, /*   修改当前编号(数据格式:字符串)   */
              name: 'slave',
              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.onDataChange,
              showConfig: child,
              formItemLayout: sMemo ? formItemLayout : {},
              textArea: sMemo,
              enabled: enabledNew,
              dataValue: rowData[child.sName],
              bTable: true,
              onFilterDropDownData: this.props.onFilterDropDownData,
              onSaveState: this.props.onSaveState,
            };
            return (
              <ShowType {...showTypeProps} />
            );
          })}
          {slaveConfig.gdsconfigformslave.filter(item => item.bVisible && item.sName.includes('spicture')).map((item) => {
            if (item.sName.includes('spicture') && commonUtils.isNotEmptyObject(rowData.spicture)) {
              const { token } = app;
              let imgBox = '';
              const picAddr = commonUtils.isNotEmptyObject(rowData.spicture) ? rowData.spicture.split(',') : '';
              const previewImageArr = [];
              if (commonUtils.isNotEmptyArr(picAddr)) {
                picAddr.forEach((item) => {
                  const dataPreviewUrl = `${commonConfig.file_host}file/download?savePathStr=${item}&width=${dWidth}&&height=${dHeight}&sModelsId=100&token=${token}`; /* 预览 */
                  previewImageArr.push(dataPreviewUrl);
                });
                const dataUrl = `${commonConfig.file_host}file/download?savePathStr=${picAddr[0]}&width=60&&height=60&sModelsId=100&token=${token}`; /* 缩略图 */
                // eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions
                imgBox = <div className="spictureStyle" key={rowID}> <img src={dataUrl} alt="img" onFocus={() => 0} onClick={e => preView(previewImageArr, e)} /></div>;
              }
              return imgBox;
            } else {
              return '';
            }
          })}
        </div>
      );
    };
    return (
      <div className="commobileBillContainer">
        <div style={{ padding: '5px 0' }} className={styles.onlyWordBg}>
          <Flex style={{ padding: '0 15px 5px 15px' }}>
            {
              f1 ? <Flex.Item className={styles.wordsFlow} style={{ color: '#fff', fontWeight: 'bold', fontSize: '16px' }}>{f1.showName} : {this.getViewValue(f1, masterData)}</Flex.Item> : ''
            }
            {
              f2 ? <Flex.Item className={`${styles.wordsFlow} ${styles.fr}`} style={{ fontSize: '17px' }}>{f2.showName} : {this.getViewValue(f2, masterData)}</Flex.Item> : ''
            }
          </Flex>
          <Flex style={{ padding: '0 15px 5px 15px' }}>
            {
              f3 ? <Flex.Item className={styles.wordsFlow} style={{ fontSize: '17px' }}>{f3.showName} : {this.getViewValue(f3, masterData)}</Flex.Item> : ''
            }
          </Flex>
          <Flex style={{ padding: '0 15px 5px 15px' }}>
            {
              f4 ? <Flex.Item className={styles.wordsFlow} style={{ fontSize: '17px' }}>{f4.showName} : {this.getViewValue(f4, masterData)}</Flex.Item> : ''
            }
          </Flex>
          <Flex style={{ padding: '0 15px 5px 15px' }}>
            {
              f5 ? <Flex.Item className={styles.wordsFlow} style={{ fontSize: '17px' }}>{f5.showName} : {this.getViewValue(f5, masterData)}</Flex.Item> : ''
            }
            {
              f6 ? <Flex.Item className={`${styles.wordsFlow} ${styles.fr}`} style={{ fontSize: '17px' }}>{f6.showName} : {this.getViewValue(f6, masterData)}</Flex.Item> : ''
            }
          </Flex>
          <Flex style={{ padding: '0 15px 5px 15px' }}>
            {
              f7 ? <Flex.Item className={styles.wordsFlow} style={{ fontSize: '17px' }}>{f7.showName} : {this.getViewValue(f7, masterData)}</Flex.Item> : ''
            }
            {
              f8 ? <Flex.Item className={`${styles.wordsFlow} ${styles.fr}`} style={{ fontSize: '17px' }}>{f8.showName} : {this.getViewValue(f8, masterData)}</Flex.Item> : ''
            }
          </Flex>
          <Flex style={{ padding: '0 15px 0px 15px' }}>
            {
              f9 ? <Flex.Item className={styles.wordsFlow} style={{ fontSize: '17px' }}>{f9.showName} : {this.getViewValue(f9, masterData)}</Flex.Item> : ''
            }
            {
              f10 ? <Flex.Item className={`${styles.wordsFlow} ${styles.fr}`} style={{ fontSize: '17px' }}>{f10.showName} : {this.getViewValue(f10, masterData)}</Flex.Item> : ''
            }
          </Flex>
        </div>
        <form style={{ paddingBottom: '47px' }}>
          <div className="flex-container" style={{ color: 'white' }}>
            <CommobileViewMaster {...this.props} iTag={1} />
            <CommobileViewMaster {...this.props} />
          </div>
          {commonUtils.isEmptyArr(slaveDataOld) ?
            <div>
              <Flex className={styles.ProcessReportMobile}>
                {
                  slaveConfig && slaveConfig.gdsconfigformslave.filter(item => item.sControlName === 'BtnChildrenAdd' && item.bVisible).length ?
                    <Flex.Item style={style}>
                      <Button type="button" inline disabled={!enabled} onClick={this.handleClick.bind(this, 'slave', 'add')} style={btnStyle}>
                        增加
                        <img src={add} className={styles.iconStyle} style={!enabled ? { opacity: 0.3 } : { opacity: 1 }} alt="add" />
                      </Button>
                    </Flex.Item> : ''
                }
                {
                  slaveConfig && slaveConfig.gdsconfigformslave.filter(item => item.sControlName === 'BtnChildrenScan' && item.bVisible).length ?
                    <Flex.Item style={style}>
                      <Button type="button" inline disabled={!enabled} onClick={ commonConfig.isWeiXin ? this.handleWxScan.bind(this, 'slave', 'scan') : this.handleClick.bind(this, 'slave', 'scan')} style={btnStyle}>
                        扫码
                        <img src={scan} className={styles.iconStyle} style={!enabled ? { opacity: 0.3 } : { opacity: 1 }} alt="scan" />
                      </Button>
                    </Flex.Item> : ''
                }
              </Flex>
            </div> :
            <div className={styles.listViewStyle}>
              <ListView
                dataSource={slaveData}
                renderRow={row}
                pageSize={commonConfig.pageSize}
                initialListSize={0}
                onScroll={() => {}}
                scrollRenderAheadDistance={500}
                onEndReachedThreshold={10}
                className={styles.listView}
              />
            </div>
            }
          <div
            className={`${styles.ProcessReportMobile} commobileBillToolbar`}
            style={{
 position: 'fixed', bottom: '0', width: '100%', zIndex: 1,
}}
          >
            <CommobileToolBar {...this.props} btnStyle={BtnStyle} divStyle={divStyle} onButtonClick={this.handleClick} />
          </div>
        </form>
      </div>
    );
  }
}

const CommobileBillComponent = createForm()(CommobileBill);

export default CommobileBase(CommobileBillEvent(CommobileBillComponent));

