/* eslint-disable no-plusplus,radix,prefer-destructuring */
/* 用于放公用方法 */
import moment from 'moment';
import lodash from 'lodash';
import { message, notification } from 'antd-v4';
import { createContext } from 'react';

global.storeDropDownData = {};
global.storeStaticDropDownData = {};
// global.systemSettings = [];
// global.commonConst = [];
// global.dNetPrice = 6;
// global.dNetMoney = 6;
// global.sDateFormat = 'YYYY-MM-DD';
/*   日期控件显示格式   */
export const dateFormatA = 'YYYY-MM-DD HH:mm:ss';


/** 获取光标位置 */
export function getCursortPosition(obj) {
  let cursorIndex = 0;
  if (document.selection) {
    obj.focus();
    const range = document.selection.createRange();
    range.moveStart('character', -obj.value.length);
    cursorIndex = range.text.length;
  } else if (obj.selectionStart || obj.selectionStart === 0) {
    cursorIndex = obj.selectionStart;
  }
  return cursorIndex;
}

/* 为输入框聚焦 */
export function focus(id, e, currentRef = document) {
  const element = currentRef.querySelector(`#${id}`);
  if (element && !element.disabled && (element.localName === 'input' || element.localName === 'textarea')) {
    if (isNotEmptyObject(e)) {
      e.preventDefault();
    }
    element.focus();
    element.select();
    return true;
  }
  return false;
}

/**   判断是否是undefined   */
export function isUndefined(value) {
  return lodash.isUndefined(value);
}

/**   判断是否非undefined   */
export function isNotUndefined(value) {
  return !isUndefined(value);
}

/** 判断是否是为undefined或null */
export function isUndefinedNull(value) {
  return value === undefined || value === null;
}

/**   判断是否是空字符串   */
export function isEmptyStr(str) {
  return str === undefined || str === null || str.toString().trim() === '';
}

/**   判断是否是非空字符串   */
export function isNotEmptyStr(str) {
  return !isEmptyStr(str);
}

/**   判断是否是空数字   */
export function isEmptyNumber(num) {
  return num === undefined || num === '';
}

/**   判断是否是非空数字   */
export function isNotEmptyNumber(num) {
  return !isEmptyNumber(num);
}

/**   转换字符串为boolean   */
export function converStrToBoolean(str) {
  if (typeof str === 'boolean') {
    return str;
  }
  if (typeof str === 'string') {
    if (isNotEmptyStr(str) && str.toLocaleLowerCase() === 'true') {
      return true;
    }
  }
  return false;
}

/**   转换数字为boolean   */
export function converNumToBoolean(num) {
  if (typeof num === 'boolean') {
    return num;
  }
  if (typeof num === 'number') {
    if (isNotEmptyNumber(num) && num > 0) {
      return true;
    }
  }
  return false;
}

/**   转换数字字符串为boolean   */
export function converNumStrToBoolean(value) {
  if (typeof value === 'boolean') {
    return value;
  }
  if (typeof value === 'number') {
    if (isNotEmptyNumber(value) && value > 0) {
      return true;
    }
  }
  if (typeof value === 'string') {
    if (isNotEmptyStr(value) && value.toLocaleLowerCase() === 'true') {
      return true;
    }
  }
  return false;
}

/**   转换undefined为空字符串   */
export function strUndefinedToEmpty(str) {
  return isEmptyStr(str) ? '' : str;
}

/**   转换undefined为空对象   */
export function convertUndefinedToEmptyObject(obj) {
  return isEmptyObject(obj) ? {} : obj;
}

/**   转换undefined为空数组   */
export function convertUndefinedToEmptyArr(arr) {
  return isEmptyArr(arr) ? [] : arr;
}

/**   转换字符串为对象   */
export function convertStrToObj(str, defaultObj = {}) {
  let result = defaultObj;
  if (isEmptyStr(str)) {
    result = defaultObj;
  } else {
    try {
      result = JSON.parse(str);
    } catch (e) {
      try {
        result = JSON.parse(str.replace(/\r\n|\n|\r|\t/g, ' '));
      } catch (error) {
        console.log(`${str}JSON报错:`, e.message);
        result = defaultObj;
      }
    }
  }
  return result;
  // return isEmptyStr(str) && !isJSON(str) ? {} : JSON.parse(str);
}

/**   转换对象为字符串   */
export function convertObjToStr(obj) {
  return isEmptyObject(obj) ? '' : JSON.stringify(obj);
}

