import dayjs from "dayjs";
import { find } from "lodash";
import numeral from "numeral";
// 状态（0草稿，1已提交，9已作废）
export const declareType = [
  {
    value: "0",
    label: "草稿",
  },
  {
    value: "1",
    label: "提交申报",
  },
  {
    value: "9",
    label: "已取消 ",
  },
];
// 订单状态
// 0=入库申请,1=入库成功，2=出库申请，3=出库成功，4=作废
const billTypeMenu = [
  {
    value: "0",
    label: "已提交",
  },
  {
    value: "1",
    label: "已完成",
  },
  {
    value: "4",
    label: "已取消",
  },
];

// 订单状态为0时 再查一次
const billStatueMenu = [
  {
    value: "0",
    label: "库单已提交",
  },
  {
    value: "1",
    label: "全部入库",
  },
  {
    value: "2",
    label: "物料部分",
  },
];
// 物料状态
const materialType = [
  {
    value: "0",
    label: "未提交",
  },
  {
    value: "1",
    label: "已提交",
  },
];
/**
 * 获取入库/出库单状态展示文本
 * @param {any} val - 状态
 * @param {string|number} status - 物料状态
 * @param {number} flag - 1=出库 0=入库
 * @returns {string} 状态文本
 */
export const getBillType = (val, status, flag) => {
  const valStr = String(val);
  const statusStr = String(status);

  if (valStr === "0") {
    const statusItem = billStatueMenu.find((item) => item.value === statusStr);
    if (!statusItem) return "";

    const isOut = flag === 1;
    const stateVal = statusItem.value;

    // 状态 0：前缀拼接 入/出
    if (stateVal === "0") {
      return isOut ? "出" + statusItem.label : "入" + statusItem.label;
    }

    // 状态 2：后缀拼接 入库/出库
    if (stateVal === "2") {
      return isOut ? statusItem.label + "出库" : statusItem.label + "入库";
    }

    // 其他状态直接返回
    return statusItem.label;
  }

  // 非0：走普通单据类型菜单
  return billTypeMenu.find((item) => item.value === valStr)?.label || "";
};

// 获取出入库单的状态以及tag颜色
export const getColor = (val) => {
  if (val == "0" || val == "2") {
    return "color:#B8B8B8;font-weight:500;font-size:12px;";
  } else if (val == "1" || val == "3") {
    return "color:green;font-weight:500;font-size:12px;";
  } else {
    return "color:red;font-weight:500;font-size:12px;";
  }
};

// 时间格式化
export const formatDate = (t, fmt = "YYYY-MM-DD HH:mm:ss") =>
  t ? dayjs(t).format(fmt) : "-";

// 数字格式化
export const formatNum = (num, fmt = "0.00") =>
  num ? numeral(num).format(fmt) : "-";

/**
 * 入库单/出库单：获取已入库、出库数量
 * @param {object} item - 此列数据
 * @param {string|number} type - 已入库、剩余数量
 * @returns {string|number} 数量
 */
export const getStockQuantity = (item, type) => {
  const num = item.quantity;
  const status = item.status;
  // 未入库
  if (status == "0") {
    if (type == "complete") {
      return "0.000";
    } else {
      return formatNum(num, "0.00");
    }
  } else {
    if (type == "complete") {
      return formatNum(num, "0.000");
    } else {
      return "0.00";
    }
  }
};

// 清楚缓存 - 将所有需要清楚的缓存写入此处不要写登录信息
export const removeStorage = () => {
  uni.removeStorageSync("app_warehousing");
  uni.removeStorageSync("app_storageArea");
  uni.removeStorageSync("app_billRemark");
  uni.removeStorageSync("app_material");
  uni.removeStorageSync("app_material_select_list");
  uni.removeStorageSync("app_technical");
  uni.removeStorageSync("app_declaration");
};

// 对象转 URL 查询字符串 ?a=1&b=2
export const objectToQuery = (params) => {
  if (!params || Object.keys(params).length === 0) return "";

  const query = Object.entries(params)
    .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
    .join("&");

  return "?" + query;
};
// 获取配置的导航路由
export const getTabBarList = () => {
  const tabBarConfig = __uniConfig.tabBar || {};
  const tabList = tabBarConfig.list || [];
  return tabList;
};
/**
 * 根据数组以及状态值获取对应的label
 * @param {Array} list - 数组
 * @param {object} obj - 数组对应的name和value字段名
 * @param {string} val - 当前状态值
 * @returns {string|number} 数量
 */
export const getLabel = (
  list,
  val,
  obj = { name: "label", value: "value" },
) => {
  if (val) {
    return find(list, (i) => i?.[obj?.value] === `${val}`)?.[obj?.name];
  }
  return "-";
};
/**
 * 数组分组转换方法：按 parentName 分组
 * @param {Array} arr 原始数组
 * @returns {Array} 转换后的分组数组 [{parentName: '', items: []}]
 */
export function transformData(arr) {
  const groupMap = arr.reduce((map, item) => {
    const key = item.parentName;
    if (!map[key]) {
      map[key] = { parentName: key, items: [] };
    }
    map[key].items.push(item);
    return map;
  }, {});

  // 把对象转成数组返回
  return Object.values(groupMap);
}
export const getToken = () => {
  return uni.getStorageSync("app_token");
};

export function getTwoOneRandom() {
  // 整数部分 10~99
  const int = Math.floor(Math.random() * 90) + 10;
  // 小数部分 0~9
  const dec = Math.floor(Math.random() * 10);
  return parseFloat(`${int}.${dec}`);
}
