var config = require("../../config/config.js");
var app = getApp();
const Http = require("../../utils/HttpBasics");
Page({
  data: {
    canIUse: wx.canIUse("button.open-type.getUserInfo"),
    path:null,
    couponChannelId: null,
    couponId:null,
  },
  onLoad: function(options) {
    var that = this;
    if (options.couponChannelId && options.couponId){
      that.setData({
        couponChannelId: options.couponChannelId,
        couponId: options.couponId,
      })
    }
    if (options.path == 'index') {
      that.path = options.path
    }
    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
      });
    })
  },

  bindGetUserInfo: function(e) {
    let that=this;
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    Http.post({
      url: config.api.getUserInfo,
      data: {
        encryptedData: encryptedData,
        iv: iv
      }
    })
    .then(
      function(res) {
        console.log(res)
        if(that.path=='index'){
          wx.reLaunch({
            url: "/pages/game/index"
          });
        }
        else{
          //返回主页
          if (that.data.couponChannelId && that.data.couponId){
            wx.redirectTo({
              url: `/pages/coupon/detail/index?couponChannelId=${that.data.couponChannelId}&couponId=${that.data.couponId}`
            });
          }else{
            wx.switchTab({
              url: "/pages/index/index"
            });
          }
        }
      },
      function(error) {
        console.log(error)
        wx.showToast({
          title: "请授权个人信息",
          icon: "none"
        });
      }
    )
    .catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000,
        mask: false
      });
    })
  }
});
