// packageI/pay_back/pay_back.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Interval: null,
    showPacket: false,
    name: '',
    is_cps: '',
    order_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '跳转中',
    });
    console.log(options);
    if (options.is_cps) {
      this.setData({
        is_cps: options.is_cps
      });
    }
    if (options.name) {
      this.setData({
        name: options.name
      });
    }
    if (options.ids) {
      this.setData({
        order_id: options.ids
      });
    }
    if (options.sn) {
      this.setData({
        order_sn: options.sn
      });
    }

  },
  toPacket() {
    wx.navigateTo({
      url: "/packageE/ConsumePacket/ConsumePacketSuccess/ConsumePacketSuccess?order_id=" + this.data.order_id.toString(),
    });
  },
  onClickHide() {
    this.setData({
      showPacket: false,
    });
    this.toRedirect(this.data.redirect);
  },
  getData() {
    let urlStr = app.getNetAddresss('order.merge-pay.check-order-pay');
    app._postNetWork({
      url: urlStr,
      data: {
        order_pay_id: this.data.order_id
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if (res.data.status == 1) {
            this.setData({
              redirect: res.data.redirect
            });
            this.checkoutConsumePacket(res.data.redirect);
            clearInterval(this.data.Interval);
            this.data.Interval = null;
          } else {
            if (!this.data.Interval) {
              this.data.Interval = setInterval(() => {
                this.getData();
              }, 1000);
            }
          }
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {}
    });
  },
  checkoutConsumePacket(redirect) {
    let basicinfo = wx.getStorageSync("yz_basic_info");
    if (basicinfo && basicinfo.globalParameter.consume_red_packet_status == 1) {
      let urlStr = app.getNetAddresss("plugin.consume-red-packet.Frontend.Modules.Packet.Controllers.has.index");
      app._postNetWork({
        url: urlStr,
        data: {
          order_id: this.data.order_id.split(","),
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              showPacket: true,
            });
          } else {
            this.toRedirect(redirect);
          }
        },
        fail: (res) => {
          this.toRedirect(redirect);
        },
      });
    } else {
      this.toRedirect(redirect);
    }
  },
  toRedirect(redirect) {
    if (this.data.is_cps) {
      wx.navigateTo({
        url: "/packageH/cpsPwd/cpsPwd?id=" + this.data.order_id,
      });
      return;
    }
    if (this.data.name == "Myshaky") {
      wx.navigateTo({
        url: "/packageC/Myshaky/activityHome/activityHome",
      });
      return;
    }
    // if (this.data.name == "blindBox") {
    //   // 跳转去订单详情 this.data.orderDetail_id
    //   // wx.redirectTo({
    //   //   url: "/packageA/member/orderdetail/orderdetail?order_id=" + this.data.orderDetail_id + "&orderType=shop",
    //   // });
    //   return;
    // }
    if (this.data.order_sn) {
      this.payCallBack(redirect);
    }
  },
  payCallBack(redirect) {
    if (app.globalData.store_alone_temp == 1) {
      // 开启了门店独立直接跳首页
      wx.reLaunch({
        url: '/packageG/independent_store/index/index?store_id=' + app.globalData.STORE_ID,
      });
      return;
    }
    if (app._isTextEmpty(redirect)) {
      wx.reLaunch({
        url: "/packageG/index/index",
      });
    } else {
      try {
        if (this.data.name == "groups" && redirect.indexOf("group_detail") != -1) {
          // 拼团
          let str = redirect.match(/group_detail\/(\S*)\?i/)[1];
          str = str.split("/");
          let id = str[0];
          if (str[1]) {
            wx.navigateTo({
              url: "/packageB/member/group/GroupDetail/GroupDetail?id=" + id + "&store_id=" + str[1],
            });
          } else {
            wx.navigateTo({
              url: "/packageB/member/group/GroupDetail/GroupDetail?id=" + id,
            });
          }
        } else {
          wx.redirectTo({
            url: redirect,
            fail: (err) => {
              console.log(err);
              wx.reLaunch({
                url: "/packageG/index/index",
              });
            },
          });
        }
      } catch (e) {
        wx.reLaunch({
          url: "/packageG/index/index",
        });
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});