// pages/add/index.js
const { api } = require("../../utils/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer:null,
    list:[],
    hotList:[
      {url:'https://res.catter.cn/pub/2025/09/30/20250930143920286.png',name:'金鱼吊兰'},
      {url:'https://res.catter.cn/pub/2025/09/30/20250930145312611.png',name:'月季'},
      {url:'https://res.catter.cn/pub/2025/09/30/20250930150006852.png',name:'多肉'}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.fetchHotList()
  },

  fetchHotList(){
    const data ={ current:1,pageSize:20,isHot:1}
    api('/library/list','POST',data,'json').then(res => {
      if (res.code === 200){
        const list = res.data.list
        this.setData({hotList:list})
      }
    })
  },

  search(e){
    if (this.data.timer) {
      clearTimeout(this.data.timer)
      this.data.timer = null;
    }
    const key = e.detail.value
    this.data.timer = setTimeout(() => {
      if (key.length === 0){
        this.setData({list:[]})
        return
      }
      this.fetchList(key)

    }, 500)
  },

  fetchList(key){
    console.log(key);
    const data ={ current:1,pageSize:20,name:key}
    api('/library/list','POST',data,'json').then(res => {
      if (res.code === 200){
        const list = res.data.list
        this.setData({list:list})
      }
    })
  },


  goInfo(e){
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../add/info?id=' + id,
    })
  }
})