const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
var app = getApp();
Page({
  data: {
    navigationBarHeight,
    canIUse: wx.canIUse("button.open-type.getPhoneNumber"),
    couponChannelId: "",
    path: null,
    paramData: null,
    signActivity: "",
    mineFlag: null,
    skipUrl: 0,
    fillIndentId: "",
    couponId: "",
    go: '',
    id: '',
    isPhone: false
    // skip: app.globalData.skip
  },

  onLoad: function (options) {
    console.log(options, 'options');
    var that = this;
    console.log(options.skipUrl, "options.skipUrl")
    if (options.skipUrl) {
      that.setData({
        skipUrl: options.skipUrl
      })
    }

    if (options.id) {
      that.setData({
        id: options.id
      })
    }

    if (options.go) {
      that.setData({
        go: options.go
      })
    }

    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: config.weapp.AppId
      }
    }).then(res => {
      that.setData({
        mallImgUrl: res.data.mallImgUrl
      });
      wx.setNavigationBarTitle({
        title: res.data.mallName
      })
    }).catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  },
  onShow: function () {
    wx.checkSession({
      success() {
        //session_key 未过期，并且在本生命周期一直有效
      },
      fail() {
        // session_key 已经失效，需要重新执行登录流程
        console.log("session_key 过期")
        wx.login({
          success: (res) => {
            if (res.code) {
              let usrdata = {
                appId: config.weapp.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;
              })
            }
          }
        })
      }
    })
  },

  changePhoneStatus() {
    const isPhone = this.data.isPhone
    this.setData({
      isPhone: !isPhone
    })
  },

  backHome: function () {
    wx.switchTab({
      url: app.globalData.goHomeUrl,
    })
  },
  getPhoneNumber: function (e) {
    if (!this.data.isPhone) {
      wx.showToast({
        title: '请您阅读并勾选用户协议哦',
        icon: "none"
      })
      return
    }
    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') {
          wx.redirectTo({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'navigateTo') {
          wx.navigateTo({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'reLaunch') {
          wx.reLaunch({
            url: app.globalData.skipUrl,
          })
          return;
        } else if (app.globalData.skip == 'switchTab') {
          wx.switchTab({
            url: app.globalData.skipUrl,
          })
          return;
        }
      }
      //线上配送
      if (that.data.path == "fi") {
        wx.redirectTo({
          url: `/pages/fillIndent/fillIndent?id=${that.data.fillIndentId}&couponId=${that.data.couponId}`
        })
      }
      // 来着问卷
      if (that.data.path == "wj") {
        wx.redirectTo({
          url: `/pages/questionnaire/questionnaire?id=${that.data.wjId}`
        })
        return;
      }
      /**a
       * 来自我的
       */
      if (that.data.path == 'main') {

        wx.switchTab({
          url: `/index/user`
        })
        return;
      }
      /**
       * 来自积分兑换
       */
      if (that.data.path == 'jifen') {
        wx.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) {
          wx.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 {
          wx.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) {
        wx.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) {
        wx.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
        })
      }
      /**
       * 活动报名
       */
      else if (that.data.signActivity) {
        wx.redirectTo({
          url: `/pages/radetail/joinActivity/edit?activityId=${that.data.signActivity}`
        })
      } else if (that.data.mineFlag == 'mine') {
        wx.switchTab({
          url: '/index/user'
        });
      } else if (that.data.mineFlag == 'passCar') {
        wx.switchTab({
          url: '/index/passCar'
        });
      } else if (that.data.mineFlag == 'index') {
        wx.switchTab({
          url: app.globalData.goHomeUrl
        });
      } else if (that.data.path == 'exchangeCard') {
        wx.navigateTo({
          url: `/pages3/exchangeCard/exchangeCard`
        })
      } else if (that.data.path == 'searchbarDetail') {
        wx.navigateTo({
          url: `/pages/index/searchbar/detail/index?id=${that.data.id}`
        })
      } else if (that.data.path == 'activityCalendar') {
        wx.navigateTo({
          url: `/pages/activityCalendar/activityCalendar`
        })
      } else if (that.data.path == 'integralmall') {
        wx.navigateTo({
          url: `/pages/integralmall/index`
        })
      } else if (that.data.path == 'questionnaire') {
        wx.navigateTo({
          url: `/pages/questionnaire/questionnaire`
        })
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: "success",
          success: function (res) {
            if (that.data.couponChannelId) {
              wx.redirectTo({
                url: "/pages/coupon/detail/index?couponChannelId=" + that.data.couponChannelId + "&flag=pay"
              })
            } else {
              if (that.data.paramData.go) {
                wx.navigateTo({
                  url: '/pages3/setUserInfo/index',
                })
              } else {
                const ifSetUserInfo = wx.getStorageSync('ifSetUserInfo') || 0
                if (ifSetUserInfo) {
                  wx.switchTab({
                    url: '/index/user'
                  });
                } else {
                  wx.switchTab({
                    url: '/index/passCar',
                  })
                }
              }
            }
          }
        });
      }
    }).catch(err => {
      wx.showToast({
        title: '授权失败',
        icon: 'error',
        duration: 2000,
        mask: false
      });
    })
  },

  goCheckProtocolI() {
    wx.navigateTo({
      url: '/pages3/userProtocol/userProtocol',
    })
  },

  goCheckProtocolII() {
    wx.navigateTo({
      url: '/pages3/privateProtocol/privateProtocol',
    })
  },
});