import { store } from "@/store";
import WebSocketManager, { type ApifoxModel, msgType } from "@/utils/websocket";
import orderApi from "@/api/order/order";
import { useUserStoreHook } from "@/store/modules/user";
import productApi from "@/api/product/index";
import categoryApi from "@/api/product/productclassification";
import shopUserApi from '@/api/account/shopUser'
import limitTimeDiscountApi from '@/api/market/limitTimeDiscount.js'
import { BigNumber } from "bignumber.js";
import _ from "lodash";
// 导入工具库及相关类型

import {
  OrderPriceCalculator, limitUtils,
  BaseCartItem,
  BackendCoupon,
  ActivityConfig,
  OrderExtraConfig, MerchantReductionConfig, MerchantReductionType,
  GoodsType, FullReductionActivity,
  couponUtils
} from "ysk-utils";

// import {
//   OrderPriceCalculator, limitUtils,
//   BaseCartItem,
//   BackendCoupon,
//   ActivityConfig,
//   OrderExtraConfig, MerchantReductionConfig, MerchantReductionType,
//   GoodsType, FullReductionActivity
// } from '@/lib/index'


console.log('OrderPriceCalculator', OrderPriceCalculator); // 不报错，能打印出类/对象
// import yskUtils from 'ysk-utils';
// const OrderPriceCalculator = yskUtils.OrderPriceCalculator

import { useUserStore } from "@/store/modules/user";
import { canUseLimitTimeDiscount, returnPrice } from '@/utils/order-utils'
// const limitUtils = yskUtils.limitUtils
// import limitUtils from './limit'

const shopUser = useUserStoreHook();
export interface CartsState {
  id: string | number;
  [property: string]: any;
}
interface SeatFeeConfig {
  pricePerPerson: number;
  personCount: number;
  isEnabled: boolean;
}
interface PointDeductionRule {
  pointsPerYuan: number;
  maxDeductionAmount: number;
}

