var e = getApp();

Page({
    data: {
        isVip: false,
        startTime: "--",
        endTime: "--",
        selectedType: null,
        cards: [],
        isQuota: false,
        show: false,
        isIOS: false, // 是否为 iOS 系统
    },
    onLoad: function (e) {
      const that = this;
      getApp().check_status()
          .then(function(res) {
              that.setData({
                  show: res.data.data.show
              });
              if (!that.data.show) {
                wx.showModal({
                    title: "提示",
                    content: "iOS 暂时无法使用此功能，请稍后再试。",
                    showCancel: false,
                    confirmText: "知道了",
                    success: () => {
                        // 可选：用户确认后返回上一级页面
                        wx.reLaunch({
                          url: "/pages/index/index",
                        })
                    }
                });
                return; // 停止执行后续逻辑
            }
          })
          .catch(function(err) {
              console.error("获取文章信息失败:", err);
          })
          .finally(function() {
              console.log("check_status调用完成");
          });
        // 使用 wx.getDeviceInfo() 检测系统是否为 iOS



        // 非 iOS 系统，继续加载页面逻辑
        var t = wx.getStorageSync("cards").filter(function (t) {
            return t.is_quota === JSON.parse(e.isQuota || "false");
        });

        console.log("renderCards", t);
        this.setData({
            cards: t,
            isQuota: JSON.parse(e.isQuota || "false"),
        });
    },


    onRecharge: function (e) {
        this.setData({
            selectedType: e.currentTarget.dataset.type,
        });
    },
    // open_button: function (e) {
    //     // 如果是 iOS，弹出提示框（备用逻辑）
    //     if (this.data.isIOS) {
    //         wx.showModal({
    //             title: "提示",
    //             content: "iOS 暂时无法使用此功能，请稍后再试。",
    //             showCancel: false,
    //             confirmText: "知道了",
    //         });
    //         return;
    //     }

    //     // 非 iOS 正常跳转到支付小程序
    //     var t = e.currentTarget.dataset.type,
    //         n = e.currentTarget.dataset.price;
    //     this.setData({
    //         selectedType: t,
    //     }),
    //         console.log(t, n),
    //         wx.navigateToMiniProgram({
    //             appId: "wxbe54dfa3311e0443",
    //             path: "pages/payment/payment?type="
    //                 .concat(t, "&externalOpenid=")
    //                 .concat(wx.getStorageSync("openid"), "&total_fee=")
    //                 .concat(n, "&transaction_type=member&isDebug=false"),
    //             envVersion: "release",
    //             success: function (e) {
    //                 console.log("跳转到支付小程序成功", e);
    //             },
    //             fail: function (e) {
    //                 console.error("跳转到支付小程序失败", e);
    //             },
    //         });
    // },
    open_button: function(t) {
      var n = t.currentTarget.dataset.type, o = t.currentTarget.dataset.price;
      this.setData({
          selectedType: n
      }), console.log(t), e.apiRequest({
          url: "/myapp/wx_pay/",
          method: "POST",
          data: {
              type: n,
              openid: wx.getStorageSync("openid"),
              total_fee: o,
              transaction_type: "member"
          },
          success: function(t) {
              console.log(t), t.data && t.data.paySign ? wx.requestPayment({
                  timeStamp: t.data.timeStamp,
                  nonceStr: t.data.nonceStr,
                  package: t.data.package,
                  signType: "RSA",
                  paySign: t.data.paySign,
                  success: function(t) {
                      e.getinfo().then(function() {
                          console.log(wx.getStorageSync("cards")), wx.navigateBack({
                              delta: 1,
                              success: function(e) {
                                  console.log("返回上一页成功");
                              }
                          });
                      }).catch(function(e) {
                          console.error("An error occurred:", e);
                      }), console.log(t), wx.showToast({
                          title: "支付成功",
                          icon: "success"
                      });
                  },
                  fail: function(e) {
                      wx.showToast({
                          title: "支付失败",
                          icon: "error"
                      });
                  }
              }) : wx.showToast({
                  title: "创建订单失败",
                  icon: "error"
              });
          }
      });
  },
    onReady: function () {},
    onShow: function () {
        var t = this;
        e.getUserInfo()
            .then(function () {
                t.setData({
                    isVip: wx.getStorageSync("isMember"),
                    startTime: t.formatEndTime(wx.getStorageSync("startTime")),
                    endTime: t.formatEndTime(wx.getStorageSync("endTime")),
                });
            })
            .catch(function (e) {
                console.error("获取用户信息失败:", e);
            })
            .finally(function () {
                console.log("getUserInfo调用完成");
            });

            //

    },
    formatEndTime: function(e) {
        // 将秒时间戳转换为毫秒时间戳
        var date = new Date(e * 1000);

        // 格式化为“YYYY年MM月DD日”
        var year = date.getFullYear();
        var month = String(date.getMonth() + 1).padStart(2, "0"); // 月份从 0 开始，需要 +1
        var day = String(date.getDate()).padStart(2, "0");

        var formattedDate = `${year}-${month}-${day}`;

       return formattedDate

  },
    onHide: function () {},
    onUnload: function () {},
    onPullDownRefresh: function () {},
    onReachBottom: function () {},
    onShareAppMessage: function () {},
});
