// 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: {
    contentInfo:[],
    id:'',
    pageNo:1,
    pageSize:100,
    proList:[]
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.id){
      console.log(options.id)
      this.setData({
        id: options.id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getContentDetail()
  },

  // 获取内容详情
  getContentDetail(){
    wxService.post(`/merchant/contentMobileTerminal/getContentParticulars`,{
      id: this.data.id
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          contentInfo: data
        })
        this.getProList()
      }

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

  //获取内容下商品列表
  getProList(){
    var params = {
      productIds: this.data.contentInfo.productIds
    }
    wxService.post(`/sale/product/buyer/listPage?pageNo=${this.data.pageNo}&pageSize=${this.data.pageSize}`, params).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0 && data.content) {
        this.setData({
          proList: data.content
        })
      }
    }).finally(() => {
    })
  },

  // 点赞
  gotoLike(){
    const { memberId } = wx.getStorageSync('_baseUserInfo')
    let params = {
      contentId: this.data.contentInfo.id,
      type: this.data.contentInfo.currentType,
      userId: memberId
    }
    if (!this.data.contentInfo.currentType) {
      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: this.data.contentInfo.id,
        userId: memberId
      }).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(() => {
      })
    }
  }
 
})