import { preOrderApi } from "@/api/order.js";
import { seckillOrderPreAPI } from "@/api/flashSale.js";
import util from "@/utils/util";
import { useToast } from "@/hooks/useToast";

/**
 * 去商品详情
 */
export function goShopDetail(item, uid) {
  return new Promise((resolve) => {
    if (item.activityH5 && item.activityH5.type === "1") {
      uni.navigateTo({
        url: `/pages/activity/goods_seckill_details/index?id=${item.activityH5.id}`,
      });
    } else if (item.activityH5 && item.activityH5.type === "2") {
      uni.navigateTo({
        url: `/pages/activity/goods_bargain_details/index?id=${item.activityH5.id}&startBargainUid=${uid}`,
      });
    } else if (item.activityH5 && item.activityH5.type === "3") {
      uni.navigateTo({
        url: `/pages/activity/goods_combination_details/index?id=${item.activityH5.id}`,
      });
    } else {
      resolve(item);
    }
  });
}

/**
 * 活动商品、普通商品、购物车、再次购买预下单
 */
export function getPreOrder(data) {
  const { Toast } = useToast();
  return new Promise((resolve, reject) => {
    preOrderApi(data)
      .then((res) => {
        uni.navigateTo({
          url:
            "/pages/users/order_confirm/index?preOrderNo=" +
            res.data.preOrderNo,
        });
      })
      .catch((err) => {
        return Toast({
          title: err.message,
        });
      });
  });
}

export function getMsPreOrder(data) {
  const { Toast } = useToast();
  return new Promise((resolve, reject) => {
    seckillOrderPreAPI(data)
      .then((res) => {
        uni.navigateTo({
          url:
            "/pages/users/utils/flashSale/confirmOrder?preOrderNo=" +
            res.data.preOrderNo,
        });
      })
      .catch((err) => {
        return Toast({
          title: err.message,
        });
      });
  });
}
// 兑换贝币优惠券
