// pages/myinfo/myinfo/myinfo.js
let util = require('../../../utils/util.js');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: util.imgurl, 
    baseImgUrl: util.baseImgUrl,
    books: [],
    isfirst: true,
    nowbook: {}
  },
  gotUserInfo(e) {
    // this.wxlogin(e.detail.userInfo)
    wx.navigateTo({
      url: '/pages/myinfo/login/login',
    })
  },
  bookChange(e) {
    util.query('accountbook/wxapi/setdefault', {
      accountBookId: this.data.books[e.detail.value].id
    }, function(res) {
      if (res.code === 10000) {
        that.getDefaultBook()
      }
    }, 'POST')
  },
  loginout() {
    wx.showModal({
      title: '确认',
      content: '是否确定退出',
      success(res) {
        if (res.confirm) {
          wx.clearStorageSync("userinfo")
          wx.clearStorageSync("token")
          that.setData({
            imageurl: util.imgurl,
            baseImgUrl: util.baseImgUrl,
            books: [],
            isfirst: true,
            nowbook: {},
            user: null,
            uss: null
          })
          util.checklogin(
            function() {
              var ui = wx.getStorageSync('userinfo')
              if (ui) {
                that.setData({
                  user: ui
                })
                that.getBookList()
                that.getDefaultBook()
              }
            }
          )
        } else if (res.cancel) {

        }
      }
    })
  },
  getDefaultBook() {
    util.query('accountbook/wxapi/getdefault', {}, function(res) {
      if (res.code === 10000) {
        var uss = []
        var us = res.data.userInfos
        for (var i = 0; i < parseInt(us.length / 5) + 1; i++) {
          uss[i] = []
          for (var j = 5 * i; j < (i + 1) * 5; j++) {
            if (us[j]) {

              uss[i].push({
                name: us[j].wechatName,
                logo: us[j].wechatIcon
              })
            } else {
              uss[i].push({
                no: 1
              })
            }

          }
        }
        that.setData({
          nowbook: res.data,
          uss: uss
        })
      }
    })
  },
  getBookList() {
    util.query('accountbook/wxapi/list', {
      pageNo: 1,
      pageSize: 9999
    }, function(res) {
      if (res.code === 10000) {
        that.setData({
          books: res.data
        })
      } else {
        that.setData({
          books: []
        })
      }
    })
  },
  toaboutus() {
    wx.navigateTo({
      url: '/pages/myinfo/aboutus/aboutus',
    })
  },
  tosetting() {
    wx.navigateTo({
      url: '/pages/myinfo/sysset/sysset',
    })
  },
  tosuggest() {
    wx.navigateTo({
      url: '/pages/myinfo/suggest/suggest',
    })
  },
  tobookmanage() {
    wx.navigateTo({
      url: '/pages/myinfo/bookmanage/bookmanage',
    })
  },
  tobookmember(e) {
    wx.navigateTo({
      url: '/pages/myinfo/bookmember/bookmember?id=' + e.currentTarget.dataset.id,
    })
  },
  tocreatebook() {
    wx.navigateTo({
      url: '/pages/myinfo/createbook/createbook',
    })
  },
  topay() {
    wx.navigateTo({
      url: '/pages/myinfo/vippage/vippage',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    util.checklogin(
      function() {
        var ui = wx.getStorageSync('userinfo')
        if (ui) {
          ui.validityTime = ui.validityTime ? ui.validityTime.split(' ')[0] : ''
          that.setData({
            user: ui
          })
          that.getBookList()
          that.getDefaultBook()
        }
      }
    )


  },

  wxlogin(ui) {
    wx.login({
      success: function(res) {
        if (res.code) {
          console.log(res.code);
          util.query(
            "/cuser/wxapi/getopenid", {
              code: res.code
            },
            function(da) {
              if (da.code === 10000) {

                if (da.registed) {
                  wx.setStorageSync("token", da.token)
                  wx.setStorageSync("userinfo", da.userinfo)
                  that.setData({
                    user: da.userinfo
                  })
                  that.getBookList()
                  that.getDefaultBook()
                } else {
                  wx.navigateTo({
                    url: '/pages/myinfo/login/login?ui=' + JSON.stringify(ui) + "&token=" + da.token,
                  })
                }
              } else {
                wx.showToast({
                  title: '登录失败',
                  icon: 'none',
                  duration: 2000
                })
              }
            }, "get", 1
          )
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'none',
            duration: 2000
          })
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (!this.data.isfirst) {
      var ui = wx.getStorageSync('userinfo')
      ui.validityTime = ui.validityTime ? ui.validityTime.split(' ')[0] : ''
      if (ui) {
        this.setData({
          user: ui
        })
        this.getBookList()
        this.getDefaultBook()
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isfirst: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      return {
        title: this.data.user.wechatName + '邀请你一起记账',
        path: 'pages/myinfo/joinbook/joinbook?id=' + this.data.nowbook.id + '&uid=' + this.data.user.id
      }
    }
  }
})