/**   创建随机数方法(最大值最小值创建)   */
export function getRandomByMinMax(min, max) {
  if (min >= max) {
    return 0;
  }
  return min + Math.round(Math.random() * (max - min));
}

/**   根据位数获取最小值   */
export function getMinByMum(mum) {
  if (mum <= 0 || mum === 1) {
    return 0;
  }
  let str = '1';
  for (let i = 1; i < mum; i++) {
    str += '0';
  }
  return Number.parseInt(str);
}

/**   根据位数获取最大值   */
export function getMaxByMum(mum) {
  let str = '';
  for (let i = 0; i < mum; i++) {
    str += '9';
  }
  return Number.parseInt(str);
}

/**   创建随机数方法(位数创建)   */
export function getRandomByNum(num) {
  return getRandomByMinMax(getMinByMum(num), getMaxByMum(num));
}

/**   创建主表id   */
export function createSid() {
  return Date.parse(new Date()).toString() + getRandomByNum(19);
}

/**   判断是否是空数组  (已废弃，请勿使用)  */
export function isEmptyArr(arr) {
  return arr === undefined || arr === null || arr.length === 0;
}

/**   判断是否是非空数组  (已废弃，请勿使用)  */
export function isNotEmptyArr(arr) {
  return !isEmptyArr(arr);
}

/**   判断是否是空数组   */
export function isEmptyArrNew(arr) {
  return Array.isArray(arr) && arr.length < 1;
}

/**   判断是否是非空数组   */
export function isNotEmptyArrNew(arr) {
  return Array.isArray(arr) && arr.length > 0;
}

/**   判断是否是空对象   */
export function isEmptyObject(obj) {
  return lodash.isEmpty(obj);
}

/**   判断是否是非空对象   */
export function isNotEmptyObject(obj) {
  return !lodash.isEmpty(obj);
}

/**   对象转数组   */
export function objectToArr(obj) {
  const arr = [];
  if (isNotEmptyObject(obj)) {
    for (const key of Object.keys(obj)) {
      const value = obj[key];
      arr.push({ value, sId: key });
    }
  }
  return arr;
}

/**   值是否为空  */
export function isEmpty(obj) {
  return obj === undefined || obj === null || obj === '';
}

/**   数组转对象   */
export function arrToObject(arr) {
  const obj = {};
  if (isNotEmptyArr(arr)) {
    for (const objEach of arr) {
      for (const key of objEach) {
        obj[key] = objEach[key];
      }
    }
  }
  return obj;
}

/**   转换boolean为是否显示   */
export function convertBooleanToDisplayBlock(ret) {
  return ret ? 'inline-block' : 'none';
}

/**   转换boolean为是否显示   */
export function convertBooleanToDisplay(ret) {
  return ret ? '' : 'none';
}

/**   转换字符串为数字   */
export function convertStrToNumber(str) {
  if (isNotEmptyStr(str) && typeof str === 'string') {
    const num = Number(str);
    return num;
  }
  return str;
}

/**   转换字符串为数字0   */
export function convertStrToNumber0(str) {
  if (typeof str === 'number') {
    return str;
  }
  if (isNotEmptyStr(str) && !isNaN(str)) {
    return Number(str);
  }
  return 0;
}

/**   转换字符串为数字undefined   */
export function convertStrToNumberUndefined(str) {
  if (typeof str === 'number') {
    return str;
  }
  if (isNotEmptyStr(str)) {
    return Number(str);
  }
  return undefined;
}

/**   转换数字为字符串   */
export function convertNumberToStr(num) {
  if (typeof num === 'string') {
    return num;
  }
  if (isNotEmptyNumber(num)) {
    return num.toString();
  }
  return '';
}

/**   转换日期控件格式   */
export function convertObjToMoment(obj, dateFormat) {
  if (isNotUndefined(obj) && obj !== null) {
    return moment(obj, dateFormat);
  }
  return null;
}

/**   转换日期控件格式(默认日期)   */
export function convertObjToMomentDefault(obj, dateFormat) {
  if (isNotUndefined(obj) && obj !== null) {
    return moment(obj, dateFormat);
  }
  return moment().format(dateFormat);
}

/** 是否是数字 */
export function isNum(value) {
  if (value === '') return false;
  value = convertStrToNumberUndefined(value);
  if (isNaN(value)) return false;
  return typeof value === 'number';
}

