// shoppingGuid/page/pages/moreProducts/moreProducts.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const app = getApp();
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: {
      goodsTypeArr: [
        { name: '最新', type: 'new', active: true },
        { name: '最热🔥', type: 'hot', active: false },
        { name: '推荐👍', type: 'recommend', active: false }
      ],
      currentGoodsType: 'new',
      goodsList: []
    },
    shareModelObj: {
      title: '',
      imageUrl: '',
      path: ''
    },
    recommandProductsNoData : false,
    showShareModel : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let type = options.type;
    if (type) {
      this.data.goods.currentGoodsType = type;
      this.data.goods.goodsTypeArr.map(item => {
        item.active = false;
        if (item.type == type) {
          item.active = true;
        }
      });

      this.setData({
        goods: this.data.goods
      });
    }

    //获取优惠券列表
    this.getGoodsList();
  },

  //筛选点击
  onTapChangeGoodsType(e){
    let index = e.currentTarget.dataset.index;
    this.data.goods.goodsTypeArr.map(item => {
      item.active = false;
    });

    this.data.goods.goodsTypeArr[index].active = true;
    this.data.goods.currentGoodsType = this.data.goods.goodsTypeArr[index].type;
    this.getGoodsList();
    this.setData({
      goods: this.data.goods
    });
  },
  //获取商品列表
  getGoodsList(refresh){
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    let order = this.data.goods.currentGoodsType;
    wxService.post(`/merchant/share/productBonus/buyer/findPageOrder?pageNo=1&pageSize=20&order=${order}`, {}).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let list = data.content ? data.content : [];
          list.forEach(item => {
            item.productImgUrl = item.productImgUrl ?
              item.productImgUrl : 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/1571636466%281%29.jpg';
          });

          if (list.length == 0) {
            this.data.recommandProductsNoData = true;
          }

          this.data.goods.goodsList = list;
          this.setData({
            goods: this.data.goods,
            recommandProductsNoData: this.data.recommandProductsNoData
          });
        }

        if (refresh) {
          wx.stopPullDownRefresh();
          wx.showToast({
            title: '刷新成功',
            icon: 'none'
          });
        }
      }
    });
  },

  //
  //立即群发商品给用户
  onTapToRecommandProduct(e) {
    let item = e.currentTarget.dataset.item;
    // 生成触点对象
    let content = {
      title: item.productName,
      id: item.productId,
      url: item.productImgUrl
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.productId,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 
      title: item.productName, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        let tentacleId = res.id;
        let inner_id = Integer.digit(item.productId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'pages/productDetail/productDetail' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        //打开企业微信通讯录选择会员
        wx.qy.shareToExternalContact({
          appid: app.globalData.appId,//小程序的appid
          title: item.productName, //小程序消息的title
          imgUrl: item.productImgUrl,//小程序消息的封面图
          page: path, //小程序消息打开后的路径
          success: function (res) {
            wx.showToast({
              title: '推荐成功！'
            });
          },
          fail: function (err) {
            console.log('error---------------', err);
          }
        });
      }
    });

  },

  //定向发送
  //定向转发商品给用户 分享功能
  onTapToShare(e) {
    let item = e.currentTarget.dataset.item;
    // 生成触点对象
    let content = {
      title: item.productName,
      id: item.productId,
      url: item.productImgUrl
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: item.productId,
      contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_DETAIL, //内容类型 
      title: item.productName, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        let tentacleId = res.id;
        let inner_id = Integer.digit(item.productId, 10, 64);
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        var path = 'pages/productDetail/productDetail' + '?i=' + inner_id + '&t=' + inner_tentacleId;
        this.data.shareModelObj.title = item.productName;
        this.data.shareModelObj.imageUrl = item.productImgUrl;
        this.data.shareModelObj.path = path;

        this.setData({
          shareModelObj: this.data.shareModelObj,
          showShareModel: true,
        });

      }
    });
  },

  //取消分享
  onTapCancelShare(){
    this.setData({
      showShareModel: false,
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.goods.goodsList = [];
    this.getGoodsList(true);
  },

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

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from == 'button') {
      return {
        title: this.data.shareModelObj.title,
        path: this.data.shareModelObj.path,
        imageUrl: this.data.shareModelObj.imageUrl
      }
    }
  }
})