// 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;

  },
  //跳转商品详情
  onTapToGoodsInfo(e){
    console.log(e)
    let item = e.currentTarget.dataset.item;
    let productId = e.currentTarget.dataset.productid;
    //组装一个临时触点信息对象到本地
    let tempObj = {
      memberId: item.userId ? item.userId : '-99',
      obj : {
        id: item.tentacleObjectId,
        tentacleType: item.tentacleType,
        tentacleId: item.tentacleId,
        contentType: item.contentType,
        contentId: item.contentId
      }
    };
    //存储
    var timestamp = Date.parse(new Date())
    var expiration = timestamp + 1800000
    wx.setStorageSync('TEMP_TENTACLE_INFO', tempObj);
    wx.setStorageSync('TIME_EXPIRATION', expiration);
    wxService.router(`/pages/productDetail/productDetail?id=${productId}&fromZc=1`);
  },

  // 分享
  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;

    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) {
    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;
      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 id = Integer.digit(this.data.currentShareContent.id, 10, 64);
        this.data.currentShareContent.tentacleId = res.tentacleId;
        let tentacleId = Integer.digit(res.tentacleId, 10, 64);
        var path = 'pages/wantToBuyDetail/wantToBuyDetail?i=' + id + '&t=' + tentacleId;
        return {
          path: path
        }
      }
    });
  }
 
})