"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: "singleGoodOrder",
  setup(__props) {
    const sumprice = common_vendor.ref(0);
    const popup = common_vendor.ref(null);
    const addressRealInfo = common_vendor.ref({
      detailAddress: "请选择/添加地址"
    });
    const productObject = 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([]);
    const quantity = common_vendor.ref(1);
    common_vendor.onMounted(() => {
      getFonts();
      utils_emitter.emitter.on("close", () => {
        close();
      });
      utils_emitter.emitter.on("addressInfo", (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",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: {
          orderType: productObject.value.type === "服务类" ? "service" : "product",
          userName: userInfo.userName,
          addressId: addressRealInfo.value.id,
          //地址信息id
          // contactsId: null, //联系人信息id
          couponId: null,
          //优惠卷id
          note: note.value,
          orderItemMainInfoAddRequestList: 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 + "/goods/getById",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: { id: totalInfo.value }
      });
      console.log("展示订单页面的商品信息-->", res.data);
      if (res.data.code === 1) {
        dealRes(res);
      }
    };
    const dealRes = (res) => {
      productObject.value = res.data.data;
      sumprice.value = res.data.data.price;
      console.log("productArr--->", productObject.value);
    };
    const shortNum = () => {
      if (quantity.value > 1) {
        quantity.value -= 1;
        sumprice.value = common_vendor.round(sumprice.value - productObject.value.price, 2);
      }
    };
    const addNum = () => {
      if (quantity.value < productObject.value.inventory) {
        quantity.value += 1;
        sumprice.value = common_vendor.round(sumprice.value + productObject.value.price, 2);
      }
    };
    const formatArr = () => {
      postCartArr.value.splice(0, postCartArr.value.length);
      postCartArr.value.push({
        goodId: totalInfo.value,
        quantity: quantity.value
      });
      console.log(postCartArr.value);
    };
    const wxPay = async (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.showModal({
              content: "支付失败，原因为:" + e.errMsg,
              showCancel: false
            });
            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 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);
        }
      });
    };
    const textAssign = (e) => {
      note.value = e.detail.value;
      console.log("note--->", note.value);
    };
    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: productObject.value.goodImg.split(";")[0],
        g: common_vendor.t(productObject.value.name),
        h: common_vendor.t(productObject.value.type),
        i: common_vendor.t(productObject.value.price),
        j: common_vendor.o(shortNum),
        k: common_vendor.t(quantity.value),
        l: common_vendor.o(addNum),
        m: common_vendor.t(quantity.value),
        n: common_vendor.t(sumprice.value),
        o: common_vendor.t(sumprice.value),
        p: common_vendor.o(textAssign),
        q: common_vendor.t(sumprice.value),
        r: common_vendor.o(createOrder),
        s: common_vendor.sr(popup, "2ef34a39-0", {
          "k": "popup"
        }),
        t: common_vendor.p({
          ["background-color"]: "#fff"
        })
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-2ef34a39"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/order/singleGoodOrder/singleGoodOrder.vue"]]);
wx.createPage(MiniProgramPage);
