// subPackage/page/pages/fun/fun.js
var app = getApp()
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const { memberId, member } = wx.getStorageSync('_baseUserInfo')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    proData: [],
    pageNo: 1,
    pageSize: 10,
    totalPages: '',
    form: {
      mallCategoryId: '',
      sortType:'',
      order:''
    },
    navTab:[
      {
        name: '推荐',
        type: 'recommend'
      },
      {
        name: '最新',
        type: 'new'
      },
      {
        name: '最热',
        type: 'hot'
      },
      {
        name: '价格',
        type: 'price'
      },
    ],
    priceSort:false,
    currentTab:'recommend',
    proStyle:false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    console.log(options)
    if (options && options.id) {
      this.setData({
        'form.mallCategoryId': options.id
      })
    }
    let memberId = options.memberId;
    if (memberId) {
      let tentacleInfo = {
        content: '商品列表',
        title: '商品列表',
        tentacleId: memberId,
        contentType: app.globalData.contants.SHARE_TYPE.PRODUCT_LIST,
        type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
      };

      this.generatePointObject(tentacleInfo);
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    this.setData({
      'form.sortType': this.data.currentTab
    })
    this.getProList();
  },

  // 切换tab
  switchTab(e){
    console.log(e)
    this.setData({
      currentTab: e.currentTarget.dataset.type,
      'form.sortType': e.currentTarget.dataset.type
    })
    if (this.data.currentTab == 'price'){
      if (!this.data.priceSort){
        this.setData({
          'form.order': 'desc',
          priceSort: true
        })
      }else{
        this.setData({
          'form.order': 'asc',
          priceSort: false
        })
      }
    }
    this.getProList();
  },

  //获取商品列表
  getProList() {
    wx.showLoading({
      title: '加载中'
    })
    wxService.post(`/sale/product/buyer/listPageByCategoryId?pageNo=${this.data.pageNo}&pageSize=${this.data.pageSize}`, this.data.form).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()
    })
  },

  // 切换列表样式
  switchProStyle(){
    this.setData({
      proStyle: !this.data.proStyle
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  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) {
   
    //修改为参数memberId
    const { memberId, member } = wx.getStorageSync('_baseUserInfo')
    var path = `subPackage/page/pages/categoryPro/categoryPro`;
    if (memberId) {
      let m = Integer.digit(memberId, 10, 64);
      path = `subPackage/page/pages/categoryPro/categoryPro?m=${m}`;
    }
    return {
      title: '商品列表',
      path: path
    }
  }
})