/** 是否不是数字 */
export function isNotNum(value) {
  return !isNum(value);
}

/**   创建新Tab标签Id   */
export function createNewTabId() {
  return new Date().getTime().toString();
}

/** 去除小数末尾0 */
export function parseFloatNum(value) {
  return parseFloat(value);
}

/** 去除小数末尾0 */
export function convertToNum(value) {
  return isNum(parseFloatNum(value)) ? parseFloatNum(value) : 0;
}

/** 转换成相应小数位的数字 */
export function convertFixNum(value, num) {
  const iFix = '10000000000'.substring(0, num + 1);
  return convertStrToNumber(Math.round(value * convertStrToNumber(iFix)) / convertStrToNumber(iFix));
}

/**   过滤非数字(n位小数)   */
export function filterDisNumberN(value, n) {
  if (typeof value === 'string' && isEmptyStr(value)) {
    return value;
  }
  let str = convertNumberToStr(value).replace(/[^\d.\d]/g, '');
  str += str.indexOf('.') < 0 ? '.000000000000000000' : '000000000000000000000';
  return convertStrToNumber(str).toFixed(n);
}

/** 不为数字转为1,为数字的去除小数末尾0 */
export function convertIsNotNumToNumber1(value) {
  if (isNum(value)) {
    return parseFloat(value) === 0 ? 1 : value;
  }
  return 1;
}

/** 空转0 */
export function isNull(value, defaultValue) {
  return isEmpty(value) ? defaultValue : value;
}

/** 空转0 */
export function nullIf(value, defaultValue) {
  return value === defaultValue ? undefined : value;
}

/**   对象循环赋值返回新对象{key: key}   */
export function returnObjValue(sAssignField) {
  const obj = {};
  const arrayObj = sAssignField.split(',');
  for (const child of arrayObj) {
    const childArray = child.split(':');
    obj[childArray[0]] = childArray[1];
  }
  return obj;
}

/**   对象是否存在某值   */
export function getObjKeyByValue(obj, value) {
  if (isNotEmptyObject(obj) && isNotEmptyStr(value)) {
    for (const key of Object.keys(obj)) {
      if (obj[key] === value) {
        return key;
      }
    }
  }
  return '';
}

export function getStoreStaticDropDownData(formId, fieldName) {
  if (global.storeStaticDropDownData[formId + fieldName]) {
    return global.storeStaticDropDownData[formId + fieldName];
  }

  const configData = global.storeDropDownData[formId] || {};
  const { formData } = configData;
  let staticDropDownData = {};
  if (formData && formData[0]) {
    const { gdsconfigformslave = [] } = formData[0];
    const iIndex = gdsconfigformslave.findIndex(item => item.sName === fieldName);
    if (iIndex !== -1) {
      let showDropDown = gdsconfigformslave[iIndex].showDropDown;
      if (showDropDown) {
        try {
          showDropDown = JSON.parse(showDropDown);
        } catch (error) {
          showDropDown = null;
        }
        if (showDropDown) {
          staticDropDownData = showDropDown;
        }
      }
    }
  }
  global.storeStaticDropDownData[formId + fieldName] = staticDropDownData;
  return staticDropDownData;
}

/**   获取存储下拉数据   */
export function getStoreDropDownData(formId, name, fieldName) {
  const allName = (fieldName === 'sCustomerId' || fieldName === 'sCustomerNo' || fieldName === 'sCustomerName' ||
  fieldName === 'sMaterialsId' || fieldName === 'sMaterialsNo' || fieldName === 'sMaterialsName' ||
  fieldName === 'sProcessId' || fieldName === 'sProcessNo' || fieldName === 'sProcessName' ||
  fieldName === 'sProductId' || fieldName === 'sProductNo' || fieldName === 'sProductName') ?
    fieldName : formId + name + fieldName;
  const dropDownData = global.storeDropDownData[allName];
  return dropDownData;
}

/**   存储下拉数据   */
export function setStoreDropDownData(formId, name, fieldName, dropDownData) {
  const allName = (fieldName === 'sCustomerId' || fieldName === 'sCustomerNo' || fieldName === 'sCustomerName' ||
  fieldName === 'sMaterialsId' || fieldName === 'sMaterialsNo' || fieldName === 'sMaterialsName' ||
  fieldName === 'sProcessId' || fieldName === 'sProcessNo' || fieldName === 'sProcessName' ||
  fieldName === 'sProductId' || fieldName === 'sProductNo' || fieldName === 'sProductName') ?
    fieldName : formId + name + fieldName;
  global.storeDropDownData[allName] = dropDownData;
  return true;
}

