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

const app = getApp();

Page({
  data: {
  },
  onLoad: function (options) {
    this.setData({
      Containnerheight: main.getWindowHeight(),
    });

    var that = this;
    var hType=0;
    if (options.Type)
      hType=options.Type;
    main.getData("GetAccreditInfo?Type=" + hType, function (data) {
      that.setData({
        Info: data,
        Htype:hType,
      });
    });

    if (!app.globalData.userInfo)
      app.globalData.userInfo = {};

  },
  //得到用户信息
  getUserInfo: function () {
    var that = this

    wx.getUserProfile({
      desc: "登录注册",
      success: function (res2) {
        that.data.Info.Text3=null;
        that.setData({
          Info: that.data.Info,
        });

        app.globalData.userInfo.NickName = res2.userInfo.nickName;
        app.globalData.userInfo.AvatarUrl = res2.userInfo.avatarUrl;
        
        //调用登录接口
        wx.login({
          success: function (res0) {
            app.globalData.userInfo.Code = res0.code;
            that.login(app.globalData.userInfo, res2.userInfo);
          }
        });
      },
      fail: function (res) {
        console.log(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){
        if (that.data.Htype==1){
          main.buildInitData(function () {
            that.gotoReturn();
          });
        }
        else{
          that.gotoReturn();
        }
      }
      else {
        app.globalData.userInfo.UserID = data.UserID;
        that.gotoReturn();
      }
      wx.setStorageSync("AccreditLimitTime",common.formatTime(new Date(),"-",true)+" 23:59:59");
    });
  },
  gotoReturn: function () {
    var that = this;
    if (app.globalData.userInfo.IsShow == 1 && that.data.Info.Text3){
      wx.showModal({
        title: '提醒',
        showCancel: false,
        content: that.data.Info.Text3,
        confirmText: '知道了',
        success(res) {
          if (res.confirm) {
            wx.setStorageSync("IsRemindContinuousNew",1);
          }
        }
      });
    }
    else{
      wx.navigateBack({
        delta: 1
      });
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})