const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
  	hotLabel:[],
  	record:[],
    dataList:[],
  	userInfo:{},
  	result:false,
    search:false,
    value:'',
    type:''
  },
  onLoad(options) {
    console.log(options)
  	let record = options.type=='goods'?(wx.getStorageSync('_GOODSCORD_') || []):(wx.getStorageSync('_BBRECORD_') || []);
  	let userInfo = app.globalData.userInfo;
  	this.setData({record,userInfo,type:options.type})
  	this.getHot(1);
  },
  bindinput(e){
    this.setData({value:e.detail.value})
  },
  // 清除记录
  clean(){
  	this.setData({record:[]})
  	wx.removeStorage({
  	  key: this.data.type=='goods'?'_GOODSCORD_':'_BBRECORD_'
  	})
  },
  // 获取热门标签
  getHot(pageIndex){
  	let that = this;
  	util.ajax({
      func:"article/hot_keywords",
      data:{"pageIndex": pageIndex,"pageSize":10,"atype":that.data.type=='goods'?1:0}
    },function(res){
      if(res.code == 0){
    	that.setData({hotLabel:res.data});
      }
    })
  },
  bindSearch(){
    this.search(this.data.value);
  },
  // 搜索
  search(text){
    var that = this,type=this.data.type;
  	if(text == ''){
  	  util.showTips('请先输入您要搜索的关键词');
      return false;
  	}
    let record = type=='goods'?(wx.getStorageSync('_GOODSCORD_') || []):(wx.getStorageSync('_BBRECORD_') || []);
    if(record.length){
      let judge = false;
      for(let i in record){
        if(record[i] == text){
          judge = true
          break ;
        }
      }
      if(!judge){
        record.splice(0,0,text);
      }
    }else{
      record.push(text);
    }
    wx.setStorage({
      key:type=='goods'?'_GOODSCORD_':"_BBRECORD_",
      data:record
    })
  	util.ajax({
      func:"article/searching",
      data:{keyword:text,pageIndex:1,pageSize:50,atype:type=='goods'?1:0}
    },function(res){
      if(res.code == 0){
      	let dataList = res.data.articles;
        that.setData({dataList,search:true})
      }else{
        util.showTips(res.reason);
      }
    })
  },
  choose(e){
    let aid = e.currentTarget.dataset.aid;
    wx.navigateTo({
      url: '/pages/product/activity/index?aid='+aid
    })
  },
  // 标签搜索
  bindLabel(e){
    this.search(e.currentTarget.dataset.text);
    this.setData({value:e.currentTarget.dataset.text});
  }
})