export const useCartsStore = defineStore("carts", () => {
  const shopInfoStr = localStorage.getItem('userInfo') || '{}';
  const shopInfo = JSON.parse(shopInfoStr);
  const limitDiscountRes = ref<Record<string, any> | null>(null);

  const shopInfoTyped = shopInfo as Record<string, any>;
  const ldRes = limitDiscountRes.value as LimitDiscountResult | null;

  // ------------------------------ 1. 移到内部的工具函数（核心修复） ------------------------------
  // 适配工具库 BaseCartItem 接口的商品数据转换函数（原外部函数，现在内部）
  const convertToBaseCartItem = (item: any): BaseCartItem => {
    const skuData = item.skuData
      ? {
        id: item.skuData.id || item.sku_id,
        salePrice: item.skuData.salePrice || 0,
        memberPrice: item.skuData.memberPrice || 0
      }
      : undefined;

    // console.log('skuData===========', skuData);

    let productType: GoodsType = GoodsType.NORMAL;
    switch (item.product_type) {
      case 'weight': productType = GoodsType.WEIGHT; break;
      case 'gift': productType = GoodsType.GIFT; break;
      case 'package': productType = GoodsType.PACKAGE; break;
      default: productType = GoodsType.NORMAL;
    }

    return {
      ...item,
      id: item.id,
      product_id: item.product_id,
      number: item.number || 0,
      product_type: productType,
      is_temporary: !!item.is_temporary,
      is_gift: !!item.is_gift,
      returnNum: item.returnNum || 0,
      memberPrice: item.memberPrice,
      discountSaleAmount: item.discount_sale_amount || 0,
      packFee: item.packFee || 0,
      packNumber: item.pack_number || 0,
      activityInfo: item.activityInfo
        ? {
          type: item.activityInfo.type,
          discountRate: OrderPriceCalculator.formatDiscountRate(item.activityInfo.discountRate),
          vipPriceShare: !!item.activityInfo.vipPriceShare
        }
        : undefined,
      skuData,
      salePrice: item.salePrice
    };
  };

  // 合并所有商品列表（原外部函数，现在内部，可直接访问 list/giftList/oldOrder）
  const getAllGoodsList = (): BaseCartItem[] => {
    const currentGoods = (list.value || []).map(convertToBaseCartItem);
    const giftGoods = (giftList.value || []).map(convertToBaseCartItem);

    // 扁平化历史订单商品
    const oldOrderGoods = Object.values(oldOrder.value.detailMap || {})
      .flat()
      .map(convertToBaseCartItem);


    // console.log('list=====================================', list.value);
    // console.log('giftList=====================================', giftList.value);
    // console.log('oldOrder.value.detailMap=====================================', oldOrder.value.detailMap);
    // console.log('getAllGoodsList.[]===================', [...currentGoods, ...giftGoods, ...oldOrderGoods])


    return [...currentGoods, ...giftGoods, ...oldOrderGoods];
  };

  // 台桌信息
  const tableInfo = ref({})
  async function changeTableInfo(info: any) {
    tableInfo.value = { ...info }
  }

  // ------------------------------ 2. Store 内部原有响应式变量 ------------------------------
  // 选择用户
  const vipUser = ref<{ id?: string | number, isVip?: boolean }>({});
  async function changeUser(user: any) {

    vipUser.value = {
      ...user,
      isMemberPrice: shopUser.userInfo.isMemberPrice
    };

    console.log('changeUser', vipUser.value);
    await getConsumeDiscountAjax()
    await getGoods({})

    // 选择用户后重新刷新历史订单限时折扣信息
    setOldOrder(oldOrder.value)

    allGoods.value = getAllGoodsList()
    console.log('选择用户后刷新所有订单购物车数据====', allGoods.value);
    sendWsTimeDiscount()
    payParamsInit()
  }

  // 根据用户id获取新客立减金额，返回null代表不可用
  async function getConsumeDiscountAjax() {
    try {
      if (vipUser.value.id) {
        const res = await limitTimeDiscountApi.getConsumeDiscount({
          shopId: localStorage.getItem("shopId"),
          shopUserId: vipUser.value.id,
          orderId: oldOrder.value.id,
        });
        console.log("根据用户id获取新客立减金额，返回null代表不可用", res);
        if (res !== null) {
          newUserDiscount.value = res;
        }
      }
    } catch (error) {
      console.log(error);
    }
  }

  // 给长连接发送更新购物车限时折扣信息
  const sendWsTimeDiscount = _.throttle(function () {
    // 在这里准备给ws发送刷新消息
    const cart = (list.value || []).map(convertToBaseCartItem);
    const history = Object.values(oldOrder.value.detailMap || {})
      .flat()
      .map(convertToBaseCartItem);

    if (cart.length || history.length) {
      WebSocketManager.sendMessage({
        type: "shopping",
        operate_type: "bulk_edit",
        table_code: table_code.value,
        shop_id: localStorage.getItem("shopId"),
        data: {
          cart: cart.map(item => {
            // console.log('cart.111111', item);
            return {
              id: item.id,
              is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value, 'product_id') ? 1 : 0
            }
          }),
          history: history.map(item => {
            // console.log('history.222222', item);
            return {
              id: item.id,
              is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value, 'productId') ? 1 : 0
            }
          })
        },
      })
    }
  }, 500)

  // 就餐类型
  let dinnerType = ref<'dine-in' | 'take-out'>('dine-in');

  // 是否启用会员价
  const useVipPrice = computed(() => {
    return (shopUser.userInfo.isMemberPrice && vipUser.value.id && vipUser.value.isVip) ? true : false;
  });

  // 台桌id
  const table_code = ref('');

  // 购物车是否初始化连接加载完成
  const isLinkFinshed = ref(false);

  // 当前购物车数据（现在 getAllGoodsList 能直接访问）
  const list = useStorage<any[]>(
    "carts",
    [],
    localStorage,
    {
      serializer: {
        read: (rawValue) => {
          // 没有值时直接返回默认空数组
          if (!rawValue) return []
          try {
            return JSON.parse(rawValue)
          } catch {
            return []
          }
        },
        write: (value) => JSON.stringify(value),
      },
    }
  )
  // 历史订单数据（现在 getAllGoodsList 能直接访问）
  const oldOrder = useStorage<any>("Instead_olold_order", {
    detailMap: [],
    originAmount: 0
  });

  /**
 * 异步查询链接/任务是否完成，完成后设置 isLinkFinshed.value = true
 * @param {number} [interval=1000] - 查询间隔时间（毫秒），默认 1 秒
 * @returns {Promise<void>} - 完成后 resolve，可通过 await 等待后续操作
 */
  async function checkLinkFinished(interval = 100) {
    return new Promise((resolve) => {
      let elapsedTime = 0; // 已消耗时间
      const checkTimer = setInterval(async () => {
        // 2. 实际查询逻辑（这里替换为你的真实查询逻辑，比如调用接口）
        try {
          if (isLinkFinshed.value == true) {
            clearInterval(checkTimer);
            resolve();
          } else {
            console.log('链接未完成，继续查询...');
            elapsedTime += interval;
          }
        } catch (error) {
          console.error('查询出错，继续重试:', error);
          elapsedTime += interval;
        }
      }, interval);
    });
  }

  const sendThrottledMessage = _.throttle(function () {
    WebSocketManager.sendMessage({
      type: "shopping",
      operate_type: "time_discount_save",
      table_code: table_code.value,
      shop_id: localStorage.getItem("shopId"),
      data: limitDiscountRes.value,
    });
  }, 500);

  // 代客下单页面商品缓存
  const goods = useStorage<any[]>("Instead_goods", []);
  async function getGoods(query: any) {
    // 获取当前店铺可用的限时折扣
    limitDiscountRes.value = await limitTimeDiscountApi.getLimitTimeDiscount({
      shopId: localStorage.getItem("shopId")
    })

    // 获取满减活动
    fullReductionActivities.value = await limitTimeDiscountApi.getDiscountActivity({
      shopId: localStorage.getItem("shopId")
    });

    if (limitDiscountRes.value !== null) {
      checkLinkFinished().then(() => {
        console.log('给ws发送限时折扣信息');
        sendThrottledMessage()
      })
    }

    console.log('代客下单页面商品缓存.获取当前店铺可用的限时折扣', limitDiscountRes.value);

    const res = await productApi.getPage({
      page: 1,
      size: 999,
      status: "on_sale",
      ...query,
    });

    interface ProductItem {
      id: string | number;
      lowMemberPrice?: number;
      lowPrice?: number;
      [key: string]: any;
    }

    interface LimitDiscountResult {
      discountPriority?: 'limit-time' | 'vip-price' | string;
      [key: string]: any;
    }

    interface GoodsWithDiscount extends ProductItem {
      isLimitDiscount: boolean;
      limitDiscountPrice: number;
    }

    goods.value = (res.records as ProductItem[]).map((item: ProductItem): GoodsWithDiscount => {
      item.salePrice = item.lowPrice
      item.memberPrice = item.lowMemberPrice

      return {
        ...item,
        is_time_discount: limitUtils.canUseLimitTimeDiscount(item, limitDiscountRes.value, shopInfoTyped, vipUser.value) ? 1 : 0,
        time_discount_price: limitUtils.returnPrice({
          goods: { ...item },
          shopInfo: shopInfoTyped,
          limitTimeDiscountRes: limitDiscountRes.value,
          shopUserInfo: vipUser.value,
          idKey: 'id'
        })
      }
    });

    const consList = await productApi.consStock({ shopId: localStorage.getItem("shopId") })

    goods.value = addGoodsSoldOutStatus(goods.value, consList)

    console.log('代客下单页面商品缓存.goods.value', goods.value);

    setGoodsMap(goods.value);
  }


  /**
 * 给商品列表批量添加 isSoldOut 售罄状态字段
 * @param {Array} goodsList - 商品列表 [ { consList, isAutoSoldStock } ]
 * @param {Array} consStockList - 真实耗材库存列表 [ { consId, stockNumber } ]
 * @returns 带 isSoldOut 字段的新商品列表
 */
  function addGoodsSoldOutStatus(goodsList, consStockList) {
    // console.log('addGoodsSoldOutStatus.goodsList', goodsList);
    // console.log('addGoodsSoldOutStatus.consStockList', consStockList);

    // 耗材ID映射真实库存（保留）
    const consMap = _.keyBy(consStockList, item => String(item.consId));

    return _.map(goodsList, goods => {
      let isSoldOut = false;

      // 开启自动售罄才判断
      if (goods.isAutoSoldStock === 1 || goods.isAutoSoldStock === true) {
        const goodsConsList = goods.consList || [];

        // 无耗材 → 不售罄
        if (goodsConsList.length === 0) {
          isSoldOut = false;
        } else {
          // 核心：只要有一个耗材 真实库存 < 商品需要量 → 售罄
          isSoldOut = _.some(goodsConsList, consItem => {
            // 商品绑定的耗材ID（对应真实库存ID）
            const consId = String(consItem.consInfoId);
            // 商品需要消耗的数量（你的需求量）
            const needStock = consItem.surplusStock || 0;
            // 起售数量
            const suitNum = goods.type == 'single' ? goods.skuList[0].suitNum : 1;
            // 真实库存
            const realStock = _.get(consMap, [consId, 'stockNumber'], 0);

            // 真实库存 < 需要量 * 起售数量 → 不足 → 售罄
            return realStock < needStock * suitNum;
          });
        }
      }

      return { ...goods, isSoldOut };
    });
  }

  function setGoodsMap(goods: any[]) {
    for (let item of goods) {
      goodsMap[item.id] = item;
    }
  }

  // 赠菜（现在 getAllGoodsList 能直接访问）
  const giftList = useStorage<any[]>("giftList", []);
  let goodsMap: { [key: string]: any } = useStorage('Instead_goods_map', {});

  // ------------------------------ 3. 原有计算属性和业务逻辑（无需修改，正常调用 getAllGoodsList） ------------------------------
  // 当前选中cart相关状态
  let selListIndex = ref(-1);
  const isSelGift = ref(false);
  const isOldOrder = ref(false);
  const selPlaceNum = ref(-1);
  const defaultCart = { id: '', number: 0 };

  // 当前购物车是否为空
  const isEmpty = computed(() => {
    return list.value.length === 0 && giftList.value.length === 0
  });

  // 当前购物车选中数据
  const selCart = computed(() => {
    if (isOldOrder.value && selPlaceNum.value >= 0) {
      return oldOrder.value.detailMap[selPlaceNum.value][selListIndex.value] || defaultCart;
    }
    if (isSelGift.value) {
      return giftList.value[selListIndex.value] || defaultCart;
    }
    return list.value[selListIndex.value] || defaultCart;
  });

  // 当前购物车选中对应商品数据
  const selGoods = computed(() => {
    return goodsMap[selCart.value.product_id];
  });

  // 当前选中购物车数据是否是可选套餐商品
  const isCanSelectGroup = computed(() => {
    if (!selGoods.value) return false;
    return selGoods.value.type === 'package' && selGoods.value.groupType === 1;
  });
  // 初始配置：默认无减免（固定金额 0 元）
  const merchantReductionConfig = ref<MerchantReductionConfig>({
    type: MerchantReductionType.FIXED_AMOUNT,
    fixedAmount: 0
  });

  // 暴露外部修改方法：设置固定金额减免
  function setMerchantFixedReduction(amount: number) {
    merchantReductionConfig.value = {
      type: MerchantReductionType.FIXED_AMOUNT,
      fixedAmount: Math.max(0, amount) // 确保金额非负
    };
  }

  // 暴露外部修改方法：设置比例折扣减免（传入折扣率，如 90 代表 9 折）
  function setMerchantDiscountReduction(discountRate: number) {
    merchantReductionConfig.value = {
      type: MerchantReductionType.DISCOUNT_RATE,
      discountRate: Math.max(0, Math.min(100, discountRate)) // 确保折扣率在 0-100% 之间
    };
  }

  // 暴露外部方法：清空商家减免
  function clearMerchantReduction() {
    merchantReductionConfig.value = {
      type: MerchantReductionType.FIXED_AMOUNT,
      fixedAmount: 0
    };
  }

  const seatFeeConfig = ref<SeatFeeConfig>({
    pricePerPerson: shopUser.userInfo.tableFee || 0,
    personCount: 0,
    isEnabled: !shopUser.userInfo.isTableFee
  })
  const pointDeductionRule = ref<PointDeductionRule>({
    pointsPerYuan: 100,
    maxDeductionAmount: Infinity
  })
  const fullReductionActivities = ref<any[]>([])

  //使用积分数量
  const userPoints = ref(0);

  // 新客立减配置
  const newUserDiscount = ref(null)

  // 订单额外配置（现在依赖响应式的 merchantReduction）
  const orderExtraConfig = computed<OrderExtraConfig>(() => ({
    // 引用扩展后的商家减免配置
    merchantReduction: merchantReductionConfig.value,
    additionalFee: 0,
    pointDeductionRule: pointDeductionRule.value,
    seatFeeConfig: seatFeeConfig.value,
    currentStoreId: shopUser.userInfo.shopId?.toString() || '',
    userPoints: userPoints.value,
    isMember: useVipPrice.value,
    memberDiscountRate: shopUser.userInfo.memberDiscountRate || 1,
    fullReductionActivities: [fullReductionActivities.value],
    currentDinnerType: dinnerType.value,
    limitTimeDiscount: limitDiscountRes.value,
    shopUserInfo: vipUser.value,
    newUserDiscount: newUserDiscount.value !== null ? newUserDiscount.value.amount : 0
  }) as OrderExtraConfig);

  // 营销活动列表
  const activityList = computed<ActivityConfig[]>(() => {
    return [];
  });

  function setCoupons(cps: BackendCoupon[]) {
    console.log('setCoupons', cps);
    let goodsCoupon = cps.filter((v) => v.type == 2);
    let otherCoupon = cps.filter((v) => v.type != 2);
    const canDikouGoodsArr = couponUtils.returnCanDikouGoods(allGoods.value, [], vipUser.value);
    //商品订单金额
    const goodsOrderPrice = new BigNumber(orderCostSummary.value.goodsOriginalAmount)
      .minus(orderCostSummary.value.goodsDiscountAmount)
      .toFixed(2);
    goodsCoupon = goodsCoupon.map((v) => {
      const discount = couponUtils.returnCouponDiscount(canDikouGoodsArr, v, vipUser.value, goodsOrderPrice, [], shopUser.userInfo);
      return {
        ...v,
        discount,
        discountAmount: discount ? discount.discountPrice : v.discountAmount
      };
    });
    otherCoupon = otherCoupon.map((v) => {
      const discount = couponUtils.returnCouponDiscount(canDikouGoodsArr, v, vipUser.value, goodsOrderPrice, goodsCoupon, shopUser.userInfo);
      return {
        ...v,
        discount,
        discountAmount: discount ? discount.discountPrice : v.discountAmount
      };
    });
    coupons.value = cps;
  }

  // 优惠券列表
  const coupons = ref<BackendCoupon[]>([]);

  // 商品加入购物车顺序
  const cartOrder = ref<Record<string, number>>({});
  let allGoods = ref<BaseCartItem[]>([]);
  // 订单费用汇总（调用内部的 getAllGoodsList）
  const orderCostSummary = computed(() => {
    allGoods.value = getAllGoodsList();
    const costSummary = OrderPriceCalculator.calculateOrderCostSummary(
      allGoods.value,
      dinnerType.value,
      coupons.value,
      activityList.value,
      orderExtraConfig.value,
      cartOrder.value,
      new Date()
    );
    console.log('costSummary----------------------------', costSummary);
    return costSummary;
  });

  // 赠菜总价（调用内部的 getAllGoodsList）
  const giftMoney = computed(() => {
    const allGoods = getAllGoodsList();
    const giftGoods = allGoods.filter(OrderPriceCalculator.isGiftGoods);
    return OrderPriceCalculator.calcGoodsOriginalAmount(giftGoods);
  });

  // 打包数量（调用内部的 getAllGoodsList）
  const packNum = computed(() => {
    const allGoods = getAllGoodsList();
    return allGoods.reduce((total, goods) => {
      const packNumber = OrderPriceCalculator.isWeightGoods(goods)
        ? 1
        : (dinnerType.value === 'take-out' ? goods.number : goods.packNumber || 0);
      return total + Math.max(0, packNumber);
    }, 0);
  });

  // 打包费
  const packFee = computed(() => {
    return orderCostSummary.value.packFee;
  });

  // 会员优惠（调用内部的 getAllGoodsList）
  const vipDiscount = computed(() => {
    const allGoods = getAllGoodsList();
    const nonMemberRealAmount = OrderPriceCalculator.calcGoodsRealAmount(
      allGoods,
      { isMember: false, memberDiscountRate: 1 },
      activityList.value
    );
    return OrderPriceCalculator.truncateToTwoDecimals(
      nonMemberRealAmount - orderCostSummary.value.goodsOriginalAmount
    );
  });

  // 单品改价优惠（调用内部的 getAllGoodsList）
  const singleDiscount = computed(() => {
    const allGoods = getAllGoodsList();
    const noDiscountGoods = allGoods.map(goods => ({ ...goods, discountSaleAmount: 0 }));
    const noDiscountRealAmount = OrderPriceCalculator.calcGoodsRealAmount(
      noDiscountGoods,
      { isMember: orderExtraConfig.value.isMember, memberDiscountRate: orderExtraConfig.value.memberDiscountRate },
      activityList.value
    );
    return OrderPriceCalculator.truncateToTwoDecimals(
      noDiscountRealAmount - orderCostSummary.value.goodsOriginalAmount
    );
  });

  // 已优惠文案
  const yiyouhui = computed(() => {
    const totalDiscount = giftMoney.value + vipDiscount.value + singleDiscount.value;
    return totalDiscount > 0
      ? `已优惠￥${OrderPriceCalculator.truncateToTwoDecimals(totalDiscount).toFixed(2)}`
      : '';
  });

  // 只算商品的总价
  const goodsTotal = computed(() => {
    return orderCostSummary.value.goodsOriginalAmount.toFixed(2);
  });

  // 总计数量（调用内部的 getAllGoodsList）
  const totalNumber = computed(() => {
    const allGoods = getAllGoodsList();
    return allGoods.reduce((total, goods) => {
      return total + Math.max(0, goods.number - (goods.returnNum || 0));
    }, 0);
  });

  // 支付总价
  const payMoney = computed(() => {
    return orderCostSummary.value.finalPayAmount.toFixed(2);
  });

  // ------------------------------ 4. 原有业务逻辑（增删改查、WebSocket等，保持不变） ------------------------------
  function changeNumber(step: number, item: CartsState) {
    if (item.number * 1 + step <= 0) {
      del(item);
      return;
    }
    const newNumber = item.number * 1 + step * 1;
    let pack_number = newNumber < item.pack_number ? (item.pack_number * 1 + step * 1) : item.pack_number;
    if (dinnerType.value === 'take-out') {
      pack_number = newNumber;
    }
    if (item.product_type === 'weight') {
      pack_number = 1;
    }
    update({ ...item, number: newNumber, pack_number });
  }

  function changeSelCart(cart: CartsState) {
    if (!cart.id) {
      return
    }

    if (cart.placeNum) {
      selPlaceNum.value = cart.placeNum
      isOldOrder.value = true
      console.log(oldOrder.value.detailMap[cart.placeNum])
      selListIndex.value = oldOrder.value.detailMap[cart.placeNum].findIndex((item: CartsState) => {
        return item.id == cart.id
      })
      return
    }


    if (cart.is_gift) {
      isSelGift.value = true
      selListIndex.value = giftList.value.findIndex((item: CartsState) => item.id === cart.id);
      console.log(selListIndex.value)

    } else {
      isSelGift.value = false
      isOldOrder.value = false
      selListIndex.value = list.value.findIndex((item: CartsState) => item.id === cart.id);
      console.log('selListIndex.value', selListIndex.value)
    }
  }

  const basic_msg = {
    number: 1,
    is_gift: 0,
    is_temporary: 0,
    discount_sale_amount: 0,
    discount_sale_note: "",
    is_print: 1,
    pro_group_info: '',
    is_wait_call: 0,
    product_name: "",
    remark: "",
    sku_id: '',
    product_type: ''
  };

  function cartsPush(data: any) {
    sendMessage('add', { ...basic_msg, ...data });
  }

  // 添加购物车/编辑购物车
  function add(data: any) {
    // console.log('添加购物车/编辑购物车===', data);
    goods.value.map(item => {
      if (item.id == data.product_id) {
        data.is_time_discount = item.is_time_discount ? 1 : 0
      }
    })

    const msg = { ...basic_msg, ...data };
    const hasCart = list.value.find((cartItem) => {
      return cartItem.product_id == msg.product_id && cartItem.sku_id == msg.sku_id;
    });

    console.log('carts.add===', data);

    if (hasCart) {
      // console.log('编辑', msg);
      if (hasCart.number * 1 + msg.number * 1 == 2) {
        ElMessage({
          type: 'warning',
          message: '购物车已有该商品，请确认是否重复',
          duration: 4000
        })
      }
      update({ ...hasCart, ...msg, number: hasCart.number * 1 + msg.number * 1 });
    } else {
      // console.log('添加', msg);
      let arr = _.flatten(_.values(oldOrder.value.detailMap))
      // console.log('添加.arr===', arr);
      const isExist = _.some(arr, item => Number(item.productId) === msg.product_id)

      // console.log('添加.isExist===', isExist);
      if (msg.number == 1 && isExist) {
        ElMessage({
          type: 'warning',
          message: '该商品已下单过，请确认是否重复',
          duration: 4000
        })
      }
      sendMessage('add', msg);
    }
  }

  function changeTable(newVal: string | undefined) {
    table_code.value = `${newVal}`;
    $initParams.table_code = newVal;
    concocatSocket();
  }

  function rotTable(newVal: string | number, cart_id = []) {
    if (cart_id.length) {
      sendMessage('rottable', {
        new_table_code: newVal,
        table_code: table_code.value,
        cart_id
      });
    }
  }

  function del(data: any) {
    sendMessage('del', { id: data.id });
  }

  function update(data: any) {
    const suitNum = data.skuData ? (data.skuData.suitNum || 1) : 1;
    if (data.number * 1 < suitNum * 1) {
      return sendMessage('del', data);
    }
    const pack_number = dinnerType.value === 'take-out' ? data.number : data.pack_number;
    sendMessage('edit', { ...data, pack_number });
  }

  function updateTag(key: string, val: any, cart: CartsState = selCart.value) {
    const skuData = cart.skuData || { suitNum: 1 };
    if (cart.number * 1 < skuData.suitNum * 1) {
      return sendMessage('del', cart);
    }
    if (key === 'discount_sale_amount' && val * 1 <= 0) {
      return ElMessage.error('价格不能为0！');
    }
    const msg = { ...cart, [key]: val };
    if (key === 'number' && dinnerType.value === 'take-out') {
      msg.pack_number = val;
    }
    sendMessage('edit', msg);
  }

  function changePack(is_pack: number | string) {
    if (!isEmpty.value) {
      sendMessage('batch', { is_pack });
    }
  }

  function clear() {
    sendMessage('cleanup', {});
  }

  function dataReset() {
    selListIndex.value = -1;
    selPlaceNum.value = -1;
    isOldOrder.value = false;
    isSelGift.value = false;
    list.value = [];
    giftList.value = [];
    oldOrder.value = { detailMap: [], originAmount: 0 };
    vipUser.value = {};
    cartOrder.value = {};
  }

  function nowCartsClear() {
    selListIndex.value = -1;
    isOldOrder.value = false;
    list.value = [];
    giftList.value = [];
    cartOrder.value = {};
    userPoints.value = 0;
  }

  interface GroupSnap {
    goods: { [key: string]: any }[];
  }

  function findInGroupSnapSku(groupSnap: GroupSnap[], sku_id: string | number) {
    for (let i in groupSnap) {
      const sku = groupSnap[i].goods.find(v => v.sku_id == sku_id);
      if (sku) return sku;
    }
  }

  // 获取历史订单信息
  async function getOldOrder(table_code: string | number) {
    const res = await orderApi.getHistoryList({ tableCode: table_code });
    console.log('获取历史订单信息===', res);
    if (res) setOldOrder(res);
  }

  // 购物车信息补全
  function getProductDetails(v: { product_id: string, sku_id: string }) {
    const goods = goodsMap[v.product_id];
    console.log('getProductDetails', goods)
    if (!goods) return undefined;

    let skuData = goods?.skuList.find((sku: { id: string, salePrice: number }) => sku.id == v.sku_id);
    if (!skuData && goods.type == 'package') {
      const SnapSku = findInGroupSnapSku(goods.groupSnap, v.sku_id);
      skuData = { ...SnapSku, salePrice: SnapSku ? SnapSku.price : 0 };
    }

    console.log('skuData===', skuData);


    if (skuData) {
      return {
        salePrice: skuData.salePrice || 0,
        memberPrice: skuData.memberPrice || 0,
        coverImg: goods.coverImg,
        name: goods.name,
        specInfo: skuData.specInfo,
        packFee: goods.packFee || 0,
        type: goods.type,
        skuData,
        is_time_discount: limitUtils.canUseLimitTimeDiscount(goods, limitDiscountRes.value, shopInfo, vipUser.value),
        time_discount_price: limitUtils.returnPrice({
          goods: skuData,
          shopInfo: shopInfoTyped,
          limitTimeDiscountRes: limitDiscountRes.value,
          shopUserInfo: vipUser.value,
          idKey: 'productId'
        }),
      };
    }
    return undefined;
  }

  // 历史订单信息补全
  function returnDetailMap(data: any) {
    const newData: { [key: string]: any } = {};
    for (let i in data) {
      newData[i] = data[i].map((v: any) => {
        const skuData = getProductDetails({ product_id: v.productId, sku_id: v.skuId });
        return {
          ...v,
          ...skuData,
          placeNum: v.placeNum,
          number: v.num,
          id: v.id,
          pack_number: v.packNumber,
          discount_sale_amount: v.discountSaleAmount * 1 || 0,
          is_print: v.isPrint,
          is_wait_call: v.isWaitCall,
          is_gift: v.isGift,
          is_temporary: v.isTemporary,
          discount_sale_note: v.discountSaleNote,
          product_name: v.productName,
          sku_name: v.skuName,
          sku_id: v.skuId,
          product_type: v.productType,
          product_id: v.productId,
          isTimeDiscount: limitUtils.canUseLimitTimeDiscount(v, limitDiscountRes.value, shopInfoTyped, vipUser.value, 'productId'),
          is_time_discount: limitUtils.canUseLimitTimeDiscount(v, limitDiscountRes.value, shopInfoTyped, vipUser.value, 'productId'),
          time_discount_price: limitUtils.returnPrice({
            goods: { ...v, ...skuData },
            shopInfo: shopInfoTyped,
            limitTimeDiscountRes: limitDiscountRes.value,
            shopUserInfo: vipUser.value,
            idKey: 'productId'
          })
        };
      });
    }
    console.log('newData2025年11月13日', newData);
    return newData;
  }

  // 初始话订单信息/补全历史订单信息
  async function setOldOrder(data: any, t: number) {
    console.log('补全历史订单信息来源', t);
    // console.log('补全订单信息', data);
    oldOrder.value = {
      ...data,
      detailMap: returnDetailMap(data.detailMap)
    };
    console.log('oldOrder.value.detailMap==', oldOrder.value);

    if (!vipUser.value.id && data.userId) {
      const res = await shopUserApi.get({ userId: data.userId })
      changeUser(res)
    }
  }

  let $initParams = {} as ApifoxModel;


  // 标志位，表示是否正在清空历史订单
  const isClearingOldOrder = ref(false);


  async function init(initParams: ApifoxModel | undefined, $oldOrder: any | undefined) {
    console.log('cart.init.initParams', initParams);
    console.log('cart.init.$oldOrder', $oldOrder);




    if (initParams) {
      initParams.table_code = initParams.table_code || '';
      table_code.value = initParams.table_code;
      $initParams = initParams;
    }

    await getGoods({});

    if ($oldOrder) {
      await setOldOrder($oldOrder);
    } else if (table_code.value) {
      await getOldOrder(table_code.value);
    } else {
      oldOrder.value = { detailMap: [] };
    }

    concocatSocket($initParams);
  }

  function concocatSocket(initParams = $initParams) {
    WebSocketManager.subscribeToTopic(initParams, (msg) => {
      console.log("收到消息：", msg);
      if (!msg.status) {
        if (msg.hasOwnProperty('status') && msg.status !== 1 && msg.operate_type !== 'bulk_edit') {
          return ElMessage.error(msg.msg || '操作失败');
        }
      }
      if (msg?.data) {
        if (Array.isArray(msg.data) && msg.data.length && msg.data[0].table_code) {
          table_code.value = msg.data[0].table_code;
        } else if (msg.data.table_code) {
          table_code.value = table_code.value || msg.data.table_code;
        } else if (msg.table_code) {
          table_code.value = table_code.value || msg.table_code;
        }
      }

      if (msg.operate_type === "manage_init") {
        isLinkFinshed.value = true;
        list.value = msg.data.filter((v: Record<string, any>) => {
          if (v.is_temporary) return v;
          const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id });
          if (skuData) {
            Object.keys(skuData).forEach(key => {
              v[key] = skuData[key];
            });
          } else {
            del({ id: v.id });
            return false;
          }
          return !v.is_gift;
        });
        giftList.value = msg.data.filter((v: Record<string, any>) => {
          if (v.is_temporary) return v && v.is_gift;
          const skuData = getProductDetails({ product_id: v.product_id, sku_id: v.sku_id });
          if (skuData) {
            Object.keys(skuData).forEach(key => {
              v[key] = skuData[key];
            });
          } else {
            del({ id: v.id });
            return false;
          }
          return v.is_gift;
        });
        cartOrder.value = msg.data.reduce((obj: Record<string, number>, item: any) => {
          obj[item.id] = new Date(item.create_time).getTime();
          return obj;
        }, {});
      }

      if (msg.operate_type === "manage_add") {
        if (list.value.find(v => v.id == msg.data.id)) {
          return ElMessage.warning(msg.message || '该商品已存在');
        }
        const skuData = getProductDetails({ product_id: msg.data.product_id, sku_id: msg.data.sku_id });
        console.log('skuData', skuData);
        const newGoods = { ...skuData, ...msg.data };
        console.log('newGoods', newGoods)
        list.value.push(newGoods);
        return ElMessage.success(msg.message || '添加成功');
      }

      if (msg.operate_type === "manage_edit") {
        const skuData = getProductDetails({ product_id: msg.data.product_id, sku_id: msg.data.sku_id });
        console.log('skuData,', skuData);
        const newCart = { ...skuData, ...msg.data };

        const listIndex = list.value.findIndex(item => item.id === newCart.id);
        const giftIndex = giftList.value.findIndex(item => item.id === newCart.id);


        if (giftIndex > -1) {
          if (!newCart.is_gift) {
            giftList.value.splice(giftIndex, 1);
            list.value.push({ ...giftList.value[giftIndex], ...newCart });
            cartOrder.value[newCart.id] = new Date().getTime();
          } else {
            giftList.value[giftIndex] = { ...giftList.value[giftIndex], ...newCart };
          }
        }
        if (listIndex > -1) {
          if (newCart.is_gift) {
            list.value.splice(listIndex, 1);
            giftList.value.push({ ...list.value[listIndex], ...newCart });
            delete cartOrder.value[newCart.id];
          } else {
            list.value[listIndex] = { ...list.value[listIndex], ...newCart };
          }
        }
        ElMessage.success(msg.message || '修改成功');
      }

      if (msg.operate_type === "manage_del") {
        const cartId = Array.isArray(msg.data) ? msg.data[0].id : msg.data.id;
        const listIndex = list.value.findIndex(item => item.id == cartId);
        const giftIndex = giftList.value.findIndex(item => item.id == cartId);

        if (listIndex > -1) list.value.splice(listIndex, 1);
        if (giftIndex > -1) giftList.value.splice(giftIndex, 1);
        delete cartOrder.value[cartId];
        return ElMessage.success(msg.message || '删除成功');
      }

      if (msg.operate_type === "manage_cleanup") {
        nowCartsClear();
        getOldOrder(msg.data.table_code);
      }

      if (msg.operate_type === "batch") {
        concocatSocket({ ...$initParams, table_code: table_code.value });
      }

      // WebSocket 监听 product_update 消息
      if (msg.operate_type === "product_update") {
        if (isClearingOldOrder.value) {
          console.log("忽略 product_update 消息，因为正在清空历史订单");
          isClearingOldOrder.value = false; // 重置标志位
          return;
        }
        console.log("处理 product_update 消息");
        init($initParams, oldOrder.value); // 重新初始化
      }

      if (msg.type === "bc") {
        msg.operate_type = 'manage_' + msg.operate_type;
        concocatSocket(initParams);
      }

      if (msg.operate_type === "bulk_edit") {
        msg.operate_type = 'manage_' + msg.operate_type;
        console.log('WebSocketManager.connected===', WebSocketManager.connected);
        if (WebSocketManager.connected) {
          WebSocketManager.sendMessage({ ...$initParams, table_code: table_code.value })
        } else {
          concocatSocket({ ...$initParams, table_code: table_code.value });
        }
      }
    });
  }

  function disconnect() {
    sendMessage('disconnect', undefined);
  }

  const delArr = ['skuData', 'coverImg', 'specInfo', 'placeNum', 'update_time', 'create_time', 'packFee', 'memberPrice', 'type'];
  function sendMessage(operate_type: msgType, message: any) {
    const msg = { ...message, operate_type, table_code: table_code.value };
    for (let i in delArr) {
      delete msg[delArr[i]];
    }
    WebSocketManager.sendMessage(msg);
  }

  function payParamsInit() {
    coupons.value = []
    clearMerchantReduction()
    userPoints.value = 0;
  }

  // 支付成功后清楚订单/用户信息
  function clearHistory() {
    vipUser.value = {}
    newUserDiscount.value = {}
  }

  return {
    disconnect,
    dinnerType,
    changePack,
    giftMoney,
    goodsTotal,
    isLinkFinshed,
    setOldOrder,
    singleDiscount,
    vipDiscount,
    dataReset,
    useVipPrice,
    changeUser,
    packNum,
    packFee,
    isOldOrder,
    oldOrder,
    isCanSelectGroup,
    goods,
    selGoods,
    cartsPush,
    table_code,
    updateTag,
    list,
    add,
    del,
    update,
    init,
    changeNumber,
    isEmpty,
    selCart,
    totalNumber,
    changeSelCart,
    payMoney,
    clear,
    yiyouhui,
    giftList,
    changeTable,
    rotTable,
    getGoods,
    setGoodsMap,
    orderCostSummary,
    // 暴露商家减免配置（供外部读取当前状态）
    merchantReductionConfig,
    // 暴露修改方法（供外部设置两种减免形式）
    setMerchantFixedReduction,
    setMerchantDiscountReduction,
    clearMerchantReduction,
    seatFeeConfig,
    pointDeductionRule,
    // 新客立减金额
    newUserDiscount,
    //使用积分数量
    userPoints,
    coupons,
    setCoupons,
    payParamsInit,
    limitDiscountRes,
    getAllGoodsList,
    vipUser,
    changeTableInfo,
    tableInfo, isClearingOldOrder,
    clearHistory
  };
});

export function useCartsStoreHook() {
  return useCartsStore(store);
}