const navigationBarHeight = (getApp().statusBarHeight + 50) + 'px'
var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
var app = getApp();
Page({
  data: {
    navigationBarHeight,
    canIUse: tt.canIUse("button.open-type.getPhoneNumber"),
    couponChannelId: "",
    path: null,
    paramData: null,
    signActivity: "",
    mineFlag: null,
    skipUrl: 0,
    fillIndentId: "",
    couponId: "",
    // skip: app.globalData.skip
  },
  onLoad: function (options) {
    var that = this;
    if (options.skipUrl) {
      that.setData({
        skipUrl: options.skipUrl
      })
    }
    if (options.mallTenantId) {
      that.setData({
        mallTenantId: options.mallTenantId
      })
    }
    that.setData({
      path: options.path
    })
    that.setData({
      paramData: options
    })
    if (options && options.fillIndentId && options.couponId) {
      that.setData({
        fillIndentId: options.fillIndentId,
        couponId: options.couponId
      })
    }
    if (options && options.couponChannelId) {
      that.setData({
        couponChannelId: options.couponChannelId
      });
    }

    if (options && options.mineFlag) {
      that.setData({
        mineFlag: options.mineFlag
      })
    }
    if (options && options.signActivity) {
      that.setData({
        signActivity: options.signActivity
      });
    }

    Http.get({
        url: config.api.marketicon,
        data: {
          appId: app.globalData.appId
        }
      }).then(res => {
        that.setData({
          mallImgUrl: res.data.mallImgUrl
        });
        tt.setNavigationBarTitle({
          title: res.data.mallName
        })
      })
      .catch(err => {
        tt.showToast({
          title: err.errMsg,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      })
  },
  onShow: function () {
    tt.checkSession({
      success() {
        //session_key 未过期，并且在本生命周期一直有效
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        console.log("session_key 过期")
        tt.login({
          success: (res) => {
            if (res.code) {
              let usrdata = {
                appId: app.globalData.appId,
                code: res.code,
              }
              Http.post({
                url: config.api.login,
                data: usrdata
              }).then(res => {
                Http.setToken(res.data.token);
                app.globalData.token = res.data.token;
              })
            }
          }
        })
      }
    })
  },
  backHome() {
    console.log("我调用的返回");
    tt.navigateBack({
      delta: 1,
      success: (res) => {

      },
      fail: (res) => {

      },
    });

    // tt.reLaunch({
    //   url: "/pages/index/index",
    //   success: (res) => {
    //   },
    //   fail: (res) => {

    //   },
    // });
  },
  getPhoneNumber: function (e) {
    var that = this;
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    Http.post({
      url: config.api.getUserPhone,
      data: {
        encryptedData: encryptedData,
        iv: iv
      }
    }).then(res => {

      app.globalData.phone = res.data.phone;
      if (that.data.skipUrl == '1') {
        console.log(app.globalData.skip, app.globalData.skipUrl, "跳转地址")
        if (app.globalData.skip == 'redirectTo') {
          tt.redirectTo({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'navigateTo') {
          tt.navigateTo({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'reLaunch') {
          tt.reLaunch({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'switchTab') {
          tt.redirectTo({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'navigateBack') {
          tt.navigateBack({
            delta: 1,
          })
          return;
        }
      }
      //确认订单
      if (that.data.path == "qr") {
        tt.redirectTo({
          url: `/pages/coupon/confirmation/confirmation?couponChannelId=${that.data.couponChannelId}`
        })
      }
      //线上配送
      if (that.data.path == "fi") {
        tt.redirectTo({
          url: `/pages/fillIndent/fillIndent?id=${that.data.fillIndentId}&couponId=${that.data.couponId}`
        })
      }
      // 来着问卷
      if (that.data.path == "wj") {
        tt.redirectTo({
          url: `/pages/questionnaire/questionnaire?id=${that.data.wjId}`
        })
        return;
      }
      /**a
       * 来自我的
       */
      if (that.data.path == 'main') {
        tt.redirectTo({
          url: `/index/user`
        })
        return;
      }
      /**
       * 来自积分兑换
       */
      if (that.data.path == 'jifen') {
        tt.redirectTo({
          url: `/pages/integralmall/payIntegcoupondetail/index?couponChannelId=${that.data.paramData.couponChannelId}&couponId=${that.data.paramData.couponId}`
        })
        return;
      }
      /**
       * 来自拼团详情
       */
      if (that.data.path == 'spell') {
        if (that.data.paramData.avatarUrl) {
          tt.redirectTo({
            url: `/pages/joinFrDpell/index?couponId=${that.data.paramData.couponId}&orderGroupId=${that.data.paramData.orderGroupId}&couponChannelId=${that.data.paramData.couponChannelId}&orderId=${that.data.paramData.orderId}&avatarUrl=${that.data.paramData.avatarUrl}&nickName=${that.data.paramData.nickName}`
          })
        } else {
          tt.redirectTo({
            url: `/pages/spellGroup/mySpellGroup/index?couponId=${that.data.paramData.couponId}&couponChannelId=${that.data.paramData.couponChannelId}`
          })
        }
        return;
      }
      /**
       * fromId若存在来自卡转赠
       */
      if (that.data.path == 'index' && !that.data.paramData.cuserId) {
        tt.redirectTo({
          url: "/pages/game/index?url=" + that.data.paramData.url + "&id=" + that.data.paramData.id + "&gameId=" + that.data.paramData.gameId,
        })
      } else if (that.data.path == 'index' && that.data.paramData.cuserId) {
        tt.redirectTo({
          url: "/pages/coupon/detail/index?cuserId=" + that.data.paramData.cuserId + '&couponChannelId=' + that.data.paramData.couponChannelId + '&coverImg=' + that.data.paramData.coverImg + '&avatarUrl=' + that.data.paramData.avatarUrl + '&userName=' + that.data.paramData.userName + '&couponOrderId=' + that.data.paramData.couponOrderId + "&updateDate=" + that.data.paramData.updateDate + "&mallTenantId=" + that.data.mallTenantId,
        })
      }
      /**
       * 活动报名
       */
      else if (that.data.signActivity) {
        tt.redirectTo({
          url: `/pages/radetail/joinActivity/edit?activityId=${that.data.signActivity}`
        })
      } else if (that.data.mineFlag == 'mine') {
        tt.redirectTo({
          url: '/index/user'
        });
      } else if (that.data.mineFlag == 'index') {
        tt.redirectTo({
          url: app.globalData.goHomeUrl
        });
      } else {
        tt.showToast({
          title: res.data.msg,
          icon: "success",
          success: function (res) {
            if (that.data.couponChannelId) {
              tt.redirectTo({
                url: "/pages/coupon/detail/index?couponChannelId=" + that.data.couponChannelId + "&flag=pay" + "&mallTenantId=" + that.data.mallTenantId,
              })
            } else {
              tt.redirectTo({
                url: '/index/passCar',
              })
            }
          }
        });
      }
    }).catch(err => {
      tt.showToast({
        title: err.message,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  },
});