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

const app = getApp();
var isPaying = false;

Page({
  data: {
  },
  onLoad: function (options) {
    var bookID = options.id;
    this.setData({
      Height: common.getSystemHeight(),
      NavClass1: "nav1",
      NavClass2: "nav2",
      IsShowNav1: true,
      BookID: bookID,
      IsShow: app.globalData.IsShow,
    });
    common.getStorageValue(this, "IsRemind", true, function () { });
  },
  onShow: function () {
    if (app.globalData.IsRefresh) {
      wx.showLoading({
        title: '请稍候',
        mask: true,
      });
      setTimeout(function () {
        wx.hideLoading();
      }, 3000);
      app.globalData.IsRefresh = false;
    }
    this.init();
  },
  init: function () {
    var arr = JSON.parse(wx.getStorageSync("HanziAll"));
    var arrReview = wx.getStorageSync("ReviewList");
    var arrFinished = wx.getStorageSync("FinishedList");

    var result;
    for (var i = 0; i < arr.length; i++) {
      if (arr[i].ID == this.data.BookID) {
        result = arr[i];
        for (var j = 0; j < result.Units.length; j++) {
          var item = result.Units[j];
          if (app.globalData.userInfo.IsMember == 1)
            item.IsLocked = 0;
          
          item.IsFinished = false;
          for (var k = 0; k < arrFinished.length; k++) {
            if (item.ID == arrFinished[k].UnitID) {
              item.IsFinished = true;
              break;
            }
          }

          item.WordLength = item.Words.length;

          for (var k = 0; k < item.Words.length; k++) {
            item.Words[k].ReviewCss = "";
            for (var l = 0; l < arrReview.length; l++) {
              if (arrReview[l].ID == item.Words[k].ID) {
                item.Words[k].ReviewCss = "boxSelect";
                break;
              }
            }
          }

          if (item.WordLength % 6 > 0) {
            var count = 6 - (item.WordLength % 6);
            //console.log(count);
            for (var n = 0; n < count; n++) {
              var obj = {
                Name: "",
                ReviewCss: "boxNull",
              };
              item.Words.push(obj);
            }
          }
        }
        break;
      }
    }
    var imgNum = this.data.BookID;
    if (imgNum == 5)
      imgNum = "11";
    else if (imgNum == 6)
      imgNum = "12";
    else
      imgNum = "0" + imgNum;
    this.setData({
      List: result.Units,
      topImage: "../images/040" + imgNum + ".png"
    });
    wx.setNavigationBarTitle({
      title: result.Name,
    });

  },
  selectNav: function (e) {
    var id = e.currentTarget.dataset.id;
    //console.log(id);
    if (id == "1") {
      this.setData({
        NavClass1: "nav1",
        NavClass2: "nav2",
        IsShowNav1: true,
      })
    }
    else if (id == "2") {
      this.setData({
        NavClass1: "nav2",
        NavClass2: "nav1",
        IsShowNav1: false,
        IsRemind:false
      });
      wx.setStorageSync('IsRemind', false);
    }
  },
  goto: function (e) {
    var islocked = e.currentTarget.dataset.islocked;
    if (islocked == 1 && app.globalData.IsShow == 1) {
      wx.navigateTo({
        url: '../../package4/main/buyad',
      })
    }
    else {
      var word = e.currentTarget.dataset.word;
      if (word) {
        var bookid = e.currentTarget.dataset.bookid;
        var unitid = e.currentTarget.dataset.unitid;
        var wordid = e.currentTarget.dataset.wordid;
        var isfinished = e.currentTarget.dataset.isfinished;

        if (!wordid)
          wordid = 0
        else {
          wx.setStorageSync('SelectedRandom', false);
        }
        wx.navigateTo({
          url: "../main/detail?bookid=" + bookid + "&unitid=" + unitid + "&wordid=" + wordid + "&isfinished=" + isfinished,
        });
      }
    }
  },
  getFinished: function () {
    var that = this;
    server.getData('GetHanziFinishedData?Category=' + this.data.BookID + '&UserID=' + app.globalData.userInfo.UserID, function (data) {
      if (data) {
        var list = that.data.List;
        var arrReview = wx.getStorageSync("ReviewList");

        for (var i = 0; i < list.length; i++) {
          list[i].IsFinished = false;
          for (var j = 0; j < data.length; j++) {
            if (list[i].ID == data[j].Title) {
              list[i].IsFinished = true;
              break;
            }
          }

          var item = list[i];
          for (var k = 0; k < item.Words.length; k++) {
            item.Words[k].ReviewCss = "";
            for (var l = 0; l < arrReview.length; l++) {
              if (arrReview[l].WordID == item.Words[k].ID) {
                item.Words[k].ReviewCss = "boxSelect";
                break;
              }
            }
          }
        }
        that.setData({
          List: list,
        })
      }
    });
  },
  gotoIndex: function () {
    wx.setStorageSync("indexNav", 2);
    wx.navigateBack({
      delta: 1
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})