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

const app = getApp();

Page({
  data: {
    IsIPhoneX: app.globalData.IsIPhoneX,
    IsYearReport:false,
  },
  onLoad: function () {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
      AvatarUrl: app.globalData.userInfo.AvatarUrl,
      NickName: app.globalData.userInfo.NickName,
      IsShow: app.globalData.userInfo.IsShow,
    });

    var year=new Date(app.globalData.userInfo.CreateTime).getFullYear();
    //console.log(year);
    if (new Date().getFullYear()>=2020 && year<2020){
      that.setData({
        IsYearReport: true,
      });
    }
    
  },
  onShow:function(){
    this.init();
  },
  init:function(){
    var that=this;
    main.getData("GetUserCardCount?UserID="+app.globalData.userInfo.UserID, function (data) {
      if (data) {
        that.setData({
          CardNumber: data.CardNumber,
        });

        if (data.UrgentNumber){
          app.globalData.TaskToday.CardNumberUrgent=data.UrgentNumber;
        }
      }
    });
    
  },
  gotoDefault:function(){
    wx.reLaunch({
      url: './default',
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: './' + url,
    })
  },
  onSearch: function (e) {
    wx.navigateTo({
      url: './searchCard1',
    })
  },
  gotoUserbook: function (e) {
    wx.navigateTo({
      url: '../other/userbook',
    })
  },
  gotoAnnualReport: function (e) {
    wx.navigateTo({
      url: '../other/annualreport',
    })
  },
  getList: function (e) {
    wx.showLoading({
      title: '请稍候',
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 5000);
    
    var IsToday=e.currentTarget.dataset.id;
    var that = this;

    if (IsToday==1){
      main.getTaskTodayList(function(task){
        var list = [];
        for (var i = 0; i < task.ListNew.length; i++) {
          list.push(task.ListNew[i]);
        }
        for (var i = 0; i < task.ListHistory.length; i++) {
          list.push(task.ListHistory[i]);
        }
        for (var i = 0; i < task.ListReview.length; i++) {
          list.push(task.ListReview[i]);
        }
        app.globalData.CardList=list;
        wx.navigateTo({
          url: './searchCardList?type=1&Count=' + list.length,
        });
      });
    }
    else{
      var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID;
      var isCollect = e.currentTarget.dataset.iscollect;
      if (!isCollect)
        isCollect = 0;
      url += "&IsCollect=" + isCollect;

      var cardType = 2;
      if (e.currentTarget.dataset.cardtype){
        cardType = e.currentTarget.dataset.cardtype;
        url += "&CardType=" + cardType;
      }

      main.getData(url, function (data) {
        wx.hideLoading();
        if (data) {
          app.globalData.CardList= data.List;
          wx.navigateTo({
            url: './searchCardList?type=0&Count=' + data.Count+'&IsCollect='+isCollect+'&CardType='+cardType,
          });
        }
      });
    }
  },
  gotoAccredit:function(){
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']){
          wx.navigateTo({
            url: '../index/accredit',
          })
        }
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
