//index.js
//获取应用实例
import http from '../../utils/http'
import api from '../../utils/api'
import util from '../../utils/util'

const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    keyword: '',
    page: 1,
    list: [],
    touchBottom: false,
    notReadCount: 0,
    topRole: null
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onShow: function () {
    this.getTabBar().init();
    this.setData({
      list: []
    })
    this.search();
    api.getByName(this, 'notifications/notReadCount', 'notReadCount');
    var that = this;
    api.getByName(this, 'users/getTopRole', 'topRole', {}, function (res) {
      var role = that.data.topRole
      if (role && role.key == 'leader') {
        wx.switchTab({
          url: '/pages/account/index',
        })
      }
    });
    api.getByName(this, 'getUserInfo', 'userInfo', {}, function (res) {
      app.updateUserInfo(res);
      that.getTabBar().init();
    });
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  navigate: function (e) {
    var url = e.currentTarget.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  updateInput: function (e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      [name]: e.detail.value
    })
  },
  search: function () {
    this.setData({
      list: [],
      page: 1
    })
    this.getList()
  },
  getList: function () {
    // if(this.data.touchBottom) return false;

    var that = this
    http({
      url: 'projects/get',
      data: {
        page: this.data.page,
        name: this.data.keyword
      },
      success: function (res) {
        if (res.code == 0) {
          var list = that.data.list.concat(res.data)
          that.setData({
            touchBottom: res.data.length == 0,
            list: list
          })
        }
      }
    })
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onReachBottom: function () {
    if (!this.data.touchBottom) {
      this.setData({
        page: this.data.page + 1
      })
      this.getList()
    } else {
      wx.showToast({
        icon: 'none',
        title: '没有更多了',
      })
    }
  }
})
