"use strict";
const common_vendor = require("../../../common/vendor.js");
const api_request = require("../../../api/request.js");
const _sfc_main = {
  __name: "mineorders",
  setup(__props) {
    const Status = common_vendor.ref({
      orderStatus: "全部",
      orders: [],
      displayedOrders: []
    });
    common_vendor.ref(0);
    var plugin = requirePlugin("logisticsPlugin");
    let waybillToken = "";
    const isSelected = (tab) => Status.value.orderStatus === tab;
    common_vendor.onMounted(async () => {
      await Getorder();
      if (Status.value.orderStatus === "全部") {
        Status.value.displayedOrders = Status.value.orders;
        console.log("Status.value.displayedOrders--->", Status.value.displayedOrders);
      } else {
        Status.value.displayedOrders = Status.value.orders.filter((order) => {
          switch (Status.value.orderStatus) {
            case "待支付":
              return order.orderStatus === "待支付";
            case "待发货":
              return order.orderStatus === "待发货";
            case "已发货":
              return order.orderStatus === "待收货";
            case "待收货":
              return order.orderStatus === "已退款";
            default:
              return false;
          }
        });
      }
      getFonts();
    });
    common_vendor.wx$1.getStorageSync("userInfo");
    const Getorder = async () => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/order/list",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        }
      });
      if (res.data.code === 1) {
        Status.value.orders = res.data.data;
        console.log("Status.value.orders--->", Status.value.orders);
      } else {
        console.log("没拿到用户数据");
      }
    };
    const zt = common_vendor.ref({
      tab: ""
    });
    common_vendor.onLoad((options) => {
      zt.value.tab = JSON.parse(options.zt);
      changeTab(zt.value.tab);
    });
    const changeTab = (tab) => {
      if (tab === "全部") {
        Status.value.displayedOrders = Status.value.orders;
        Status.value.orderStatus = "全部";
      } else {
        Status.value.displayedOrders = Status.value.orders.filter((order) => {
          switch (tab) {
            case "待支付":
              return order.orderStatus === "待支付";
            case "待发货":
              return order.orderStatus === "待发货";
            case "待收货":
              return order.orderStatus === "待收货";
            case "已退款":
              return order.orderStatus === "已退款";
            default:
              return false;
          }
        });
        Status.value.orderStatus = tab;
      }
    };
    const goToText = (orderInfo) => {
      console.log(orderInfo);
      common_vendor.index.navigateTo({
        url: "../../../pages/order/product-paysuccess/product-paysuccess?oid=" + JSON.stringify(orderInfo.id)
      });
    };
    const cancelOrder = async (order) => {
      console.log(order);
      common_vendor.index.showModal({
        title: "提示",
        content: "确定要取消订单吗?",
        success: (e) => {
          if (e.confirm) {
            postCancelOrder(order);
          } else if (e.cancel) {
            return;
          }
        }
      });
    };
    const goToPay = async (order) => {
      wxPay(order.id);
    };
    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) {
          }
        });
      } catch (error) {
        console.error("支付请求失败", error);
        common_vendor.index.showModal({
          content: "支付请求失败,请重试。",
          showCancel: false
        });
      }
    };
    const postCancelOrder = async (order) => {
      const res = await common_vendor.index.request({
        url: api_request.baseUrl + "/order/cancel/id",
        method: "POST",
        header: {
          cookie: common_vendor.wx$1.getStorageSync("cookie")
        },
        data: { id: order.id }
      });
      console.log("取消订单的信息--->", res.data);
      if (res.data.code === 1) {
        Getorder();
        common_vendor.index.redirectTo({
          url: "/pages/mine/mineorders/mineorders?zt=" + JSON.stringify(Status.value.orderStatus)
        });
      }
    };
    const viewLogistics = (oid) => {
      common_vendor.index.request({
        url: api_request.baseUrl + "/logistics/get/info",
        method: "POST",
        data: {
          id: oid
        },
        header: {
          "cookie": common_vendor.wx$1.getStorageSync("cookie")
        },
        success: (res) => {
          console.log(res.data.data);
          waybillToken = res.data.data.waybill_token;
          plugin.openWaybillTracking({
            waybillToken
          });
        }
      });
    };
    const deleteOrder = async () => {
      console.log("删除订单方法");
    };
    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 confirm = () => {
      console.log("确认收货方法");
    };
    return (_ctx, _cache) => {
      return {
        a: _ctx.orderStatus === "全部" ? 1 : "",
        b: isSelected("全部") ? 1 : "",
        c: common_vendor.o(($event) => changeTab("全部")),
        d: _ctx.orderStatus === "待支付" ? 1 : "",
        e: isSelected("待支付") ? 1 : "",
        f: common_vendor.o(($event) => changeTab("待支付")),
        g: _ctx.orderStatus === "待发货" ? 1 : "",
        h: isSelected("待发货") ? 1 : "",
        i: common_vendor.o(($event) => changeTab("待发货")),
        j: _ctx.orderStatus === "待收货" ? 1 : "",
        k: isSelected("待收货") ? 1 : "",
        l: common_vendor.o(($event) => changeTab("待收货")),
        m: _ctx.orderStatus === "已退款" ? 1 : "",
        n: isSelected("已退款") ? 1 : "",
        o: common_vendor.o(($event) => changeTab("已退款")),
        p: common_vendor.f(Status.value.displayedOrders, (order, index, i0) => {
          return common_vendor.e({
            a: common_vendor.t(order.orderNumber),
            b: common_vendor.t(order.orderStatus),
            c: common_vendor.f(order.orderItemList, (item, goodIndex, i1) => {
              return {
                a: item.goodSnapshot.goodImg.split(";")[0],
                b: common_vendor.t(item.goodSnapshot.name),
                c: common_vendor.t(item.goodSnapshot.type),
                d: common_vendor.t(item.quantity),
                e: common_vendor.t(item.goodSnapshot.price),
                f: goodIndex
              };
            }),
            d: common_vendor.t(order.totalAmount),
            e: order.orderStatus === "待支付"
          }, order.orderStatus === "待支付" ? {
            f: common_vendor.o(($event) => cancelOrder(order), order.id),
            g: common_vendor.o(($event) => goToPay(order), order.id)
          } : {}, {
            h: order.orderStatus === "待发货"
          }, order.orderStatus === "待发货" ? {} : {}, {
            i: order.orderStatus === "待收货"
          }, order.orderStatus === "待收货" ? {
            j: common_vendor.o(($event) => viewLogistics(order.id), order.id),
            k: common_vendor.o(($event) => confirm(), order.id)
          } : {}, {
            l: order.orderStatus === "已退款" || order.orderStatus === "交易关闭"
          }, order.orderStatus === "已退款" || order.orderStatus === "交易关闭" ? {
            m: common_vendor.o(($event) => deleteOrder(), order.id)
          } : {}, {
            n: common_vendor.o(() => {
            }, order.id),
            o: order.id,
            p: common_vendor.o(($event) => goToText(order), order.id)
          });
        })
      };
    };
  }
};
const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7d0f2622"], ["__file", "D:/微信小程序/课设整合/匠承非遗小程序端/jiangchengfeiyi-xiaochengxu/pages/mine/mineorders/mineorders.vue"]]);
wx.createPage(MiniProgramPage);
