// 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,
    contentList:[]
  },
 
  // 点击切换
  clickTab: function (e) {
    const { current } = e.target.dataset
    if (this.data.currentTab == current) {
      return false;
    } else {
      this.setData({
        currentTab: current,
      })
    }
    this.getContentList(current)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

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

// 获取分类
  getCategoryList(){
    wxService.post(`/merchant/contentMobileTerminal/gatClassify`).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0 ) {
        this.setData({
          categoryList:data,
          currentTab:data[0].id
        })

        this.getContentList(data[0].id)
      }

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

  // 获取发现内容
  getContentList(classifyId){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    wxService.post(`/merchant/contentMobileTerminal/getContentList?pageNum=${this.data.pageNum}&pageSize=${this.data.pageSize}`, {
      classifyId: classifyId,
      userId: memberId
    }).then(res => {
      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(() => {
    })
  },

  // 点赞
  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(this.data.currentTab)
          }

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

      }else{
        wxService.post(`/merchant/contentMobileTerminal/operationDelete`, {
          contentId: 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(this.data.currentTab)
          }

        }).finally(() => {
        })
      }
  },
  
  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  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)
    } 
  }
})