/**
 * 小数位数
 */
var numberOfDecimalPlacesAmount = 0;

/**
 *  小数位数
 * @param num 
 */
var setNumberOfDecimalPlacesAmount = function (num) {
  numberOfDecimalPlacesAmount = num;
  return "ok";
}

/**
 * 截取字符串
 * @param str 
 * @param start 
 * @param end 
 */
var subStr = function (str, start, end) {
  return str.substring(start, end)
}

/**
 * 转成字符串
 * @param number 
 */
var toLocaleString = function (number) {
  number = number.toFixed(numberOfDecimalPlacesAmount);
  var arr = number.split('.')
  return parseInt(arr[0]).toLocaleString() + '.' + arr[1]
}

/**
 * 格式化日期
 * @param time 
 */
var format = function (time) {
  if (!time || time == '') {
    return ''
  }
  var date = getDate(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);

}

var formatDateTime = function (time) {
  if (!time || time == '') {
    return ''
  }
  var date = getDate(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  // var second = date.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hour + ':' + minute
}


var formatDate = function (time) {
  if (!time || time == '') {
    return ''
  }
  var date = getDate(time);
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  return year + '-' + month + '-' + day 
}

/**
 * json
 * @param str 
 */
var parse = function (str) {
  if (str == '') return str;
  return JSON.parse(str);
}

/**
 * 截取日期
 * @param str 
 * @param start 
 * @param end 
 */
var dateFormat = function (str, start, end) {
  if (!str) return ''
  return str.substring(start, end);
}

/**
 * 获取类型
 * @param item 
 */
var getType = function (item) {
  if (item.BOMID && item.BOMID != 0) {
    return "blue"
  } else if (item.CustomFlag == 1) {
    return ""
  } else {
    return ""
  }
}

/**
 * 价格和金额
 * @param num 
 */
var jsCalcFix2 = function (num) {
  return parseFloat(num + "").toFixed(numberOfDecimalPlacesAmount)
}
/**
 * 判断str中是否含有val
 * @param str 
 * @param val 
 */
var defineIndexOf = function (str, val) {
  if (str != null) {
    return str.indexOf(val);
  }
}

var addressFullIsUndefined = function (addressFull) {
  if (addressFull && (addressFull.indexOf('undefined') != -1)) {
    return addressFull.replace("undefined", "") ? addressFull.replace("undefined", "") : ''
  } else {
    return addressFull ? addressFull : ''
  }
}
//地址出现undefined null NaN(目前只能替换一个 每种)

function addressToIndexOf(str) {
  // console.log('地址出现undefined',typeof str)
  if (typeof str == 'string') {
    if (str != null && str != '') {
      return str = str.replace('undefined', '').replace('null', '').replace('NaN', '') ? str.replace('undefined', '').replace('null', '').replace('NaN', '') : ''
    }
  }
}


/**
 * @desc   : 设置值
 * @author : 周兴
 * @date   : 2024/1/26 11:46
 */
function setItemValue(item, colName) {
  var value = ''
  if (typeof colName == 'object') {
    var count = colName.length;
    for (i = 0; i < count; i++) {
      value += colName[i].title + (item[colName[i].name] ? item[colName[i].name] : '')
        + (item[colName[i].name] ? ' | ' : '');
    }
    if (value) {
      value = value.substring(0, value.length - 3);
    }
  } else {
    value = item[colName]!=null ? item[colName] : '';
  } 
  return value;
}

/**
 * @desc   : 设置底部值
 * @author : 周兴
 * @date   : 2024/1/26 11:46
 */
function setFooterItemValue(item, colName) {
  var value = ''
  if (typeof colName == 'object') {
    var count = colName.length;
    for (i = 0; i < count; i++) {
      // 如果前缀有值就显示
      if (colName[i].prefix) {
        //控制参数为0 等于自定义参数
        if (colName[i].different && item[colName[i].name] == 0) {
          value += colName[i].different
        } else {
          value += colName[i].prefix + ' ' + (item[colName[i].name] ? item[colName[i].name] : '') + ' ' + colName[i].title;
        }

      } else {
        value += (item[colName[i].name] ? item[colName[i].name] : '') + colName[i].title;
      }
    }
  } else {
    value = item[colName] ? item[colName]: '';
  }

  return value.indexOf('NaN') < 0 ? value : '';
}

/**
 * @desc   : 设置查询提示值
 * @author : 周兴
 * @date   : 2024/1/26 11:46
 */
function setSearchPlaceholder(lang, cols) {
  var text = '';
  if (typeof cols == 'object') {
    for (i = 0; i < cols.length; i++) {
      text += lang[cols[i]] + '/';
    }
    text = text.substring(0, text.length - 1);
  } else {
    text = lang[cols];
  }
  return lang['search'] + ' ' + text;
}

/**
 * @desc   : 设置提示信息
 * @author : 周兴
 * @date   : 2024/1/26 11:46
 */
function setPlaceholder(lang, name, searchFlag = false) {
  var text;
  if (lang[name]) {
    text = lang[name];
  } else {
    text = name;
  }
  if (searchFlag) {
    text = lang['searchWords'] + text;
  } else {
    text = lang['inputWords'] + text;
  }
  return text;
}

/**
 * @desc : 前台实现千分位
 * @date : 2024/2/18 16:49
 * @author : 周兴
 */
function toThousandCents(num) {
  if (num == undefined) {
    return 0;
  }
  var num = num + '';
  var d = '';
  if (num.slice(0, 1) == '-') {
    d = num.slice(0, 1);
    num = num.slice(1);
  }
  var len = num.length;
  var index = num.indexOf('.');
  if (index == -1) {
    num = num + '.00';
  } else if ((index + 2) == len) {
    num = num + '0';
  }
  var index = num.indexOf('.'); // 字符出现的位置
  var num2 = num.slice(-3);
  num = num.slice(0, index)
  var result = '';
  while (num.length > 3) {
    result = ',' + num.slice(-3) + result;
    num = num.slice(0, num.length - 3);
  }
  if (num) {
    result = num + result;
  }
  return d + (result + num2)
}

/**
 * @desc : 向上取整
 * @date : 2024/2/18 16:49
 * @author : 周兴
 */
function ceiling(val) {
  var value = parseInt(val);
  if (val > value) {
    value = value + 1
  }
  return value
}


/**
 * 暴露接口调用
 */
module.exports = {
  ceiling: ceiling,
  toThousandCents: toThousandCents,
  setItemValue: setItemValue,
  setFooterItemValue: setFooterItemValue,
  setSearchPlaceholder: setSearchPlaceholder,
  setPlaceholder: setPlaceholder,
  addressToIndexOf: addressToIndexOf,
  addressFullIsUndefined: addressFullIsUndefined,
  setNumberOfDecimalPlacesAmount: setNumberOfDecimalPlacesAmount,
  toLocaleString: toLocaleString,
  subStr: subStr,
  format: format,
  parse: parse,
  dateFormat: dateFormat,
  getType: getType,
  jsCalcFix2: jsCalcFix2,
  defineIndexOf: defineIndexOf,
  formatDateTime: formatDateTime,
  formatDate:formatDate
} 