// pages/productDetail/productDetail.js
var app = getApp()
const wxService = require('../../utils/wxService')


wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    productId:'',
    productInfo:'',
    showHtml: false,
    reachBottom: false, // 标识是否触底
    showSkuPopup: false,
    btnText: '确定',
    baseImgUrl: app.globalData.imageUrl,
    swiperCurrent: 0,
    selectSkuName:'',
    openPlusStatus:false,
    memberLevel:false
  },

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

  swiperChange(e) {
    console.log(e)
    this.setData({
      swiperCurrent: e.detail.current
    })
  },

  addCart: function (event) {
    this.setData({
      showSkuPopup: true,
      showSkuPopupType: 'cart'
    })
    // 商品加入购物车
    // xm_bi.Event.stat("skuAddToCart", { "amount":"666","skuId":'abcd1234'});
  },

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

  //获取商品详情
  getProInfo(){
    wx.showLoading({
      title: '加载中'
    })
    wxService.get(`/sale/product/buyer/findByProductId?productId=${this.data.productId}`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        wx.hideLoading()
        this.setData({
          productInfo: data
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  // 获取会员等级开关
  getMemberLevel(){
    wxService.get(`/merchant/brandSwitchSetting/getOpenStatus?businessType=2`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          memberLevel: data
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

  // 获取plus会员开关
  getOpenStatus(){
    wxService.get(`/merchant/brandSwitchSetting/getOpenStatus?businessType=3`).then(res => {
      const { result, data } = res.data
      if (result == 0) {
        this.setData({
          openPlusStatus:data
        })
      }
    }).finally(() => {
      wx.hideLoading()
    })
  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 页面touchstart事件
  pageTouchStart: function (e) {
    if (this.data.showHtml) {
      return
    }
    this.setData({
      reachBottom: false,
      startTouchY: null
    })
  },

  // 页面touchend事件
  pageMoveEnd: function (e) {
    if (this.data.showHtml) {
      return
    }
    if (this.data.startTouchY - e.changedTouches[0].pageY >= 50) {
      this.setData({
        showHtml: true,
        scrollTips: ''
      })
    }
  },

  // 页面touchmove事件
  pageMove: function (e) {
    if (this.data.showHtml) {
      return
    }
    wx.createSelectorQuery().selectViewport().scrollOffset((pageRes) => {
      wx.createSelectorQuery().select('.product-container').fields({
        size: true
      }, (res) => {
        if (res.height >= app.globalData.systemInfo.windowHeight + pageRes.scrollTop) {
          this.setData({
            reachBottom: true
          })
        } else {
          if (this.data.reachBottom) {
            this.setData({
              reachBottom: false
            })
          }
        }
        if (this.data.startTouchY == null) {
          this.setData({
            startTouchY: e.changedTouches[0].pageY
          })
        }
      }).exec()
    }).exec()
  },

  confirmClick(event){
    console.log(event)
    var selectSkuName = []
    for (let i in event.detail.selectSku){
      selectSkuName.push(event.detail.selectSku[i].name)
    }
    var selectSkuStr = selectSkuName.join(',')
    this.setData({
      selectSkuName: selectSkuStr
    })
  }



})