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

const app = getApp();

Page({
  data: {
    fileUrl: app.globalData.fileUrl,
  },
  onLoad: function (options) {
    var text0 = "", text1 = "", text2 = "", text3 = "";
    text0 = "会员登录";
    text1 = "无需注册，微信授权即可登录";
    text2 = "去登录";
    text3 = "注意：会员登录后可使用付费功能。授权微信用户信息即可一键登录。为保障消费权益，购买后请尽快授权用户信息。登录后才能正常使用统计、排行、我的订单、套装、礼品卡等功能。";

    this.setData({
      Height: common.getSystemHeight(),
      Text0: text0,
      Text1: text1,
      Text2: text2,
      Text3: text3,
      OrderType: options.type,
    });

    //适配ipad
    var systemInfo = wx.getSystemInfoSync();
    if (systemInfo.model) {
      if (systemInfo.model.indexOf("iPad") >= 0) {
        this.setData({
          ipad: "ipad",
        })
      }
    }
  },
  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.Code = res0.code;
            app.globalData.userInfo.iv = res.iv;
            app.globalData.userInfo.encryptedData = res.encryptedData;

            app.globalData.userInfo.NickName = res.userInfo.nickName;
            app.globalData.userInfo.AvatarUrl = res.userInfo.avatarUrl;

            that.login(app.globalData.userInfo, res.userInfo);
          },
          fail: function (res) {

          }
        });
      },
      fail: function () {

      }
    });
  },
  login: function (param, param2) {
    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: param2.nickName,
      AvatarUrl: param2.avatarUrl,
      Language: param2.language,
      Gender: param2.gender,
      City: param2.city,
      Province: param2.province,
      Country: param2.country,
      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) {
      wx.navigateBack({
        delta: 1
      });
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})