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

const app = getApp();

Page({
  data: {
    IsEdit: false,
    SelectedCount: 0,
  },
  onLoad: function (options) {
    this.setData({
      Height: common.getSystemHeight(),
    });
  },
  onShow:function(){
    this.initReview();
  },
  initReview: function () {
    var arrReview = wx.getStorageSync("ReviewList");
    if (!arrReview)
      arrReview = [];
    this.setData({
      IsEdit: false,
      List: arrReview,
    });
  },
  startEdit: function () {
    this.setData({
      IsEdit: true,
    });
  },
  gotoReview: function (e) {
    var id = e.currentTarget.dataset.id;
    var cotegory = "review";
    wx.navigateTo({
      url: '../main/detail?Type=' + cotegory + '&indexID=' + id,
    });
  },
  selectedHanzi: function (e) {
    var id = e.currentTarget.dataset.id;
    this.data.List[id].IsSelected = !this.data.List[id].IsSelected;
    var SelectedCount = 0;
    for (var i = 0; i < this.data.List.length; i++) {
      if (this.data.List[i].IsSelected)
        SelectedCount++;
    }
    this.setData({
      List: this.data.List,
      SelectedCount: SelectedCount,
    });
  },
  cancelHanzi: function () {
    var that = this;
    var arr = [];
    for (var i = 0; i < this.data.List.length; i++) {
      if (this.data.List[i].IsSelected)
        arr.push(this.data.List[i].ID);
    }
    server.postData('UpdateHanziReview', {
      UserID: app.globalData.userInfo.UserID,
      WordID: arr.join(","),
      Status: "deleteAll",
    }, function (data) {
      if (data) {
        wx.setStorageSync("ReviewList", data);
        that.initReview();
        wx.showToast({
          title: '删除成功',
        })
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
});