// pages/message/message.js
const utils = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: '', //是否登陆
    userInfo: '', //缓存用户信息
    list: [],
    pageNum: 1,
    pageSize: 10,
    pages: '',
    finish: false,
    listNotice: [],
    pageNumNotice: 1,
    pageSizeNotice: 10,
    pagesNotice: '',
    finishNotice: false,
    curIndex: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: utils.getInfo()
    })
    if (this.data.userInfo.mobile) {
      this.getList()
      this.getNotice()
    } else {
      this.setData({
        isLogin: false
      })
    }
  },

  switchTab(e) {
    const curIndex = Number(e.currentTarget.dataset.index)
    this.setData({
      curIndex
    })
  },

  // 公告列表
  getNotice() {
    let params = {
      mobile: this.data.userInfo.mobile,
      pageNum: this.data.pageNumNotice,
      pageSize: this.data.pageSizeNotice
    }
    utils.axios({
      method: 'get',
      url: '/auth/user/msg/findGlobalNotice',
      data: params,
      noLogin: () => {},
      complete: () => {},
      success: res => {
        console.log(12);
        let result = res.data
        let listNotice = this.data.listNotice
        listNotice = listNotice.concat(result.records)
        this.setData({
          listNotice,
          pagesNotice: result.pages
        })
        if (result.total <= this.data.pageSizeNotice) {
          this.setData({
            finishNotice: true
          })
        }
      }
    })
  },

  //消息列表
  getList() {
    let params = {
      mobile: this.data.userInfo.mobile,
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize
    }
    utils.axios({
      method: 'get',
      url: '/auth/user/msg/findUserMsg',
      data: params,
      sendBefore() {
        wx.showLoading({
          title: '加载中...'
        })
      },
      complete() {
        wx.hideLoading()
        wx.stopPullDownRefresh()
      },
      noLogin: () => {
        this.setData({
          isLogin: false
        })
      },
      success: res => {
        this.setData({
          isLogin: true
        })
        let result = res.data
        let list = this.data.list
        list = list.concat(result.records)
        this.setData({
          list,
          pages: result.pages
        })
        if (result.total <= this.data.pageSize) {
          this.setData({
            finish: true
          })
        }
      }
    })
  },
  toFresh() {
    if (this.data.curIndex === 0) {
      this.setData({
        list: [],
        pageNum: 1,
        pages: '',
        finish: false
      })
      this.getList()
    } else {
      this.setData({
        listNotice: [],
        pageNumNotice: 1,
        pagesNotice: '',
        finishNotice: false
      })
      this.getNotice()
    }
  },

  //公告详情
  toDetail(e) {
    let msgId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/notice/noticeDetail?msgId=' + msgId
    })
  },

  //去登录
  toLogin() {
    utils.toLogin()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toFresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.curIndex === 0) {
      let pageNum = this.data.pageNum
      let pages = this.data.pages
      pageNum++
      if (pageNum <= pages) {
        this.setData({
          pageNum
        })
        this.getList()
      } else {
        this.setData({
          finish: true
        })
        return
      }
    }else{
      let pageNumNotice = this.data.pageNumNotice
      let pagesNotice = this.data.pagesNotice
      pageNumNotice++
      if (pageNumNotice <= pagesNotice) {
        this.setData({
          pageNumNotice
        })
        this.getNotice()
      } else {
        this.setData({
          finishNotice: true
        })
        return
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '诺信云',
      path: '/pages/index/index'
    }
  }
})