import React from "react";
import { routerRedux } from "dva/router";
import { getDefaultCurrency } from '@/utils/request';
const defaultScale = 2;
/**
 *
 * @param {*} val 版本号
 * @param {*} type 类型：card, list, text 默认为list
 * @param {*} right 右偏移(百分比)
 */
export const goodsVersion = (val, type, right) => {
  if (!val) {
    val = 1;
  }
  const style = {
    color: "#999"
  };
  if (type == "card") {
    style.marginLeft = "10px";
  } else if (type != "text") {
    style.float = "right";
    style.marginRight = right ? right : "10%";
  }
  return (
    <span style={style}>
      {`V${val}.0`}
    </span>
  );
};
/**
 * 判断数组aa是否包含数组bb
 * @param {*} aa
 * @param {*} bb
 */
export const isContainer = (aa, bb) => {
  if (!Array.isArray(aa) || !Array.isArray(bb) || aa.length < bb.length) return false;
  const aaStr = aa.toString();
  return bb.every((item) => {
    return aaStr.indexOf(item) >= 0;
  });
};
/**
 * 跳转商品明细
 * @param {*} dispatch
 * @param {*} data
 * @param {*} e
 */
export const toGoodsDetailHandle = (props, data, e) => {
  e.preventDefault();
  const { dispatch } = props;
  dispatch(
    routerRedux.push({
      pathname: "/buyer/goods/GoodsDetail",
      query: {
        code: data.code,
        id: data.id,
        saleOrgId: data.saleOrgId ? data.saleOrgId : data.supplierId,
        saleOrgName: data.saleOrgId ? data.saleOrgName : data.supplierName,
        productId: data.productId,
        stock: data.stock
      }
    })
  );
  localStorage.setItem("goodsSku", data.code);
  localStorage.setItem("goodsDetailsId", data.id);  // 商品Id
  localStorage.setItem("goodsDetailSsaleOrgId", data.saleOrgId ? data.saleOrgId : data.supplierId);  // 供应商
  localStorage.setItem("goodsDetailSsaleOrgName", data.saleOrgId ? data.saleOrgName : data.supplierName);  // 供应商
  localStorage.setItem("goodsDetailProductId", data.productId);  // 产品id
  localStorage.setItem("goodsDetailStock", data.stock);  // 产品id
};
// 订单金额/价格精度化
/**
 * 优先使用订单本身币符及精度
 * ...args
 * args[0]: 金额/单价
 * args[1]: 金额格式   price：单价格式  amount： 金额格式
 * args[2]: 币符
 * args[3]: 精度
 */
export function amountPrecision (...args) {
  const amt = args[0] ? args[0] : 0;
  let currencySign = getDefaultCurrency().currencySign;
  let scale = args[1] == 'price' ? getDefaultCurrency().currencyPriceScale : getDefaultCurrency().currencyAmountScale;
  if (args[2]) {
    currencySign = args[2] || "￥";
  }
  if (args[3]) {
    scale = args[3] || 2;
  }
  const newAmount = currencySign + amt.toFixed(scale);
  return newAmount;
}

// 重量/体积精度化
/**
 * 优先使用订单本身单位精度
 * ...args
 * args[0]: 重量/体积
 * args[1]: 精度
 * args[2]: 单位名称
 */
export function unitPrecision (...args) {
  const unitVal = args[0] ? args[0] : 0;
  let unitScale = defaultScale;
  if (args[1]) {
    unitScale = args[1];
  }
  let unitName = "";
  if (args[2]) {
    unitName = args[2];
  }
  const newUnitVal = unitVal.toFixed(unitScale);
  const styles = {
    paddingLeft: "5px",
    fontSize: "12px",
    color: "#333"
  };
  return (
    <span>
      {newUnitVal}
      <em className={styles}>{unitName}</em>
    </span>
  );
}
