// nova-tourism/components/suggest/index.js
let Parse = getApp().Parse;
const company = getApp().globalData.company
const uid = Parse.User.current()?.id
const req = require('../../../utils/request')


Component({

  /**
   * 组件的属性列表
   */
  properties: {
    store_id:null
  },

  /**
   * 组件的初始数据
   */
  data: {
    gift: [], //望仙礼遇-商品
    swiper:1,//左右滑动遍历次数
    skip:0,
  },

  lifetimes: {
    detached: function () {},
    attached: async function () {
      setTimeout(() => {
        let {store_id}=this.properties
        this.setData({store_id})
        this.getGift()
        this.getCount()
      }, 800);
   
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    /** 获取望仙礼遇-商品*/
    async getGift() {
      let {
        store_id
      } = this.properties
      let {skip}=this.data
      console.log(store_id)
      let sql = `SELECT good."objectId",good."name",good."price",good."image"
  FROM "ShopGoods" good
  WHERE good."company" = '${company}'
  AND good."isDeleted" IS NOT TRUE
  AND good."status" IS TRUE
  AND good."shopStore"='${store_id}'
  ORDER BY COALESCE(good."isRecom", FALSE) DESC,
  CASE WHEN good."top"= 0 THEN 1 ELSE 0 END,
  good."top" ASC,good."createdAt"
  LIMIT 4 OFFSET ${skip}`
      let gift = await req.customSQL(sql) || []
      console.log(gift)
      this.setData({
        gift
      })
    },

/**获取商品总量 */
async getCount(){
  let {
    store_id
  } = this.properties
  let query = new Parse.Query('ShopGoods')
  query.equalTo('company',company)
  query.notEqualTo('isDeleted',true)
  query.equalTo('status',true)
  query.equalTo('shopStore',store_id)
  let count = await query.count()
  console.log(count)
  let swiper = Math.ceil(count / 4)||1
  if(swiper>10) swiper = 10//最多10个内切换,40个商品
  this.setData({swiper,count})
},
    /**切换Swiper */
    changeSwiper(e){
      let {current} = e.detail
      let skip = current * 4
      this.setData({skip})
      this.getGift()
    },
    /** 跳转*/
    tourl(e) {
      const url = e.currentTarget.dataset.url
      wx.navigateTo({
        url: `${url}`  //目标页面的路径
      });
    },
  }
})