// pages/coupons.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const envInfo = require('../../../../config/index').envInfo

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [], //分类
    baseImgUrl: app.globalData.imageUrl,
    pageNum: 1,
    pageSize: 10,
    totalPages: 0,
    totalElements: 0,
    currentTab: 0,
    showCardModal: false,
    cardTitle: '',
    cardUrl: '',
    contentList: [
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getBannerInfo()
    this.getContentList()
  },

  // banner
  getBannerInfo(){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/merchant/contentMobileTerminal/getBannerAndInviteSum`, {
      userId: memberId
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          bannerUrl: data.bannerUrl,
          inviteSum: data.inviteSum,
          yesterdayInviteSum: data.yesterdayInviteSum
        })
      }
    }).finally(() => {
    })
  },



  // 获取发现内容
  getContentList() {
    wx.showLoading({
      title: '加载中'
    })
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/merchant/contentMobileTerminal/getContentList?pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`, {
      userId: memberId
    }).then(res => {
      wx.hideLoading()
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        if (data.content) {
          this.setData({
            contentList: this.data.pageNum == 1 ? [...data.content] : [...this.data.contentList, ...data.content],
            totalPages: data.totalPages,
            totalElements: data.totalElements
          })
        } else {
          this.setData({
            contentList: []
          })
        }
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  // 点赞
  gotoLike(e) {
    console.log(e.detail)
    let params = e.detail
    if (!e.detail.type) {
      wxService.post(`/merchant/contentMobileTerminal/operationSave`, params).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          wx.showToast({
            title: '点赞成功',
            icon: 'none'
          })
          this.getContentList()
        }

      }).finally(() => {
      })

    } else {
      wxService.post(`/merchant/contentMobileTerminal/operationDelete`, {
        id: e.detail.contentId,
        userId: e.detail.userId
      }).then(res => {
        if (!res) return false
        const { result, data } = res.data
        if (result == 0) {
          wx.showToast({
            title: '取消点赞成功',
            icon: 'none'
          })
          this.getContentList()
        }

      }).finally(() => {
      })
    }
  },

  // 分享
  _onTapShare(e) {
    console.log(e)
    this.setData({
      showCardModal: true,
      cardTitle: e.detail.title,
      cardUrl: e.detail.url,
    })
  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    this.setData({
      pageNum: 1,
    })
    this.getContentList(this.data.currentTab)
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { pageNum, pageSize, totalPages } = this.data
    if (pageNum < totalPages) {
      this.setData({
        pageNum: pageNum + 1,
      })
      this.getContentList(this.data.currentTab)
    }
  }
})