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

const app = getApp();
var urlRedirectTo = "";

Page({
  data: {
    ShowNotice:false,
  },
  onLoad: function (options) {
    urlRedirectTo = options.url;
    this.setData({
      Containnerheight: main.getWindowHeight(),
      IsShow: app.globalData.userInfo.IsShow,
    });

    this.init();

    if (!app.globalData.userInfo)
      app.globalData.userInfo={};
  },
  init: function () {
    var that=this;
    main.getData("GetAccreditInfo?IsShow=" + app.globalData.userInfo.IsShow, function (data) {
      that.setData({
        Info: data,
      });
    });
  },
  goto: function () {
    var that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              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) {
            that.gotoReturn();
          }
        });
      },
    });
  },
  login: function (param, param2) {
    var that = this;
    var url = "MiaoguoLogin";
    main.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) {
      app.globalData.userInfo.NickName = data.NickName;
      app.globalData.userInfo.AvatarUrl = data.AvatarUrl;
      app.globalData.userInfo.Language = data.Language;
      app.globalData.userInfo.Gender = data.Gender;
      app.globalData.userInfo.City = data.City;
      app.globalData.userInfo.Province = data.Province;
      app.globalData.userInfo.Country = data.Country;
      
      app.globalData.userInfo.IsMember=data.IsMember;

      if (app.globalData.userInfo.UserID)
        main.buildInitData(function(){
          that.gotoReturn();
        });
      else{
        app.globalData.userInfo.UserID = data.UserID;
        that.gotoReturn();
      }
    });
  },
  gotoReturn:function(){
    wx.navigateBack({
      delta: 1
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})