// 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:'',
    loadingTime: '',
    proList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu();
    console.log('mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm',options)
    if (options && options.id) {
      this.setData({
        id: options.id
      },() => {
        this.getContentDetail()
      })
    }
    let memberId = options.memberId;
    if (memberId) {
      let content = {
        title: this.data.contentInfo.headline,
        id: this.data.contentInfo.id,
        article: this.data.contentInfo.article,
        url: this.data.contentInfo.sspList[0].url
      }
      let tentacleInfo = {
        content: JSON.stringify(content),
        contentId: this.data.contentInfo.id,
        tentacleId: memberId,
        contentType: app.globalData.contants.SHARE_TYPE.DETAILS_OF_THE_CONTENT, //内容类型 
        title: this.data.contentInfo.headline, //标题
        type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
      }

      this.generatePointObject(tentacleInfo);
    }
  },
  handleDel() {
    this.setData({
      showDialig: true
    })
  },

  // 分享
  onTapShare(){
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    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.id) {
        this.data.currentShareContent.tentacleId = res.id;
        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.sspList[0].url,
          pagePath:path
        });
        let params = {
          contentId: this.data.contentInfo.id,
          type: 3,
          userId: memberId
        }
        this.getReadyOrForwardNumber(params)
      }
    })
  },

  //获取内容下商品列表
  getProList() {
    var params = {
      productIds: this.data.contentInfo.productIds
    }
    if (this.data.contentInfo.productIds && this.data.contentInfo.productIds.length > 0) {
      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(() => {
      })
    }
  },

  // 获取内容详情
  getContentDetail() {
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    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
        })
        this.getProList()
      }

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

  // 统计阅读和转发量
  getReadyOrForwardNumber(params) {
    wxService.post(`/merchant/contentMobileTerminal/operationSave`, params).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
      }
    }).finally(() => {
    })
  },

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

      }).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.getContentDetail()
        }

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

  // 删除
  closeDialog(){
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    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 () {
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    if(this.data.id){
      this.getContentDetail()
    }
    this.data.currentShareContent = null;
    //超过15s统计阅读
    this.setData({
      loadingTime: setTimeout(() => {
        let params = {
          contentId: this.data.contentInfo.id,
          type: 2,
          userId: memberId
        }
        this.getReadyOrForwardNumber(params)

      }, 15000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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,
        imageUrl: this.data.contentInfo.sspList[0].url
      }
    }else{
      //修改为参数memberId
      var path = `pages/wantToBuyDetail/wantToBuyDetail`;
      const { memberId, member } = wx.getStorageSync('_baseUserInfo')
      console.log('--------------------memberId-=---', memberId)
      if (memberId) {
        let m = Integer.digit(memberId, 10, 64);
        let contentId = Integer.digit(this.data.contentInfo.id,10,64);
        path = `pages/wantToBuyDetail/wantToBuyDetail?i=${contentId}&m=${m}`;
      }
      return {
        path: path,
        imageUrl: this.data.contentInfo.sspList[0].url
      }

    }

  }
})