// subPackage/page/pages/myReleaseDetail.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')
const { memberId } = wx.getStorageSync('_baseUserInfo')
import { Integer } from '../../../../utils/integerDigitalConvertion'


wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    showDialig: false,
    showCardModal: false,
    currentShareContent: null,
    pagePath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options && options.id) {
      this.setData({
        id: options.id
      },() => {
        this.getContentDetail()
      })
    }
  },
  handleDel() {
    this.setData({
      showDialig: true
    })
  },

  // 分享
  onTapShare(){
    let content = {
      title: 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: app.globalData.contants.SHARE_TYPE.DETAILS_OF_THE_CONTENT, //内容类型 
      title: this.data.contentInfo.headline, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    this.data.currentShareContent = this.data.contentInfo;
    console.log(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/terminalContentParticulars`, {
      id: this.data.id,
      userId: memberId
    }).then(res => {
      if (!res) return false
      const { result, data } = res.data
      console.log('content info ----------------------------------')
      console.log(data)
      if (result == 0) {
        this.setData({
          contentInfo: data
        })
        
      }

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

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

  // 删除
  closeDialog(){
    wxService.post(`/merchant/contentMobileTerminal/contentDelete`, {
      id: 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'
        })
        wxService.back()
      }

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

 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.id){
      this.getContentDetail()
    }
    this.data.currentShareContent = null;
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    if (res.from != 'menu') {
      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
      }
    }

    let content = {
      title: 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: app.globalData.contants.SHARE_TYPE.DETAILS_OF_THE_CONTENT, //内容类型 
      title: this.data.contentInfo.headline, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }
    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.tentacleId) {
        let tentacleId = Integer.digit(res.tentacleId, 10, 64);
        var path = 'pages/wantToBuyDetail/wantToBuyDetail?i=' + id + '&t=' + tentacleId;
        return {
          path: path
        }
      }
    });
  }
})