function toPrice(val) {
  if(!val){
    val = 0;
  }
  return (val.toFixed(2))
  
};

function parsePrice(val){
  if (!val) {
    val = 0;
  }
  return val.toFixed(2).split(".");
}

// 取整
function rounding(val) {
  if (!val) {
    val = 0;
  }
  return parseInt(val);
}

/**
 * <分销员等级>根据index动态添加样式
 */
var indexof = function (index) {
  switch (index) {
    case 1:
      return 'width2';
      break;
    case 2:
      return 'width3';
      break;
    case 3:
      return 'width4';
      break;
    case 4:
      return 'width5';
      break;
  }
};


function array_contain(array, obj) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] == obj)//如果要求数据类型也一致，这里可使用恒等号===
      return true;
  }
  return false;
}

//判断当前的规格值 是否可以选，即其他
function props_contain(allProperties, selectedPropObj, key, item, propKeys){
  var properties = "";
  selectedPropObj[key] = item;
  for (var j = 0; j < propKeys.length; j++){
    properties += propKeys[j] + ":" + selectedPropObj[propKeys[j]] + ";";
  }
  properties = properties.substring(0, properties.length - 1);
  var find = false;
  for (var i = 0; i < allProperties.length; i++){
    if (properties == allProperties[i]){
      find = true;
      break;
    }
  }
  return find;
}

/**
 * 当前属性是否可以选择  
 * 
 * 参数说明：
 * @param allProperties  ['颜色:金色;内存:64GB','颜色:金色;内存:256GB']
 * @param selectedPropObj {'颜色':'金色','内存':'64GB'}
 * @param propKeys ['颜色','内存']
 * @param key 颜色
 * @param item 金色
 * 
 * @return 0 不可选  1 可选  2 可选但跟其他值不匹配
 */
function props_contain2(allProperties, selectedPropObj, key, item, propKeys) {
  var properties = "";
  selectedPropObj[key] = item;
  for (var j = 0; j < propKeys.length; j++) {
    properties += propKeys[j] + ":" + selectedPropObj[propKeys[j]] + ";";
  }
  properties = properties.substring(0, properties.length - 1);
  var find = false;
  for (var i = 0; i < allProperties.length; i++) {
    if (properties == allProperties[i]) {
      find = true;
      return 1;
      break;
    }
  }
  if(!find){
    for (var i = 0; i < allProperties.length; i++) {
      if (allProperties[i].indexOf(item)>=0) {
        return 2;
        break;
      }
    }
  }
  return 0;
}

function parseDiscount(discountRule){
  if (discountRule == 0){
    return '满额减';
  } else if(discountRule == 1){
    return '满件减';
  } else if (discountRule == 2) {
    return '满额折';
  } else if (discountRule == 3) {
    return '满件折';
  }else{
    return '';
  }
}

function parseDiscountMsg(discountRule, needAmount,discount){
  if (discountRule == 0) {
    return '购满' + needAmount + '元减' + discount + '元';
  } else if (discountRule == 1) {
    return '购满' + needAmount + '件减' + discount + '元';
  } else if (discountRule == 2) {
    return '购满' + needAmount + '元打' + discount + '折';
  } else if (discountRule == 3) {
    return '购满' + needAmount + '件打' + discount + '折';
  } else {
    return '';
  }
}

function getCurrDiscountName(discountId, discounts) {
  for (var i = 0; i < discounts.length; i++) {
    if (discounts[i].discountId == discountId) {
      return discounts[i].discountName
    }
  }
  return '不参与促销'
}

/**
 * 裁剪日期  2020-03-20 15:04:40  ->  2020-03-20
 */
function spliceDate(dateStr){
  if (!dateStr) return
  return dateStr.split(' ')[0]
}

// function trim(str) {
//   return str.replace(/^\s+|\s+$/g, '');
// }

module.exports = ({
  spliceDate: spliceDate,
  toPrice: toPrice,
  parsePrice: parsePrice,
  array_contain: array_contain,
  props_contain: props_contain,
  props_contain2: props_contain2,
  parseDiscount: parseDiscount,
  parseDiscountMsg: parseDiscountMsg,
  getCurrDiscountName: getCurrDiscountName,
  indexof: indexof,
  rounding: rounding
});