var config = require("../../config/config.js");
const Http = require("../../utils/HttpBasics");
var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse("button.open-type.getPhoneNumber"),
    couponChannelId: "",
    couponId: "",
    path:null
  },
  onLoad: function (options) {
    if(options.path=='index'){
      this.path = options.path
    }
    var that = this;
    console.log(options);
    if (options.couponChannelId && options.couponId) {
      that.setData({
        couponChannelId: options.couponChannelId,
        couponId: options.couponId
      });
    }


    Http.get({
      url: config.api.marketicon,
      data: {
        appId: config.weapp.AppId
      }
    }).then(res => {
      console.log(res.data.mallImgUrl);
      that.setData({
        mallImgUrl: res.data.mallImgUrl
      });
      wx.setNavigationBarTitle({
        title: res.data.mallName
      })
    });
  },

  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(
      function (res) {
        console.log(res);
        console.log("这是用户授权手机号的结果");
        console.log(that.data.couponChannelId);
        console.log(that.data.couponId);
        console.log("这是从立即支付传回来的值  因为用户没有授权手机号");
        app.globalData.phone = res.data.phone;
        if (that.path == 'index') {
          wx.reLaunch({
            url: "/pages/game/index"
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: "success",
            success: function (res) {
              if (that.data.couponChannelId && that.data.couponId) {
                wx.redirectTo({
                  url: "/pages/coupon/detail/index?couponChannelId=" +
                    that.data.couponChannelId +
                    "&couponId=" +
                    that.data.couponId +
                    "&flag=pay"
                })
              } else {
                wx.switchTab({
                  url: '/pages/passCar/passCar',
                })
              }
            }
          });
        }
      },
      function (error) {
        wx.showToast({
          title: "请重新授权手机号",
          icon: "none"
        });
      }
    );
  }
});