/**   存储下拉数据   */
export function clearStoreDropDownData() {
  global.storeDropDownData = {};
  return true;
}

/**   存储下拉数据   */
export function clearFormStoreDropDownData(formId) {
  for (const key of Object.keys(global.storeDropDownData)) {
    if (key.indexOf(formId) > -1) {
      global.storeDropDownData[key] = [];
    }
  }
  return true;
}

message.config({
  top: 20,
  duration: 3,
  maxCount: 5,
});

notification.config({
  placement: 'bottomRight',
  bottom: 50,
  duration: null,
  rtl: false,
});


/**   获取存储下拉数据   */
export function getNewToken(token) {
  if (token !== null && token !== undefined) {
    const newToken = token.replace(/\//g, '+').replace(/&/g, '==').replace(/ /g, 'E');
    return newToken;
  }
}

/**   判断是否为json字符串   */
export function isJSON(str) {
  if (typeof str === 'string') {
    try {
      const obj = JSON.parse(str);
      if (typeof obj === 'object' && obj) {
        return true;
      } else {
        return false;
      }
    } catch (e) {
      return false;
    }
  }
}

/**   数组生成tree   */
export function genTreeByArr(arr, idName, pidName) {
  const tempArr = JSON.parse(JSON.stringify(arr));
  const formatObj = tempArr.reduce((pre, cur) => {
    return { ...pre, [cur[idName]]: cur };
  }, {});
  const formatArray = tempArr.reduce((pre, cur) => {
    const pid = cur[pidName] !== '' ? cur[pidName] : '';
    const parent = pid ? formatObj[pid] : false;
    if (parent) {
      if (parent.children) {
        parent.children.push(cur);
      } else {
        parent.children = [cur];
      }
    } else {
      pre.push(cur);
    }
    return pre;
  }, []);
  return formatArray;
}

export function genTreeDataByArr(arr, idName, pidName) {
  /*  idName:sId, pidName: sFatherSlaveId */
  const sParentIdMap = {};
  if (isNotEmptyArr(arr)) {
    arr.forEach((item) => {
      const sOriginalId = item.sOriginalId;
      if (isNotEmptyObject(sOriginalId)) {
        sParentIdMap[sOriginalId] = item[idName];
      }
    });
    console.log('sParentIdMap', sParentIdMap);
    arr.forEach((item) => {
      const sFatherOldId = item[pidName];
      const sFatherSlaveIdNew = sParentIdMap[sFatherOldId];
      // const sFatherSlaveIdNew = sParentIdMap.get(item[pidName]);
      if (isEmptyObject(sFatherSlaveIdNew)) {
        item[pidName] = '';
      } else {
        item[pidName] = sFatherSlaveIdNew;
      }
    });
  }
  return arr;
}

/**   根据条件过滤出来数组生成tree   */
export function genTreeByFilterArr(arr, idName, pidName, selfKey, filterValue) {
  let tempArr = JSON.parse(JSON.stringify(arr));
  if (isNotEmptyObject(filterValue)) {
    tempArr = tempArr.filter(item => item[selfKey] === filterValue);
  }
  const formatObj = tempArr.reduce((pre, cur) => {
    return { ...pre, [cur[idName]]: cur };
  }, {});
  const formatArray = tempArr.reduce((pre, cur) => {
    const pid = cur[pidName] !== '' ? cur[pidName] : '';
    const parent = pid ? formatObj[pid] : false;
    if (parent) {
      if (parent.children) {
        parent.children.push(cur);
      } else {
        parent.children = [cur];
      }
    } else {
      pre.push(cur);
    }
    return pre;
  }, []);
  return formatArray;
}

/* 解决订单新增多个主产品时 数据层级错乱问题 处理方式为第一个层级形成后 第二个树形打造时 不处理已形成的树形数据 */
export function genTreeDataByArrSales(arr, idName, pidName) {
  /*  idName:sId, pidName: sFatherSlaveId */
  const sParentIdMap = {};
  if (isNotEmptyArr(arr)) {
    arr.forEach((item) => {
      const sOriginalId = item.sOriginalId;
      if (isNotEmptyObject(sOriginalId) && !item.bTreeFinish) {
        sParentIdMap[sOriginalId] = item[idName];
      }
    });
    arr.forEach((item) => {
      if (!item.bTreeFinish) {
        const sFatherOldId = item[pidName];
        const sFatherSlaveIdNew = sParentIdMap[sFatherOldId];
        // const sFatherSlaveIdNew = sParentIdMap.get(item[pidName]);
        if (isEmptyObject(sFatherSlaveIdNew)) {
          item[pidName] = '';
        } else {
          item[pidName] = sFatherSlaveIdNew;
        }
        item.bTreeFinish = true;
      }
    });
  }
  return arr;
}
/* 将部件、材料、工序的sSlaveId 均换成最新的 */
export function genSlaveNewId(arr, tableName, idName, oldName, baseObj) {
  /*  controlData, key, idName: 'sSlaveId', oldName:'sOriginalId', slaveRow */
  if (isNotEmptyArr(arr) && tableName !== 'slave') {
    const filterData = arr.filter(item => item[oldName] === baseObj[oldName]);
    if (isNotEmptyArr(filterData)) {
      filterData.forEach((item) => {
        item.sSlaveId = baseObj.sId;
        const index = arr.findIndex(child => child.sId === item.sId);
        if (index > -1) {
          arr[index] = item;
        }
      });
    }
  }
  return arr;
}


/**
 * 从props中提取带DataOld后缀的字段，组装成以表名为key的原始数据对象
 * @param {Object} props - 组件的props对象
 * @returns {Object} - 组装后的sOldData，格式：{ 表名: 对应的数据数组 }
 */
export function getOldDataFromProps(props) {
  // 初始化返回的空对象
  const sOldData = {};

  // 2. 遍历props的所有key
  for (const key of Object.keys(props)) {
    if (key.includes('DataOld')) {
      // 提取去掉DataOld后的表名前缀
      const tablename = key.replace('DataOld', '').trim();

      // 获取对应的配置对象（tablename + Config）
      const tableConfig = props[tablename + 'Config'];
      // 获取对应的原始数据（tablename + DataOld），默认空数组
      const tableData = props[tablename + 'DataOld'] || [];

      // 3. 校验配置对象是否有效，避免无配置时赋值
      if (isNotEmptyObject(tableConfig) && tableConfig.sTbName) {
        // 以配置中的真实表名为key，存入数据
        const sTbName = tableConfig.sTbName;
        sOldData[sTbName] = tableData;
      }
    }
  }

  return sOldData;
}


export const myContext = createContext(null);

export function reducer(state, action) {
  const [type, payload] = action;
  switch (type) {
    case 'saveState':
      return {
        ...state,
        ...payload,
      };
    default:
      return {
        ...state,
        ...payload,
      };
  }
}
// 将数组根据条件分组
export function groupBy(array, key) {
  return array.reduce((result, currentItem) => {
    // 使用 key 函数提取分组键
    const groupKey = key(currentItem);
    // 确保 result 对象中有对应分组的数组
    if (!result[groupKey]) {
      result[groupKey] = [];
    }

    // 将当前项添加到对应分组的数组中
    result[groupKey].push(currentItem);
    // console.log('result', result);
    return result;
  }, {});
}

/* 配置支持日期格式化, 接收日期，格式2个参数，返回格式化后的日期 */
export function getDate(date, dateFormat) {
  const result = moment(date).format(dateFormat);
  return result;
}

/* 根据表名返回选中行对象 */
export function getSelectedData(props, tableName) {
  const selectedRowKeys = props[`${tableName}SelectedRowKeys`] || [];
  let selectedDataIndex = -1;
  const tableData = props[`${tableName}Data`];
  if (isNotEmptyArr(tableData)) {
    if (isNotEmptyArr(selectedRowKeys)) {
      selectedDataIndex = tableData.findIndex(item => item.sId === selectedRowKeys[0]);
    } else {
      selectedDataIndex = 0;
    }
  }

  if (selectedDataIndex > -1) {
    return props[`${tableName}Data`][selectedDataIndex];
  } else {
    return {};
  }
}

/* 根据传入的类型 生成批次号的通用方法 */
export function getDefineNo(app, sModelsType, tableName, masterData, tableRow) {
  const models = tableName === 'materials' ? 'Materials' :
    sModelsType.includes('sales/') || sModelsType.includes('manufacture/') || sModelsType.includes('quotation/') || sModelsType.includes('productStock/') ? 'Product' : 'Materials';
  /* 只要tableRow中有sDefineNo字段 */
  const bHasDefineNo = 'sDefineNo' in tableRow;
  if (bHasDefineNo) {
    if (models === 'Materials') {
      let iIndex = app.systemData.findIndex(item => item.sName === 'CbxMaterialsDefine');
      if (isNotEmptyObject(masterData) && (isNotEmptyObject(masterData?.sMinusSrcId) || isNotEmptyObject(masterData?.sMinusUsed))) {
        iIndex = -1;
      }
      if (iIndex > -1) {
        const sCbxMaterialsDefine = app.systemData[iIndex].sValue;
        if (sCbxMaterialsDefine === 'datenum') {
          tableRow.sDefineNo = moment(new Date()).format('YYYYMMDD');
        } else if (sCbxMaterialsDefine === 'datetimenum') {
          tableRow.sDefineNo = moment(new Date()).format('YYYYMMDDHHmm');
        } else if (sCbxMaterialsDefine === 'yearnum') {
          tableRow.sDefineNo = moment(new Date()).format('YYYY');
        } else if (sCbxMaterialsDefine === 'yearmonthnum') {
          tableRow.sDefineNo = moment(new Date()).format('YYYYMM');
        } else if (sCbxMaterialsDefine === 'worknum') {
          tableRow.sDefineNo = tableRow.sWorkOrderNo;
        } else if (sCbxMaterialsDefine === 'manuualno') {
          tableRow.sDefineNo = tableRow.sManualNo;
        } else if (sCbxMaterialsDefine === 'createnownum') { /* 批号根据生产日期+当前日期 */
          tableRow.sDefineNo = moment(tableRow.tProductionDate).format('YYYYMMDDHHmm') + moment(new Date()).format('YYYYMMDDHHmm');
        }
      }
    } else if (sModelsType === 'productStock/productInStore' || sModelsType === 'outside/outsideinstoreAll') { /* 成品入库、整单发外入库 */
      if (bHasDefineNo) {
        let iIndex = app.systemData.findIndex(item => item.sName === 'CbxProductDefine');
        if (isNotEmptyObject(masterData.sMinusSrcId) || isNotEmptyObject(masterData.sMinusUsed)) {
          iIndex = -1;
        }
        if (iIndex > -1) {
          const sCbxProductDefine = app.systemData[iIndex].sValue;
          if (sCbxProductDefine === 'datenum') {
            tableRow.sDefineNo = moment(new Date()).format('YYYYMMDD');
          } else if (sCbxProductDefine === 'datetimenum') {
            tableRow.sDefineNo = moment(new Date()).format('YYYYMMDDHHmm');
          } else if (sCbxProductDefine === 'yearnum') {
            tableRow.sDefineNo = moment(new Date()).format('YYYY');
          } else if (sCbxProductDefine === 'yearmonthnum') {
            tableRow.sDefineNo = moment(new Date()).format('YYYYMM');
          } else if (sCbxProductDefine === 'worknum') {
            tableRow.sDefineNo = tableRow.sWorkOrderNo;
          } else if (sCbxProductDefine === 'manuualno') {
            tableRow.sDefineNo = tableRow.sManualNo;
          } else if (sCbxProductDefine === 'worktimenum') { /* 批号根据工单号码+日期时间批号 */
            tableRow.sDefineNo = tableRow.sWorkOrderNo + moment(new Date()).format('YYYYMMDDHH');
          } else if (sCbxProductDefine === 'yearToDate') { /* 2259 批号根据工单制单日期年月日，年取两位 */
            tableRow.sDefineNo = moment(masterData.tCreateDate || new Date()).format('YYMMDD');
          }
        }
      }
    }
  }

  return tableRow;
}


// 获取选中数据index
export function getSelectedDataIndex(props, tableName) {
  const selectedRowKeys = props[`${tableName}SelectedRowKeys`] || [];

  let selectedDataIndex = -1;
  const tableData = props[`${tableName}Data`];
  if (isNotEmptyArr(tableData)) {
    if (isNotEmptyArr(selectedRowKeys)) {
      selectedDataIndex = tableData.findIndex(item => item.sId === selectedRowKeys[0]);
    } else {
      selectedDataIndex = 0;
    }
  }
  return selectedDataIndex;
}
