// pages/coupons.js
const app = getApp()
const wxService = require('../../utils/wxService')
const utils = require('../../utils/util')
const envInfo = require('../../config/index').envInfo
import { Integer } from '../../utils/integerDigitalConvertion'

wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    contentInfo:[],
    id:'',
    pageNo:1,
    pageSize:100,
    proList:[],
    showCardModal: false,
    currentShareContent: null,
    pagePath:''
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options && options.id){
      console.log(options.id)
      this.setData({
        id: options.id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getContentDetail()
    this.data.currentShareContent = null;

  },

  // 分享
  onTapShare() {
    let content = {
      headline: this.data.contentInfo.headline,
      id: this.data.contentInfo.id,
      article: this.data.contentInfo.article,
      url: this.data.contentInfo.url
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: this.data.contentInfo.id,
      contentType: 2, //内容类型 1.文章 2.商品 3.页面
      title: this.data.contentInfo.headline, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    this.data.currentShareContent = this.data.contentInfo;

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.tentacleId) {
        this.data.currentShareContent.tentacleId = res.tentacleId;
        let id = Integer.digit(this.data.currentShareContent.id, 10, 64);
        let tentacleId = Integer.digit(this.data.currentShareContent.tentacleId, 10, 64);
        var path = 'pages/wantToBuyDetail/wantToBuyDetail?i=' + id + '&t=' + tentacleId;
        this.setData({
          showCardModal: true,
          cardTitle: this.data.contentInfo.headline,
          cardUrl: this.data.contentInfo.url,
          pagePath: path
        });
      }
    })
  },

  // 获取内容详情
  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(() => {
      })
    }
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
      let id = Integer.digit(this.data.currentShareContent.id, 10, 64);
      let tentacleId = Integer.digit(this.data.currentShareContent.tentacleId, 10, 64);
      var path = 'pages/wantToBuyDetail/wantToBuyDetail?i=' + id + '&t=' + tentacleId;
      console.log(path)
      return {
        path: path
      }
  }
 
})