// subPackage/page/pages/fun/fun.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    proData: [],
    pageNo: 1,
    pageSize: 10,
    totalPages:'',
    form:{
      queryDto: {
        mallCategoryId: ''
      }
    }
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getProList();
  },

  //获取商品列表
  getProList(){
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/sale/product/buyer/listPageByCategoryId?pageNo=${this.data.pageNo}&pageSize=${this.data.pageSize}`, this.data.form.queryDto).then(res => {
      const { result, data } = res.data
      if (result == 0 && data.content) {
        wx.hideLoading()
        this.setData({
          proData: this.data.pageNo == 1 ? [...data.content] : [...this.data.proData, ...data.content],
          totalPages: data.totalPages,
          totalElements: data.totalElements
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.setData({
        pageNo: 1,
      })
      this.getProList()
      wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const {  pageNo, pageSize, totalPages } = this.data
      if (pageNo < totalPages) {
        this.setData({
          pageNo: pageNo + 1,
        })
        this.getProList()
      } 
  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (res) {
    let tentacleInfo = {
      content: this.route,
      title: this.route,
      contentType: 3,
      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 = `subPackage/page/pages/fun/fun?t=${tentacleId}`;
        console.log(path)
        return {
          path: path
        }
      }
    });
  }
})