import dayjs from "dayjs";
import numeral from "numeral";
// 订单状态
// 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) : "-";

// 入库单/出库单：获取已入库、出库数量
//  item:此列数据  type：已入库、剩余数量
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");
};

// 对象转 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 = () => {
  // 1. 从 uni-app 全局配置中读取 tabBar
  const tabBarConfig = __uniConfig.tabBar || {};
  // 2. 获取 list 数组
  const tabList = tabBarConfig.list || [];
  return tabList;
};

// ======================
// 工具函数：blobUrl 转 可上传的临时文件路径
// ======================
export function blobToTempFilePath(blobUrl) {
  return new Promise((resolve, reject) => {
    // 1. 下载 blob 数据
    uni.downloadFile({
      url: blobUrl,
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.tempFilePath);
        } else {
          reject("下载失败");
        }
      },
      fail: reject,
    });
  });
}
