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

const app = getApp();
const WORD_LENGTH=18;

Page({
  data: {
    IsStart: false,
    Searching: false,
    SearchList: [],
    SearchInfo: "",
  },
  onLoad: function () {
    var that = this;

    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

  },
  onShow: function () {
    var that = this;
    common.getStorageValue(that, "SearchTextList", [], function () {});
    common.getStorageValue(that, "SearchWord3", [], function () {
      var list = that.data.SearchWord3;
      for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (item.Type == "shici") {
          item.TypeName = "S";
          item.Remark = item.Author + " " + item.Dynasty;
        } else if (item.Type == "eng") {
          item.TypeName = "D";
        } else {
          if (item.Key.length == 1)
            item.TypeName = "Z";
          else
            item.TypeName = "C";
        }

        if (item.Content && item.Content.length > WORD_LENGTH)
              item.Content = item.Content.substr(0, WORD_LENGTH) + "...";


      }
      that.setData({
        SearchWord3: list,
      });
    });

  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: './' + url,
    })
  },
  onKeyInput: function (e) {
    var search = e.detail.value;
    var that = this;
    that.setData({
      SearchInfo: search,
    });
  },
  onSearchHistory: function (e) {
    var index = Number(e.currentTarget.dataset.index);
    var obj = {};
    if (e.currentTarget.dataset.type == "SearchWord3")
      obj = this.data.SearchWord3[index];
    else
      obj = this.data.SearchList[index];

    main.updateSearchList(obj);
    app.globalData.TempSearchBackNumber = 2;
    app.globalData.CardList2=[];
    wx.navigateTo({
      url: './addInfomationDetail',
    });
  },

  searchResult: function (e) {
    var that = this;

    var search = that.data.SearchInfo;
    if (e.currentTarget.dataset.search) {
      search = e.currentTarget.dataset.search;
      that.setData({
        SearchInfo:search,
      });
    }
    var searchtype=e.currentTarget.dataset.searchtype;
    var author=e.currentTarget.dataset.author;
    var shiciurl=e.currentTarget.dataset.shiciurl;

    main.searchInfomation(search,searchtype,author,shiciurl,function(list,obj){
      
      if (obj){
        main.updateSearchList(obj,function(){
          wx.navigateTo({
            url: './addInfomationDetail',
          });
        });
      }
      else{
        wx.setNavigationBarTitle({
          title: "搜索结果"
        });

        that.setData({
          SearchList: list,
          IsStart: true,
        });

        that.getCardList();

        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);
      }

      
    });

    // wx.showLoading({
    //   title: '查询中',
    // });
    // var timeout=setTimeout(function () {
    //   wx.hideLoading();
    // }, 5000);

    // var url = 'GetMiaoguoAISearch2?UserID=' + app.globalData.userInfo.UserID;
    // url += "&Word=" + search;
    // if (e.currentTarget.dataset.searchtype)
    //   url += "&SearchType=" + e.currentTarget.dataset.searchtype;
    // if (e.currentTarget.dataset.author)
    //   url += "&Author=" + e.currentTarget.dataset.author;
    // if (e.currentTarget.dataset.shiciurl)
    //   url += "&ShiciUrl=" + e.currentTarget.dataset.shiciurl;

    // main.getData(url, function (data) {
    //   wx.hideLoading();
    //   clearTimeout(timeout);
    //   wx.setNavigationBarTitle({
    //     title: "搜索结果"
    //   });
    //   if (data) {
    //     //console.log(data);

    //     if (data.List) {
    //       var len = WORD_LENGTH;
    //       var list = data.List;
    //       for (var i = 0; i < list.length; i++) {
    //         var item = list[i];
    //         if (item.TypeName == "字词") {
    //           if (item.Key.length == 1)
    //             item.TypeName = "Z";
    //           else
    //             item.TypeName = "C";
    //         } else if (item.TypeName == "诗词") {
    //           item.TypeName = "S";
    //           item.Remark = item.Author + " " + item.Dynasty;
    //         } else if (item.TypeName == "翻译") {
    //           item.TypeName = "D";
    //         }

    //         if (item.Content && item.Content.length > len)
    //           item.Content = item.Content.substr(0, len) + "...";

    //       }
    //       that.setData({
    //         SearchList: list,
    //         IsStart: true,
    //       });
    //     } else if (data.CHN || data.ENG) {
    //       var obj = {};
    //       obj.Key = search;
    //       obj.Value = data;
    //       if (data.CHN && data.CHN.Author)
    //         obj.Author = data.CHN.Author;
    //       if (data.CHN && data.CHN.Dynasty)
    //         obj.Dynasty = data.CHN.Dynasty;
    //       if (data.CHN && data.CHN.PeomContent) {
    //         obj.Type = "shici";
    //         obj.TypeName = "诗词";
    //         obj.Content = data.CHN.PeomContent.join("").substr(0, WORD_LENGTH);
    //         obj.ShiciUrl = e.currentTarget.dataset.shiciurl;
    //       } else if (data.CHN) {
    //         obj.Type = "zici";
    //         if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].pinyin)
    //           obj.Remark = data.CHN.PinYin[0].pinyin;
    //         if (data.CHN.PinYin && data.CHN.PinYin[0] && data.CHN.PinYin[0].explain) {
    //           obj.Content = data.CHN.PinYin[0].explain.substr(0, WORD_LENGTH);
    //           obj.Content = obj.Content.replace("<p>", "");
    //           obj.Content = obj.Content.replace("</p>", "");
    //           if (obj.Content.length >= WORD_LENGTH)
    //             obj.Content += "..."
    //         }
    //       }
    //       if (data.ENG && !data.CHN) {
    //         obj.Type = "eng";
    //         obj.TypeName = "D";
    //         if (data.ENG.Soundmark && data.ENG.Soundmark.Eng)
    //           obj.Remark = data.ENG.Soundmark.Eng;
    //         if (data.ENG.Paraphrase.length > 0) {
    //           if (common.checkIsArray(data.ENG.Paraphrase) && data.ENG.Paraphrase[0].ParaphraseList)
    //             obj.Content = data.ENG.Paraphrase[0].ParaphraseList.join("; ").substr(0, WORD_LENGTH);
    //           else
    //             obj.Content = data.ENG.Paraphrase.substr(0, WORD_LENGTH) + "...";
    //         }
    //       }

    //       main.updateSearchList(obj);

    //       app.globalData.TempSearchBackNumber = 2;
    //       //app.globalData.CardList=[];
    //       app.globalData.CardList2=[];
    //       wx.navigateTo({
    //         url: './addInfomationDetail',
    //       });
    //     }
    //     else{
    //       that.setData({
    //         SearchList: [],
    //         IsStart: true,
    //       });
    //     }
    //   } else {
    //     that.setData({
    //       SearchList: [],
    //       IsStart: true,
    //     });
    //   }

      
    // });
  },
  getCardList: function () {
    var that = this;
    var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID;
    url += "&Key=" + that.data.SearchInfo;
    that.setData({
      Searching: true,
    });
    main.getData(url, function (data) {
      that.setData({
        Searching: false,
      });
      if (data) {
        if (data.List.length > 0) {
          var list=data.List;
          for (var i = 0; i < list.length; i++) {
            var item = list[i];
            item.Content[1].ContentStr = main.replaceCardInfoString(item.Content[1].Content);
            if (item.Content[1].ContentStr.length > WORD_LENGTH)
              item.Content[1].ContentStr = main.replaceCardInfoString(item.Content[1].ContentStr.substr(0, WORD_LENGTH)) + "...";
      
            item.Content[2].ContentStr = main.replaceCardInfoString(item.Content[2].Content);
            if (item.Content[2].ContentStr.length > WORD_LENGTH)
              item.Content[2].ContentStr = main.replaceCardInfoString(item.Content[2].ContentStr.substr(0, WORD_LENGTH)) + "...";
      
            var imageUrl = getImage(item.Content[1].Content);
            if (imageUrl.length == 0 && item.Content[2].Content)
              imageUrl = getImage(item.Content[2].Content);
            if (imageUrl.length == 0 && item.Content[3].Content)
              imageUrl = getImage(item.Content[3].Content);
      
            if (imageUrl.length > 0) {
              item.ImageUrlTemp = main.getTempImage(imageUrl);
              item.ImageUrlServer = imageUrl;
            }
          }
          that.setData({
            SearchCardList:list,
          });
          app.globalData.CardList2=list;
        }
        else{
          that.setData({
            SearchCardList:[],
          });
        }
      }
    });

    function getImage(str) {
      var result = "";
      if (str.indexOf("[图") >= 0) {
        result = str.substring(str.indexOf("[图") + 3, str.indexOf("[/图]"));
        result = result.substring(result.indexOf("]") + 1);
      }
      //console.log(result);
      return result;
    }
  },

  clearInput: function () {
    this.setData({
      IsStart: false,
      Searching: false,
      SearchInfo: "",
      Focus:true,
    });
  },
  close: function (e) {
    wx.navigateBack({
      delta: 1,
    });
  },
  deleteItem: function (e) {
    var that = this;
    that.setData({
      SearchTextList: [],
    });
    wx.removeStorageSync("SearchTextList");
  },
  deleteItem2: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.SearchWord3.splice(index, 1);
    that.setData({
      SearchWord3: that.data.SearchWord3,
    });
    wx.setStorageSync("SearchWord3", that.data.SearchWord3);

  },

  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
});