// nova-tourism/pages/collect/good-list/index.js
const Parse = getApp().Parse;
const company = getApp().globalData.company;
const uid = Parse.User.current()?.id
const req = require('../../../../utils/request')

let {
  statusBarHeight,
  screenHeight,
  screenWidth,
  safeArea
} = wx.getSystemInfoSync()
let custom = wx.getMenuButtonBoundingClientRect()
let customHeight = custom.height
let customWidth = custom.width

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodList: [],
    searchTimeOut: null,
    value:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let nav_px = statusBarHeight + customHeight + 10
    let scroll_px = +safeArea.bottom - statusBarHeight - customHeight
    let {store_id}=options
    this.setData({
      nav_px,
      scroll_px,
      cart_buttom: 120,
      statusBarHeight,
      customWidth,
      store_id
    })
    this.getGood()
  },


  /**搜索框输入内容 */
  searchInput() {
    let {
      searchTimeOut
    } = this.data
    if (searchTimeOut) clearTimeout(searchTimeOut)
    let that = this
    let timeOut = setTimeout(() => {
      that.setData({
        goodList: []
      })
      that.getGood()
    }, 500);
    this.setData({
      searchTimeOut: timeOut
    })
  },

  /**获取商品 */
  async getGood() {
    let {
      goodList,
      store_id,
      value
    } = this.data
    if (value && value != '') {}
    let sql = `SELECT good."objectId",good."name",good."price",good."image"
    FROM "ShopGoods" good
    LEFT JOIN "ShopStore" shop ON shop."objectId"=good."shopStore"
    WHERE good."company" = '${company}'
    AND good."isDeleted" IS NOT TRUE
    ${store_id?`AND good."shopStore" = '${store_id}'`:''}
    AND good."status" IS TRUE
    AND good."name" LIKE '%${value}%'
    AND shop."isDeleted" IS NOT TRUE
    AND shop."isVerified" IS TRUE
    ORDER BY COALESCE(good."isRecom", FALSE) DESC,
    CASE WHEN good."top"= 0 THEN 1 ELSE 0 END,
    good."top" ASC
    LIMIT 20 OFFSET ${goodList?.length||0}`
    // console.log(sql)
    let list = await req.customSQL(sql)||[]
    console.log(list)
    this.setData({
      goodList:[...(goodList||[]),...list]
    })
  },

     /** 跳转*/
     tourl(e) {
      const url = e.currentTarget.dataset.url
      wx.navigateTo({
        url: `${url}` // 目标页面的路径
      });
    },
  goback() {
    wx.navigateBack({
      delta: 1
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getGood()
  },

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

  }
})