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

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

Page({
  data: {
    List: [
      {
        ImageUrl: "home_plate_11.png",
        Url: "../main/list?id=1",
      },
      {
        ImageUrl: "home_plate_12.png",
        Url: "../main/list?id=2",
      },
      {
        ImageUrl: "home_plate_21.png",
        Url: "../main/list?id=3",
      },
      {
        ImageUrl: "home_plate_22.png",
        Url: "../main/list?id=4",
      },
      {
        ImageUrl: "home_plate_31.png",
        Url: "../main/list?id=5",
      },
      {
        ImageUrl: "home_plate_32.png",
        Url: "../main/list?id=6",
      },
      {
        IsAd:1,
      },
      {
        ImageUrl: "home_plate_41.png",
        Url: "../main/list?id=7",
      },
      {
        ImageUrl: "home_plate_42.png",
        Url: "../main/list?id=8",
      },
      
      {
        ImageUrl: "home_plate_51.png",
        Url: "../main/list?id=9",
      },
      {
        ImageUrl: "home_plate_52.png",
        Url: "../main/list?id=10",
      },
      {
        ImageUrl: "home_plate_61.png",
        Url: "../main/list?id=11",
      },
      {
        ImageUrl: "home_plate_62.png",
        Url: "../main/list?id=12",
      },
    ],
    NickName: "陌生用户",
    IsMember: 1,
    NewUserNumber: 2,
    AvatarUrl: "",
    DayNumber: "0",
    ReviewCount: "0",
    FinishedCount: "0",
    IsAccredit: false,
    IsMemberForeverRemind: 0,
    IsMiaoguoAD: false,
    IsMiaoguoAD2:0,
    IsShowGift:0,  
    ProgramName:app.globalData.ProgramName,
    version: app.globalData.Version,
  },
  onLoad: function (options) {
    //app.globalData.userInfo.UserID=5;
    this.setData({
      Height: common.getSystemHeight(),
      ListProgram: server.getProgramListOld(),
      IsShowGift:app.globalData.userInfo.IsShowGift,
    });

  },
  onShow: function () {
    this.setData({
      IsLocked: app.globalData.IsLocked,
      IsShow: app.globalData.userInfo.IsShow,
      IsMember: app.globalData.userInfo.IsMember,
      NickName: app.globalData.userInfo.NickName,
      AvatarUrl: app.globalData.userInfo.AvatarUrl,
    });
    
    
    this.getData();

    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 = "";
    }
  },
  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({
          IsAccredit:true,
          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);
        }
      });
    }
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });

  },
  gotoSearch: function (e) {
    wx.navigateTo({
      url: "../main/search",
    });
  },
  gotoReview: function () {
    wx.navigateTo({
      url: '../main/review',
    });
  },
  gotoTest: function (e) {
    wx.navigateTo({
      url: '../main/test',
    });
  },
  switchProgram: function (e) {
    wx.navigateToMiniProgram({
      appId: e.currentTarget.dataset.appid,
      path: "pages/index/index?SourceID="+app.globalData.ProgramID,
      extraData: {
      },
      success(res) {
        // 打开成功
      }
    });
  },
  gotoMemberCenter: function () {
    if (app.globalData.userInfo.IsShow>=0) {
      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,
        });
      }

    }
  },
  close: function (e) {
    this.setData({
      IsMiaoguoAD2: 0,
      IsShowGift:0,
    });
    var num=e.currentTarget.dataset.num;
    if (num>0){
      var time = common.addDate("d", Number(num), new Date());
      time = common.formatTime(time);
      wx.setStorageSync("miaoguoADTime", time);
    }
    app.globalData.userInfo.IsShowGift=0;
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: 'pages/index/index?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})