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

const app = getApp();

Page({
  data: {
  },
  onLoad: function (options) {
    let that = this;
    app.globalData.SelectedWords=[];
    app.globalData.OCRWords=[];
    if (options) {
      if (options.UserID) {
        console.log("UserID:" + options.UserID);
        app.globalData.introducer = options.UserID;
      }
      if (options.goto) {
        console.log("goto:" + options.goto);
        app.globalData.goto = options.goto;
        if (options.ID)
          app.globalData.goto+="?ID="+options.ID;
        //debugger;
        if (options.Words && options.goto=="selectword"){
          app.globalData.goto="wordsinput?goto="+options.goto;
          app.globalData.OCRWords=options.Words.split(",");
        }
      }
      if (options.SourceID) {
        console.log("SourceID:" + options.SourceID);
        app.globalData.SourceID = options.SourceID;
      }
      // 扫码介绍人
      if (options.scene) {
        var scene = decodeURIComponent(options.scene);
        console.log("scene:" + scene);
        var introducer = 0;
        if (scene.indexOf("UserID") >= 0)
          introducer = scene.substr(scene.indexOf("UserID") + 7);
        console.log("introducer:" + introducer);
        app.globalData.introducer = scene;
      }

      if (options.ProductID) {
        this.setData({
          ProductID: options.ProductID,
          ProductUserID: options.ProductUserID,
        });
      }
    }
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });
    
    this.getUserInfo();
  },
  //得到用户信息
  getUserInfo: function (cb) {
    let that = this
    //调用登录接口
    wx.login({
      success: function (res0) {
        app.globalData.userInfo = {};
        app.globalData.userInfo.Code = res0.code;
        //console.log("Code:" + res0.code);app.globalData.userInfo.nickName = "陌生用户";
        that.login(app.globalData.userInfo);
        
      },
      fail: function () {
        setTimeout(function () {
          that.getUserInfo();
          console.log("reboot:" + new Date().getTime());
        }, 2000);
      }
    });
  },

  login: function (param) {
    let that = this;
    console.log("app.globalData.systemInfo.screenHeight:" + app.globalData.systemInfo.screenHeight);
    console.log("app.globalData.systemInfo.windowHeight:" + app.globalData.systemInfo.windowHeight);
    main.getLocalHost(function () {
      main.postData('YJBDCLogin', {
        Code: param.Code,
        Introducer: app.globalData.introducer,
        UserSource: app.globalData.userSource,
        SourceID: app.globalData.SourceID,
        LastUserSource: app.globalData.userSource,
        Brand: app.globalData.systemInfo.brand,
        Model: app.globalData.systemInfo.model,
        PixelRatio: app.globalData.systemInfo.pixelRatio,
        ScreenWidth: app.globalData.systemInfo.screenWidth,
        ScreenHeight: app.globalData.systemInfo.screenHeight,
        WindowWidth: app.globalData.systemInfo.windowWidth,
        WindowHeight: app.globalData.systemInfo.windowHeight,
        WXLanguage: app.globalData.systemInfo.language,
        WXVersion: app.globalData.systemInfo.version,
        System: app.globalData.systemInfo.system,
        Platform: app.globalData.systemInfo.platform,
        SDKVersion: app.globalData.systemInfo.SDKVersion,
        ProgramVersion: app.globalData.Version,
      }, function (data) {
        //console.log("UserInfo:" + data);
        if (!data) {
          setTimeout(function () {
            that.getUserInfo();
            console.log("reboot:" + new Date().getTime());
          }, 2000);
        }
        else {
          app.globalData.userInfo = data;

          app.globalData.userInfo.IsShow = data.IsShow;
          
          app.globalData.IsLocked = data.IsLocked;
          //app.globalData.userInfo.AvatarUrl = param.avatarUrl;
          //setTimeout(function () {
            wx.redirectTo({
              url: '../main/index'
            })

          //}, 2000);
        }
      });
    });
  },
  
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})