const util = require('../../utils/util.js');
const app = getApp();
Page({
  data: {
    userInfo: app.globalData.userInfo,
    config: util.config,
    isWallet: 0,
    isGrowup: 0,
    stat: {},
    levelName: '普通',
    flag: "",
    isShow: "1",
    tabbar: {}
  },
  onLoad(options) {
    app.editTabbar();
    let info = wx.getStorageSync('WXuserInfo')
    console.log(info);
    if (!info) {
      this.logion()
    }

    console.log(options);
    if (options.flags) {
      this.setData({
        flag: "false"
      })
    } else {
      this.setData({
        flag: ""
      })
    }
    // this.updateUserInfo();

  },
  onUnload(e) {
    console.log(e);
    this.setData({
      flag: "true"
    })
  },
  onShow() {
    this.setData({ userInfo: app.globalData.userInfo });
    if (this.data.isShow == "1") {
      this.updateUserInfo()
    }



    // this.updateUserInfo();
  },
  // 跳转至宝贝币中心
  jumpPage() {
    if (this.data.userInfo) {
      wx.navigateTo({
        url: '/pages/account/monetaryCenter/index',
      })
    }
  },

  isWallet: function () {
    let that = this, userInfo = app.globalData.userInfo;
    util.ajax({
      func: "v2/user/showCoin",
      data: { "rid": userInfo ? userInfo['rid'] : '' },
      load: false
    }, function (res) {
      if (res.code == 0) {
        that.setData({ isWallet: parseInt(res.data) });
      }
    }, false);
  },
  stat() {
    let that = this;
    util.stat().then(res => {
      let data = util.getCoinLevel(res.allCoin)
      that.setData({ stat: res, levelName: data.levelName });
    })
  },
  isGrowupCard: function () {
    let that = this, userInfo = app.globalData.userInfo;
    util.ajax({
      func: "v2/user/growthCard/center",
      data: { "rid": userInfo ? userInfo['rid'] : '' },
      load: false
    }, function (res) {
      if (res.code == 0) {
        that.setData({ isGrowup: res.data });
      }
    }, false);
  },
  updateUserInfo() {
    let that = this
    let self = this, userInfo = app.globalData.userInfo;
    console.log(userInfo);
    if (util.isObjEmpty(userInfo)) return false;
    util.ajax({
      func: "user/stat_info",
      load: false
    }, function (res) {
      console.log(res.data);
      if (res.code == 0 && res.data) {
        if (self.data.flag == "false") {
          if (res.data.roleType == "sharer") {
            wx.navigateTo({
              url: `/pages/sharingHome/index?organStatus=${2}`,
            })

          } else if (res.data.roleType == "organ") {
            wx.navigateTo({
              url: `/pages/sharingHome/index?organStatus=${1}`,
            })
          } else {
            wx.navigateTo({
              url: '/pages/public/swiper/web?url=https://mp.weixin.qq.com/s/H8WuXW_VeCeWtxQXFBGu2Q&title=宝贝走天下',
            })
          }
          that.setData({
            isShow: "2"
          })
        }
        self.isWallet();
        self.isGrowupCard();
        self.stat();
        res.data.score = Math.round(res.data.score);
        userInfo['role'] = util.userRole(res.data.score);
        userInfo['score'] = res.data.score;
        userInfo['coin'] = res.data.coin;
        userInfo['membered'] = res.data.membered;
        userInfo['roleType'] = res.data.roleType || 'normal';
        userInfo['roleTypeDisable'] = res.data.roleTypeDisable || 0;
        userInfo['babyCoin'] = util.toMoney(res.data.coin, ",");
        userInfo['weixin'] = res.data.weixin || 0;
        app.globalData.userInfo = userInfo;
        wx.setStorageSync("WXuserInfo", userInfo);
        self.setData({ userInfo });
        console.log(self.data.flag);


      }
    });
  },
  navigatorURl(e) {
    let self = this;
    // if (e.currentTarget.dataset.url == '/pages/account/growthCourse/index') {
    //   util.ajax({
    //     func: "v2/gwcourse/isAttended"
    //   }, function (res) {
    //     if (res.code == 0) {
    //       if (res.data ==1) {
    //         util.navigator(e.currentTarget.dataset.url);
    //       }else{
    //         wx.showModal({
    //           content: '购买成长课活动后才可预约成长课',
    //           showCancel:false,
    //           success (res) {
    //             if (res.confirm) {
    //               console.log('用户点击确定')
    //             }
    //           }
    //         })
    //       }
    //     } else{
    //       util.showTips(res.reason);
    //     }
    //   });
    // }else{
    //   util.navigator(e.currentTarget.dataset.url);
    // }
    util.navigator(e.currentTarget.dataset.url);
  },
  logion() {
    let that = this
    util.silentLogin().then(res => {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    })
  },
  copy(e) {
    let code = e.currentTarget.dataset.code;
    wx.setClipboardData({
      data: code
    });
  },
  logout() {
    let self = this;
    wx.showModal({
      content: '确定登出吗？',
      success: function (res) {
        if (res.confirm) {
          util.ajax({
            func: "login_out"
          }, function (res) {
            if (res.code == 0) {
              wx.removeStorageSync("WXuserInfo");
              app.globalData.userInfo = null;
              self.setData({ userInfo: app.globalData.userInfo, isGrowup: 0, isWallet: 0, stat: {} });
            } else
              util.showTips(res.reason);
          });
        }
      }
    });
  },
  onShareAppMessage() {
    return {
      title: '个人中心-我的信息',
      path: '/pages/home/account'
    }
  }
})