import common from '../../utils/util';
import main from '../../utils/main';

const app = getApp();

Page({
  data: {
  },
  onLoad: function () {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
      IsShowNull: false,
      
    });

    common.getStorageValue(that, "SearchTextList", [], function () {
    });
  },
  onShow:function(){
    this.setData({
      Focus: true,
    });
  },
  onKeyInput: function (e) {
    var search = e.detail.value;
    var that = this;
    that.setData({
      SearchInfo: search,
    });
  },
  onSearch: function (e) {
    if (e.currentTarget.dataset.search)
      this.data.SearchInfo = e.currentTarget.dataset.search;
    if (this.data.SearchInfo && this.data.SearchInfo.length > 0) {
      var search = this.data.SearchInfo;
      
      var that = this;
      var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID;
      if (search)
        url += "&Key=" + search;

      var timeoutGetData=0;
      wx.showLoading({
        title: '请稍后...',
      });
      main.getData(url, function (data) {
        wx.hideLoading();
        clearTimeout(timeoutGetData);
        if (data) {
          app.globalData.CardList= data.List;
          wx.navigateTo({
            url: './searchCardList?search=' + search+'&Count='+data.Count,
          })
        }
      });
      timeoutGetData=setTimeout(function(){
        wx.hideLoading();
      },10000);



      setTimeout(function () {
        var arr = that.data.SearchTextList;
        for (var i = 0; i < arr.length; i++) {
          if (arr[i] == search) {
            arr.splice(i, 1);
            break;
          }
        }
        arr.unshift(search);

        while (arr.length > 10) {
          arr.pop();
        }

        wx.setStorageSync("SearchTextList", arr);
        that.setData({
          SearchTextList: arr,
        });
      }, 2000);
    }
    else {
      wx.showToast({
        title: '请输搜索内容',
      })
    }
  },
  close: function (e) {
    wx.navigateBack({
      delta: 1,
    });
  },
  deleteItem: function (e) {
    var that = this;
    that.setData({
      SearchTextList: [],
    });
    wx.removeStorageSync("SearchTextList");
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
