"use strict";
const common_vendor = require("../../../common/vendor.js");
const utils_emitter = require("../../../utils/emitter.js");
const api_request = require("../../../api/request.js");
const common_assets = require("../../../common/assets.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) {
    common_vendor.ref(1);
    common_vendor.ref(138);
    common_vendor.ref(1);
    common_vendor.ref(120);
    common_vendor.ref(50);
    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 = wx.getStorageSync("userInfo");
    common_vendor.ref({});
    const totalInfo = common_vendor.ref([]);
    const note = common_vendor.ref("");
    common_vendor.onMounted(() => {
      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 = wx.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: wx.getStorageSync("cookie")
        },
        data: { id: userInfo.value.id }
      });
      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);
      const resOrder = await common_vendor.index.request({
        //向后端发送生成订单请求
        url: api_request.baseUrl + "/order/add",
        method: "POST",
        header: {
          cookie: wx.getStorageSync("cookie")
        },
        data: {
          userId: userInfo.id,
          orderType: productArr.value[0].goodVO.isGoodType ? "product" : "service",
          userName: userInfo.userName,
          orderNumber: "null",
          addressId: addressRealInfo.value.id,
          //地址信息id
          // contactsId: null, //联系人信息id
          // couponId: null, //优惠卷id
          totalAmount: sumprice.value,
          //实付价格
          orderStatus: "待支付",
          note: note.value,
          orderItemMainInfoAddRequestList: common_vendor.toRaw(totalInfo.value)
        }
      });
      console.log("后台返回订单响应==>", resOrder);
      if (resOrder.data.code === 1) {
        common_vendor.index.navigateTo({
          url: "/pages/mine/OrderDetails/OrderDetails"
        });
      } else {
        common_vendor.index.showToast({
          icon: "error",
          title: "购买失败，请求错误"
        });
        return;
      }
    };
    const getProduct = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/cart/cart/list",
        method: "POST",
        header: {
          cookie: wx.getStorageSync("cookie")
        },
        data: JSON.stringify(totalInfo.value)
      });
      if (res.data.code === 1) {
        productArr.value = res.data.data;
        console.log("productArr--->", productArr.value);
        productArr.value.forEach((item) => {
          sumprice.value += item.goodVO.price * item.quantity;
        });
      }
    };
    const shortNum = (index) => {
      if (productArr.value[index].quantity > 1) {
        productArr.value[index].quantity -= 1;
        sumprice.value -= productArr.value[index].goodVO.price * 1;
      }
    };
    const addNum = (index) => {
      if (productArr.value[index].quantity < productArr.value[index].goodVO.inventory) {
        productArr.value[index].quantity += 1;
        sumprice.value += productArr.value[index].goodVO.price * 1;
      }
    };
    return (_ctx, _cache) => {
      return {
        a: common_vendor.t(addressRealInfo.value.region),
        b: common_vendor.unref(common_assets.dingwei$1),
        c: common_vendor.t(addressRealInfo.value.detailAddress),
        d: common_vendor.unref(common_assets.you_cheng),
        e: common_vendor.o(($event) => loadPop()),
        f: common_vendor.t(addressRealInfo.value.name),
        g: common_vendor.t(addressRealInfo.value.phone),
        h: common_vendor.f(productArr.value, (item, index, i0) => {
          return {
            a: item.goodVO.goodImg,
            b: common_vendor.t(item.goodVO.name),
            c: common_vendor.t(item.goodVO.type),
            d: common_vendor.t(item.goodVO.price),
            e: common_vendor.o(($event) => shortNum(index)),
            f: common_vendor.t(item.quantity),
            g: common_vendor.o(($event) => addNum(index)),
            h: index
          };
        }),
        i: common_vendor.unref(common_assets.short),
        j: common_vendor.unref(common_assets.add),
        k: common_vendor.unref(common_assets.yiwen),
        l: common_vendor.t(productArr.value.length),
        m: common_vendor.unref(common_assets.rmb_36px),
        n: common_vendor.t(sumprice.value),
        o: note.value,
        p: common_vendor.o(($event) => note.value = $event.detail.value),
        q: common_vendor.unref(common_assets.rmb_huang),
        r: common_vendor.t(sumprice.value),
        s: common_vendor.o(createOrder),
        t: () => ({
          r: popup,
          k: "popup"
        }),
        v: common_vendor.o(_ctx.change),
        w: 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"]]);
my.createPage(MiniProgramPage);
