import common from '../../utils/util';
import server from '../../utils/main';

const app = getApp();
var giftID=0;

Page({
  data: {
  },
  onLoad: function (options) {
    giftID = options.GiftID;
    var userID = options.UserID;
    this.setData({
      Containnerheight: common.getSystemHeight(),
      ProgramList: server.getProgramList(),
      CurrentUserID:app.globalData.userInfo.UserID,
    });

    this.init(giftID,userID);

  },
  init: function (id,userID) {
    var that = this;
    server.getData('GetGiftInfoByUserID500?ID=' + id + "&ProductID=" + app.globalData.ProgramID+"&UserID="+userID, function (data) {
      if (data) {
        
        var price = data.PriceTotal;
        var numberWidth = 240;
        var fontsize = 100;
        if (price.toString().length == 2) {
          numberWidth = 180;
        }
        else if (price.toString().length == 3) {
          numberWidth = 220;
        }
        else if (price.toString().length == 4) {
          numberWidth = 220;
        }
        else if (price.toString().length == 5) {
          numberWidth = 240;
          fontsize = 85;
        }

        that.setData({
          NumberWidth: numberWidth,
          GiftInfo: data,
          FontSize:fontsize,
        });
      }
    });
  },
  gotoFeedback: function () {
    server.gotoFeedback();
  },
  goto: function (e) {
    var that = this;

    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              that.getUserInfo();
            },
            fail() {
              wx.openSetting({
                success(res) {
                  that.getUserInfo();
                },
              });
            }
          })
        } else {
          that.getUserInfo();
        }
      }
    });
  },
  //得到用户信息
  getUserInfo: function () {
    var that = this
    //调用登录接口
    wx.login({
      success: function (res0) {
        wx.getUserInfo({
          withCredentials: true,
          success: function (res) {
            app.globalData.userInfo = res.userInfo;
            app.globalData.userInfo.NickName = app.globalData.userInfo.nickName;
            app.globalData.userInfo.AvatarUrl = app.globalData.userInfo.avatarUrl;

            that.setData({
              IsAccredit: true,
              NickName: app.globalData.userInfo.NickName,
              AvatarUrl: app.globalData.userInfo.AvatarUrl
            });

            app.globalData.userInfo.Code = res0.code;
            app.globalData.userInfo.iv = res.iv;
            app.globalData.userInfo.encryptedData = res.encryptedData;
            that.login(app.globalData.userInfo);
          },
          fail: function (res) {
            wx.reLaunch({
              url: '../index/index'
            });
          }
        });
      },
      fail: function () {
        wx.reLaunch({
          url: '../index/index'
        });
      }
    });
  },
  login: function (param) {
    var that = this;
    var url="Login";
    switch(app.globalData.ProgramID){
      case 164:
        url = "MathStar" + url;
        break;
      case 105:
        url = "Math" + url;
        break;
      case 106:
        url = "Hanzi" + url;
        break;
      case 98:
        url = "Pinyin" + url;
        break;
      case 99:
        url = "Phonics" + url;
        break;
    }
    server.postData(url, {
      Code: param.Code,
      NickName: param.nickName,
      Language: param.language,
      Gender: param.gender,
      City: param.city,
      Province: param.province,
      Country: param.country,
      AvatarUrl: param.avatarUrl,
      ProgramVersion: app.globalData.Version,
      Introducer: app.globalData.introducer,
      UserSource: app.globalData.userSource,
      SourceID: app.globalData.SourceID,
      LastUserSource: app.globalData.userSource,
      iv: param.iv,
      encryptedData: param.encryptedData,
    }, function (data) {
      app.globalData.userInfo.UserID = data.UserID;
      wx.showLoading({
        title: '兑换中...',
      });
      server.getData('SetUserGiftExchange500?UserID=' + app.globalData.userInfo.UserID + "&ProductPayInfoID=" + giftID, function (data2) {
        wx.hideLoading();
        if (data2) {
          app.globalData.userInfo.IsMember = 1;
          app.globalData.IsMemberForeverRemind = 1;
          if (that.data.GiftInfo.GiftNumber>1)
            wx.setStorageSync("HasActivate", 1);
          
          wx.reLaunch({
            url: '../../pages/index/index'
          });
        }
        else {
          var title = "无效";
          var content = "兑换卡已被领取，请联系赠送者了解原因！";
          wx.showModal({
            showCancel: false,
            title: title,
            content: content,
            confirmText: "返回首页",
            success: function () {
              wx.reLaunch({
                url: '../../pages/index/index'
              });
            }
          });

        }
      });
    });
  },
  gotoDefault:function(){
    wx.reLaunch({
      url: '../../pages/index/index'
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})