// pages/components/blocks/search_box/search_box.js
const getReq = require('../../../../config.js').getReq;
let app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    inputVal:'',
    searchItems:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    skip_target(e){
      let word = e.target.dataset.word;
      app.navigateto(`/pages/brand/brand?keyword=${word}&title=${word}`)
    },
    search_btn(){
      let inputVal = this.data.inputVal;
      if (!inputVal) {
        return;
      } 
      app.navigateto(`/pages/brand/brand?keyword=${inputVal}&title=${inputVal}`)
    },
    input(e){
      let self = this;
      let val = e.detail.value; 
      this.setData({
        inputVal: val
      });
      if (val == "") {
        clearTimeout(this.search);
        self.setData({
          searchItems: []
        })
        return;
      }
      clearTimeout(this.search);
      this.search = setTimeout(function(){  
        getReq(app,{
            act: 'search',
            op: 'suggest_words',
            keyword: val
          }, function (res) {
            if (res.code == 200) {
              if(res.datas.words == null) {
                self.setData({
                  searchItems:[]
                })
              }
              else {
                self.setData({
                  searchItems: res.datas.words
                });
              }
            }
          })
      },300);
      
    }
  }
})
