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

const app = getApp();

Page({
  data: {
  },
  onLoad: function (options) {
    var that = this;

    if (options && options.type && options.type == "bebound") {
      var bBind = 0;
      //console.log("userSource3:" + app.globalData.userSource);
      

      var arrUserSource = [1007, 1037, 1089, 1006, 1001, 1005, 1048, 1012, 1024, 1042, 1011, 1106, 1035, 1047, 1020, 1013, 1053];
      for (var i = 0; i < arrUserSource.length; i++) {
        if (app.globalData.userSource == arrUserSource[i]) {
          bBind = 1;
          break;
        }
      }
      if (options && options.BindTime) {
        var time = new Date(options.BindTime);
        var timeNow = new Date();
        var sub = common.diffDate("s", time, timeNow);

        if (app.globalData.userSource==1006) {
          if (sub > 86400) {
            bBind = -1;
          }
        }
        else{
          if (sub > 180) {
            bBind = -1;
          }
        }
      }

      if (bBind == 1) {
        that.setData({
          ShowPanel: "bebound",
          ParentUserID: options.ParentUserID,
          NickName: options.NickName,
        });
        that.download(options.AvatarUrl);
      }
      else {
        if (bBind == -1) {
          wx.showModal({
            title: '请求时间过期',
            content: '请联系邀请人，再次私聊发给您。',
            showCancel: false,
            confirmText: "知道了",
            success: function () {
              wx.reLaunch({
                url: '../index/index',
              });
            }
          });
        }
        else {
          wx.showModal({
            title: '无效邀请',
            content: '发群无效。请联系邀请人，单独私聊发给您。',
            showCancel: false,
            confirmText: "知道了",
            success: function () {
              wx.reLaunch({
                url: '../index/index',
              });
            }
          });
        }
      }
    }
    else {
      main.getData("GetUserBindingList?UserID=" + app.globalData.userInfo.UserID, function (data) {
        if (data && data.length > 0) {
          that.setData({
            List: data,
            ShowPanel: "unbinding",
          });
        }
        else {
          that.setData({
            ShowPanel: "binding",
          });
        }
      });
    }

    that.setData({
      Containnerheight: main.getWindowHeight(),
    });
  },
  download: function (avatarUrl) {
    var that = this;
    wx.downloadFile({
      url: avatarUrl,
      success(res) {
        if (res.statusCode === 200) {
          that.setData({
            AvatarUrl: res.tempFilePath,
          });
        }
      }
    });
  },
  onShow: function (options) {
    var that = this;
    if (app.globalData.userInfo.IsShow == 1) {
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userInfo']) {

          }
          else {
            wx.navigateTo({
              url: '../index/accredit',
            });
          }
        }
      });
    }
  },
  shareFinished: function () {
    setTimeout(function () {
      wx.showModal({
        title: '等待对方接受邀请',
        content: '请尽快与对方联系确认邀请',
        showCancel: false,
        confirmText: "知道了",
      });
    }, 3000);
  },
  gotoBinding: function () {
    var that = this;
    main.getData("UpdateUserMemberInfo?UserID=" + app.globalData.userInfo.UserID + "&ParentUserID=" + this.data.ParentUserID, function (data) {
      wx.reLaunch({
        url: '../index/index',
      })
    });
  },
  gotoUnBinding: function () {
    var that = this;
    main.getData("UpdateUserBinding?ParentUserID=" + app.globalData.userInfo.UserID, function (data) {
      wx.reLaunch({
        url: '../index/index',
      })
    });
  },
  onShareAppMessage: function () {
    if (this.data.ShowPanel == "binding") {
      var time = common.formatTime(new Date());
      var path = '/pages/index/index?type=bebound&ParentUserID=' + app.globalData.userInfo.UserID + '&BindTime=' + time;
      path += "&NickName=" + app.globalData.userInfo.NickName;
      path += "&AvatarUrl=" + app.globalData.userInfo.AvatarUrl;
      //console.log(path);
      return {
        title: "帐号绑定 " + time,
        path: path,
        imageUrl: app.globalData.uploadImageUrl + "web/program_screenshot_bindaccount.png",
      }
    }
  },
})
