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

const app = getApp();
var interval = 0;
var isPaying = false;
var hanziNumber = 0;

Page({
  data: {
    Version: app.globalData.Version,
    ListProgram: server.getProgramListOld(),
    NickName: "陌生用户",
    IsMember: 1,
    NewUserNumber: 2,
    AvatarUrl: "",
    DayNumber: "0",
    ReviewCount: "0",
    FinishedCount: "0",
    IsAccredit: false,
    NavClass1: "",
    NavClass2: "nav2",
    IsRemind: false,
    IsMemberForeverRemind: 0,
  },
  onReady: function (e) {
  },
  onLoad: function (options) {
    //app.globalData.userInfo.UserID=5;
    this.setData({
      Height: common.getSystemHeight(),
      NickName: app.globalData.userInfo.NickName,
      AvatarUrl: app.globalData.userInfo.AvatarUrl,
    });

    var that = this;
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          that.setData({
            IsAccredit: true,
          });
        }
        else {
          wx.navigateTo({
            url: './accredit',
          });
        }
      }
    })

    common.getStorageValue(this, "IsRemind", true, function () { });
  },
  onShow: function () {
    this.setData({
      IsLocked: app.globalData.IsLocked,
      IsShow: app.globalData.IsShow,
      IsMember: app.globalData.userInfo.IsMember
    });

    if (wx.getStorageSync("HasActivate") == 1) {
      wx.navigateTo({
        url: '../../package4/main/activate',
      });
    }
    else {
      this.getData();

      if (wx.getStorageSync("indexNav")) {
        var obj = {
          currentTarget: {
            dataset: {
              id: 2,
            }
          }
        }
        this.selectNav(obj);
        wx.removeStorageSync("indexNav");
      }


      if (app.globalData.goto) {
        var url = "../main/list?id=1";
        if (app.globalData.goto == "test")
          url = '../main/test';
        wx.navigateTo({
          url: url,
        });
        app.globalData.goto = "";
      }
      else {
        var IsRemindTest = wx.getStorageSync("IsRemindTest");
        if (IsRemindTest == 1) {
          wx.setStorageSync('IsRemindTest', 2);
          wx.navigateTo({
            url: '../main/test',
          });
        }
      }
    }

  },
  getData: function () {
    var that = this;
    server.getData('GetHanziRecordData?UserID=' + app.globalData.userInfo.UserID + "&Version=" + app.globalData.Version, function (data) {

      if (data) {

        if (!data.DayNumber)
          data.DayNumber = 0;
        var finishedCount = 0;
        if (data.FinishedList)
          finishedCount = data.FinishedList.length;
        var reviewCount = 0;
        if (data.ReviewList)
          reviewCount = data.ReviewList.length;

        hanziNumber = 0;
        var arr = JSON.parse(wx.getStorageSync("HanziAll"));
        for (var k = 0; k < data.FinishedList.length; k++) {
          for (var i = 0; i < arr.length; i++) {
            var b1 = false;
            for (var j = 0; j < arr[i].Units.length; j++) {
              var item = arr[i].Units[j];
              if (item.ID == data.FinishedList[k].UnitID) {
                hanziNumber += item.Words.length;

                for (var n = 0; n < item.Words.length; n++) {
                  for (var l = 0; l < data.ReviewList.length; l++) {
                    if (data.ReviewList[l].WordID == item.Words[n].ID) {
                      hanziNumber--;
                    }
                  }
                }

                b1 = true;
                break;
              }

            }
            if (b1)
              break;
          }
        }
        if (!hanziNumber) {
          hanziNumber = 0;
        }
        that.setData({
          DayNumber: data.DayNumber,
          HanziNumber: hanziNumber,
          FinishedCount: finishedCount,
          ReviewCount: reviewCount,
        });
        wx.setStorageSync("FinishedList", data.FinishedList);
        wx.setStorageSync("ReviewList", data.ReviewList);
      }
    });

    var UnitCustom = wx.getStorageSync("UnitCustom");
    if (!UnitCustom) {
      var that = this;
      server.getData('GetHanziCustomList?UserID=' + app.globalData.userInfo.UserID, function (data) {
        if (data && data.length > 0) {
          for (var n = 0; n < data.length; n++) {
            if (data[n].List)
              data[n].List = JSON.parse(data[n].List);
          }
          wx.setStorageSync("UnitCustom", data);
        }
      });
    }
  },
  selectNav: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    //console.log(id);
    if (id == "1") {
      this.setData({
        NavClass1: "",
        NavClass2: "nav2",
      })
    }
    else if (id == "2") {
      this.setData({
        NavClass1: "nav2",
        NavClass2: "nav3",
        IsRemind: false,
      })
      wx.setStorageSync('IsRemind', false);
      wx.pageScrollTo({
        scrollTop: 0
      });

      var url = e.currentTarget.dataset.url;
      if (url == "orderlist") {
        setTimeout(function () {
          that.gotoAllPay();
        }, 1000);
      }
    }
  },
  goto: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../main/list?id=" + id,
    });
  },
  gotoSearch: function (e) {
    wx.navigateTo({
      url: "../main/search",
    });
  },
  gotoReview: function () {
    if (this.data.IsShow == 1 && this.data.IsMember == 0) {
      wx.navigateTo({
        url: '../../package4/main/buyad',
      })
    }
    else {
      wx.navigateTo({
        url: '../main/review',
      });
    }
  },
  gotoTest: function (e) {
    wx.navigateTo({
      url: '../main/test',
    });
  },
  switchProgram: function (e) {
    wx.navigateToMiniProgram({
      appId: e.currentTarget.dataset.appid,
      path: e.currentTarget.dataset.path,
      extraData: {
      },
      success(res) {
        // 打开成功
      }
    });
  },
  gotoMemberCenter: function () {
    wx.navigateTo({
      url: './userinfo',
    });
  },
  updateMember: function () {
    if (app.globalData.userInfo.UserID == 1) {
      if (app.globalData.userInfo.IsMember == 1) {
        app.globalData.userInfo.IsMember = 0;
        this.setData({
          IsMember: false,
        });
      }
      else {
        app.globalData.userInfo.IsMember = 1;
        this.setData({
          IsMember: true,
        });
      }

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