"use strict";
const common_vendor = require("../../../common/vendor.js");
const utils_emitter = require("../../../utils/emitter.js");
const api_request = require("../../../api/request.js");
require("../../../common/global.js");
if (!Array) {
  const _easycom_uni_popup2 = common_vendor.resolveComponent("uni-popup");
  _easycom_uni_popup2();
}
const _easycom_uni_popup = () => "../../../uni_modules/uni-popup/components/uni-popup/uni-popup.js";
if (!Math) {
  (addressComponentVue + _easycom_uni_popup)();
}
const addressComponentVue = () => "../component/addressComponent.js";
const _sfc_main = {
  __name: "product-waitpay",
  setup(__props) {
    const sumprice = common_vendor.ref(0);
    const popup = common_vendor.ref(null);
    const addressRealInfo = common_vendor.ref({
      detailAddress: "请选择/添加地址"
    });
    const productArr = common_vendor.ref([]);
    common_vendor.ref([]);
    const userInfo = common_vendor.wx$1.getStorageSync("userInfo");
    common_vendor.ref({});
    const totalInfo = common_vendor.ref([]);
    const note = common_vendor.ref("");
    const postCartArr = common_vendor.ref([]);
    common_vendor.onMounted(() => {
      getFonts();
      utils_emitter.emitter.on("close", () => {
        close();
      });
      utils_emitter.emitter.on("FangZhengFonts", (val) => {
        addressRealInfo.value = val;
      });
    });
    common_vendor.onLoad((options) => {
      totalInfo.value = JSON.parse(options.cartInfo);
      console.log("options.cartInfo-->", JSON.parse(options.cartInfo));
      console.log("totalInfo.value-->", common_vendor.toRaw(totalInfo.value));
      getProduct();
    });
    common_vendor.onShow(() => {
      userInfo.value = common_vendor.wx$1.getStorageSync("userInfo");
      getDefaultAddress();
    });
    const close = () => {
      popup.value.close();
    };
    const loadPop = () => {
      popup.value.open("bottom");
    };
    const getDefaultAddress = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/address/list",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        }
      });
      for (let key in res.data.data) {
        if (res.data.data[key].isDefault === 1) {
          addressRealInfo.value = res.data.data[key];
        }
      }
    };
    const createOrder = async () => {
      console.log("地址信息-->", addressRealInfo.value);
      formatArr();
      console.log("postCartArr--->", postCartArr.value);
      const resOrder = await common_vendor.index.request({
        //向后端发送生成订单请求
        url: api_request.baseUrl + "/order/add/cart",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          orderType: productArr.value[0].cartGoodVO.type === "服务类" ? "service" : "product",
          userName: userInfo.userName,
          addressId: addressRealInfo.value.id,
          //地址信息id
          // contactsId: null, //联系人信息id
          couponId: null,
          //优惠卷id
          note: note.value,
          cartOrderItemAddRequestList: common_vendor.toRaw(postCartArr.value)
        }
      });
      console.log("后台返回订单响应==>", resOrder.data);
      if (resOrder.data.code === 1) {
        wxPay(resOrder.data.data);
      }
    };
    const getProduct = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/cart/submit/list",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          idList: totalInfo.value
        }
      });
      console.log("展示订单页面的商品信息-->", res.data);
      if (res.data.code === 1) {
        dealRes(res);
      }
    };
    const dealRes = (res) => {
      productArr.value = res.data.data;
      console.log("productArr--->", productArr.value);
      productArr.value.forEach((item) => {
        sumprice.value += item.cartGoodVO.price * item.quantity;
      });
    };
    const shortNum = (index) => {
      if (productArr.value[index].quantity > 1) {
        productArr.value[index].quantity -= 1;
        sumprice.value -= productArr.value[index].cartGoodVO.price * 1;
      }
    };
    const addNum = (index) => {
      if (productArr.value[index].quantity < productArr.value[index].cartGoodVO.inventory) {
        productArr.value[index].quantity += 1;
        sumprice.value += productArr.value[index].cartGoodVO.price * 1;
      }
    };
    const formatArr = () => {
      postCartArr.value.splice(0, postCartArr.value.length);
      productArr.value.forEach((item, index) => {
        postCartArr.value.push({
          cartRecordId: totalInfo.value[index],
          quantity: item.quantity
        });
      });
    };
    const wxPay = async (oid) => {
      console.log("oid--->", oid);
      try {
        const res = await common_vendor.index.request({
          url: api_request.baseUrl + "/wechat/payment/create",
          method: "POST",
          header: {
            "cookie": common_vendor.wx$1.getStorageSync("cookie")
          },
          data: { id: oid }
        });
        const paymentData = res.data.data;
        common_vendor.wx$1.requestPayment({
          appid: paymentData.appId,
          nonceStr: paymentData.nonceStr,
          package: paymentData.packageVal,
          paySign: paymentData.paySign,
          timeStamp: paymentData.timeStamp,
          signType: paymentData.signType,
          success(res2) {
            common_vendor.index.showModal({
              content: "支付成功",
              showCancel: false
            });
            common_vendor.index.redirectTo({
              url: "/pages/order/product-paysuccess/product-paysuccess?oid=" + JSON.stringify(oid)
            });
            console.log("支付成功res--->", res2);
          },
          fail(e) {
            common_vendor.index.redirectTo({
              url: "/pages/order/product-paysuccess/product-paysuccess?oid=" + JSON.stringify(oid)
            });
            console.log("e.errMsg--->", e.errMsg);
          }
        });
      } catch (error) {
        console.error("支付请求失败", error);
        common_vendor.index.showModal({
          content: "支付请求失败,请重试。",
          showCancel: false
        });
      }
    };
    const textAssign = (e) => {
      note.value = e.detail.value;
    };
    const getFonts = () => {
      common_vendor.index.loadFontFace({
        family: "FangZhengFonts",
        source: `url("https://carbon2.obs.cn-north-4.myhuaweicloud.com/fonts/FangZhengFonts.TTF")`,
        success: (res) => {
          console.log("success", res);
        },
        fail: (err) => {
          console.log("err", err);
        }
      });
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.t(addressRealInfo.value.region),
        b: common_vendor.t(addressRealInfo.value.detailAddress),
        c: common_vendor.o(loadPop),
        d: common_vendor.t(addressRealInfo.value.name),
        e: common_vendor.t(addressRealInfo.value.phone),
        f: common_vendor.f(productArr.value, (item, index, i0) => {
          return {
            a: item.cartGoodVO.goodImg.split(";")[0],
            b: common_vendor.t(item.cartGoodVO.name),
            c: common_vendor.t(item.cartGoodVO.type),
            d: common_vendor.t(item.cartGoodVO.price),
            e: common_vendor.o(($event) => shortNum(index), index),
            f: common_vendor.t(item.quantity),
            g: common_vendor.o(($event) => addNum(index), index),
            h: index
          };
        }),
        g: common_vendor.t(productArr.value.length),
        h: common_vendor.t(sumprice.value.toFixed(2)),
        i: common_vendor.t(sumprice.value.toFixed(2)),
        j: common_vendor.o(textAssign),
        k: common_vendor.t(sumprice.value.toFixed(2)),
        l: common_vendor.o(createOrder),
        m: common_vendor.sr(popup, "eda1e8f0-0", {
          "k": "popup"
        }),
        n: common_vendor.p({
          ["background-color"]: "#fff"
        })
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-eda1e8f0"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/order/product-waitpay/product-waitpay.vue"]]);
wx.createPage(MiniProgramPage);
