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

const app = getApp();

Page({
  data: {
    IsNull:false,
    Searching:true,
    SearchList:[],
    InfoArr:["拼音","默写","书写","组词","诗词","译文","发音","释义","反义词","近义词","部首","作者","翻译","例句"],
  },
  onLoad: function () {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
      IsShowNull: false,
      Searching:true,
    });

    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,
    });
  },
  onFocus: function (e) {
    var that = this;
    that.setData({
      Searching:true,
      IsNull:false,
    });
  },
  onSearch: function (e) {
    if (e.currentTarget.dataset.search){
      this.data.SearchInfo = e.currentTarget.dataset.search;
      this.setData({
        SearchInfo:this.data.SearchInfo,
      });
    }
    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) {
          if (data.List.length==0){
            that.setData({
              IsNull:true,
            });
            that.getSearchData();
          }
          else{
            app.globalData.CardList= data.List;
            wx.navigateTo({
              url: './mainlist?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: '请输搜索内容',
      })
    }
  },
  getSearchData:function(){
    var that=this;
    main.searchInfomation(that.data.SearchInfo,null,null,null,function(list,isList){
      that.setData({
        SearchList:list,
        Searching:false,
      });
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    var search=e.currentTarget.dataset.word;
      
    if (search){
      app.globalData.TempStr=e.currentTarget.dataset.url2;
      var search=e.currentTarget.dataset.word;
      var author=e.currentTarget.dataset.author;
      var searchtype=e.currentTarget.dataset.searchtype;
      url+="&Word="+search;
      if (author)
        url+="&Author="+author;
      if (searchtype)
        url+="&SearchType="+searchtype;
        
      wx.navigateTo({
        url: url,
      });
    }
    else{
      wx.navigateTo({
        url: url,
      });
    }
    
  },
  clearInput:function(){
    this.setData({
      SearchInfo:"",
      IsNull:false,
      SearchList:[],
      Focus:true,
    });
  },
  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,
    }
  },
})
