// 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: {
    // id: "5d6e2a2338c95504a8de84e8",
    id:'',
    proList: [],
    showCardModal: false,
    currentShareContent: null,
    pagePath: '',
    pageNo: 1,
    pageSize: 100,
    sharePicData: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    console.log()
    if (options && options.id) {
      console.log('idididididi',options.id)
      this.setData({
        id: options.id
      })
    }

    let memberId = options.memberId;
    if (memberId) {
      let tentacleInfo = {
        content: '推广商品列表',
        title: '推广商品',
        tentacleId: memberId,
        contentType: app.globalData.contants.SHARE_TYPE.PRODUCTS_PROMOTION,
        type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
      };
      console.log('触点对象id-----------', options)

      this.generatePointObject(tentacleInfo);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getExtendInfo()
    this.data.currentShareContent = null;
  },
 
  //获取推广商品获取商品id
  getExtendInfo() {
    console.log(this.data.id)
    wxService.post(`/merchant/spread/${this.data.id}`, {}).then(res => {
      if (!res) return false
      const { result, data } = res.data
      if (result == 0) {
        let productIds = []
        for (var i in data.spreadProducts){
          if (data.spreadProducts[i].id){
            productIds.push(data.spreadProducts[i].id)
          }
        }
        this.getByIdExtendProList(productIds)
      }
    }).finally(() => {
    })
  },

  // 根据商品id，获取商品列表
  getByIdExtendProList(productIds){
    let params = {
      productIds: 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
        })
        for (var i in data.content){
          this.data.sharePicData.push(data.content[i].indexUrl)
          
          this.setData({
            sharePicData: this.data.sharePicData
          })
        }
        console.log(this.data.sharePicData)
      }
    }).finally(() => {
    })
  },

 

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    this.data.proList.forEach((item)=>{
      item.url = item.indexUrl
      item.title = item.productName
      item.id = item.productId
    })
   
    // wxService.getTentacleContent(tentacleInfo).then(res => {
    //   if (res && res.tentacleId) {
    //     let id = Integer.digit(this.data.id, 10, 64);
    //     let tentacleId = Integer.digit(res.tentacleId, 10, 64);
    //     var path = `subPackage/page/pages/friendShareList/friendShareList?id=${id}&t=${tentacleId}`;
    //     console.log(path)
    //     return {
    //       path: path
    //     }
    //   }
    // });

    //修改为参数memberId
    var path = `subPackage/page/pages/friendShareList/friendShareList`;
    if (memberId) {
      let m = Integer.digit(memberId, 10, 64);
      let id = Integer.digit(this.data.id, 10, 64);
      path = `subPackage/page/pages/friendShareList/friendShareList?m=${m}&i=${id}`;
    }
    return {
      path: path
    }
  }

})