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(),
    });
  },
  onShow: function () {
    var that = this;
    var arr = wx.getStorageSync("SearchWord3");
    if (!arr)
      arr = [];

    var list = [];
    if (arr.length > 0) {
      var count=arr.length<10?arr.length:10;
      for (var i = 0; i < count; i++) {
        if (!arr[i].Type)
          arr[i].Type = "zici";
        switch (arr[i].Type) {
          case 'zici':
            arr[i].TypeName = '字词';
            break;
          case 'shici':
            arr[i].TypeName = '诗词';
            if (arr[i].Key.indexOf("《") < 0 && arr[i].Key.indexOf("》") < 0 )
              arr[i].Key="《"+arr[i].Key+"》";
            break;
          case 'eng':
            arr[i].TypeName = '翻译';
            break;
        }
        
        arr[i].ScrollLeft = 0;
        arr[i].DeleteStr = "删除";

        list.push(arr[i]);
      }
    }
    that.setData({
      SelectList: list,
    });
  },
  onSearch: function (e) {
    app.globalData.TempSearchBackNumber = 3;
    wx.navigateTo({
      url: './searchWeb1',
    })
  },
  onSearchHistory: function (e) {
    var index = Number(e.currentTarget.dataset.index);
    var obj=this.data.SelectList[index];
    main.updateSearchList(obj);
    app.globalData.TempSearchBackNumber=2;
    wx.navigateTo({
      url: './searchWeb2',
    });
  },
  DeleteItem: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var list = that.data.SelectList;
    var b = true;
    for (var i = 0; i < list.length; i++) {
      var item = list[i];
      if (index == i) {
        if (item.DeleteStr == "删除") {
          item.ScrollLeft = 160;
          item.DeleteStr = "确认删除";
        }
        else if (item.DeleteStr == "确认删除") {
          b = false;
          item.DeleteStr = "删除";
          item.ScrollLeft = 0;
          that.data.SelectList.splice(index, 1);
          that.setData({
            SelectList: that.data.SelectList,
          });
          wx.setStorageSync("SearchWord3", that.data.SelectList);
        }
      }
      else {
        item.DeleteStr = "删除";
        item.ScrollLeft = 0;
      }
    }
    if (b) {
      that.setData({
        SelectList: list,
      });
    }
  },
  deleteItem: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;

    wx.showModal({
      title: '提醒',
      content: '看过记录要删除吗？',
      success(res) {
        if (res.confirm) {
          that.data.SelectList.splice(index, 1);
          that.setData({
            SelectList:that.data.SelectList,
          });
          wx.setStorageSync("SearchWord3", that.data.SelectList);
        }
      }
    });

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