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

const app = getApp();

Page({
  data: {
    ImagePath: app.globalData.uploadImageUrl,
    IsIPhoneX: app.globalData.IsIPhoneX,
    IsYearReport:true,
    IsShowOrder:false,
    IsIPad:app.globalData.IsIPad,
  },
  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()>=2021 && year<2020){
      that.setData({
        IsYearReport: true,
      });
    }
   
  },
  onShow:function(){
    this.init();

    if (wx.getStorageSync("TempCardNoSaved")) {
      wx.navigateTo({
        url: '../other/recover',
      });
    }
  },
  init:function(){
    var that=this;
    main.getData("GetUserCardCount?UserID="+app.globalData.userInfo.UserID, function (data) {
      if (data) {
        that.setData({
          CardNumber: data.CardNumber,
          NormalNumber:data.NormalNumber,
          UrgentNumber:data.UrgentNumber,
        });

        if (data.UrgentNumber){
          app.globalData.TaskToday.CardNumberUrgent=data.UrgentNumber;
        }

        that.getFolderList();
      }
    });

    app.globalData.TempStr2=null;
  },
  getFolderList:function(e){
    var that=this;
    var list=app.globalData.FolderList;
    common.getStorageValue(that, "FolderOrder", ["","","PanelBtnSelect","","",""], function () {
      if (list.length==0 || e){
        var order=that.data.FolderOrder;
        var url="GetUserFolderList?UserID="+app.globalData.userInfo.UserID;
        if (e){
          url+="&OrderType="+e.currentTarget.dataset.order;
          order=["","","","","",""];
          order[e.currentTarget.dataset.index]="PanelBtnSelect";
        }
        
        main.getData(url, function (data) {
          if (data) {
            var arr=data;
            var item;
            for(var i=0;i<arr.length;i++){
              item=arr[i];
              item.Width=item.IconName.substring(item.IconName.lastIndexOf("_")+1,item.IconName.lastIndexOf("x"));
              item.Height=item.IconName.substring(item.IconName.lastIndexOf("x")+1);
            }
            that.setData({
              FolderArr:data,
              IsShowOrder:false,
              FolderOrder:order,
            });
            app.globalData.FolderList=data;
            wx.setStorageSync('FolderOrder', order);
          }
        });
      }
      else{
        that.setData({
          FolderArr:app.globalData.FolderList,
        });
      }
    });
  },
  gotoDefault:function(){
    wx.reLaunch({
      url: './default',
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: './' + url,
    })
  },
  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: './mainlist?type=1&Count=' + list.length,
        });
      });
    }
    else{

      if (e.currentTarget.dataset.datatype=="delete"){
        var url = 'GetMiaoguoCardDeleteList?UserID=' + app.globalData.userInfo.UserID+"&Type=app";
        main.getData(url, function (data) {
          wx.hideLoading();
          if (data) {
            app.globalData.CardList= data.List;
    
            wx.navigateTo({
              url: './mainlist?type=0&CardType=-2&Count='+data.List.length,
            });
          }
        });
      }
      else{
        var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID;
        var url2="";

        var folderid = e.currentTarget.dataset.folderid;
        if (!folderid){
          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;
          }

        }
        else{
          url += "&FolderID=" + folderid;
        }
  
        main.getData(url, function (data) {
          wx.hideLoading();
          if (data) {
            app.globalData.CardList= data.List;
            url2='./mainlist?type=0&Count=' + data.Count+'&IsCollect='+isCollect+'&CardType='+cardType;
            if (folderid){
              var name=e.currentTarget.dataset.name;
              if (name.indexOf("????")>=0){
                name=name.replace("????","");
              }
              url2='./mainlist?type=0&Count=' + data.Count+'&FolderID='+folderid;
              url2+='&ColorName='+e.currentTarget.dataset.color;
              url2+='&IconName='+e.currentTarget.dataset.icon;
              url2+='&Name='+name;
            }
            wx.navigateTo({
              url: url2,
            });
          }
        });
      }
    }
  },
  gotoAccredit:function(){
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']){
          wx.navigateTo({
            url: '../index/accredit',
          })
        }
      }
    });
  },
  openOrder:function(){
    this.setData({
      IsShowOrder:true,
    });
  },
  closeHelp:function(){
    this.setData({
      IsShowOrder:false,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
