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

const app = getApp();

Page({
  data: {
    IsTempCardNoSaved: false,
    IsExit: false,
    DayNumber: 0,
    TodayTaskNumber: 0,
    IsStart: 0,
    IsMaintain: 0,
    IsIPhoneX: app.globalData.IsIPhoneX,
    ImagePath: app.globalData.uploadImageUrl,
    IsShowUserID: false,
    VersionID:app.globalData.Version,
    AwardDayNumber:0,
    IsShowRemind:false,
  },
  onPullDownRefresh: function () {
    wx.redirectTo({
      url: '../main/default',
    })
    wx.stopPullDownRefresh();
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

    if (app.globalData.IsIPad) {
      that.setData({
        IsIPad: "_iPad",
      });

      if (app.globalData.systemInfo.windowHeight < app.globalData.systemInfo.windowWidth) {
        that.setData({
          IsExit: true,
        });
      }
    }

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })

  },
  onHide: function () {
    wx.hideLoading();
  },
  onShow: function () {
    var that = this;
    if (app.globalData.userInfo.IsNewUser && app.globalData.userInfo.IsNewUser == 1 && app.globalData.userInfo.IsShow == 1) {
      if (app.globalData.userInfo.Subscribe == 0) {
        wx.redirectTo({
          url: '../other/newuser',
        });
      }
    }
    else {
      wx.getSetting({
        success(res) {
          if (res.authSetting['scope.userInfo'] || app.globalData.userInfo.IsShow == 0) {
            if (!app.globalData.userInfo.IsMember && app.globalData.userInfo.IsShow == 1) {
              if (app.globalData.userInfo.Subscribe == 1) {
                wx.navigateTo({
                  url: '../other/userbook',
                });
                app.globalData.GotoUrl = "";
              }
              else {
                wx.redirectTo({
                  url: '../other/newuser',
                });
              }
            }
            else if (app.globalData.userInfo.IsMember && app.globalData.userInfo.IsShow == 1) {
              if (app.globalData.GotoUrl.indexOf("../other/pay?PayType=6") >= 0) {
                wx.navigateTo({
                  url: app.globalData.GotoUrl,
                });
                app.globalData.GotoUrl = "";
              }
              else {
                var productServiceTime = common.formatDateCHS(common.formatTime(new Date(app.globalData.userInfo.ProductServiceTime)));
                var today = common.formatDateCHS(common.formatTime(new Date()));
                if (productServiceTime < today) {
                  wx.redirectTo({
                    url: '../other/userbook',
                  });
                }
                else if (app.globalData.userInfo.ClassDayNumber < 0) {
                  wx.redirectTo({
                    url: '../other/userbook',
                  });
                }
                else if (app.globalData.GotoUrl) {
                  //console.log(app.globalData.GotoUrl);
                  wx.navigateTo({
                    url: app.globalData.GotoUrl,
                  });
                  app.globalData.GotoUrl = "";
                }
              }

            }
            else if (app.globalData.userInfo.IsShow == 0) {
              main.buildInitData(function () { });
            }

            that.setData({
              IsAccredit: true,
            });

            that.init();

            that.getTaskTodayList();

            that.isNoSavedCard();

          }
          else {
            wx.navigateTo({
              url: '../index/accredit',
            });
          }

          if (res.authSetting['scope.record'] === true) {
            app.globalData.IsRecorderAccredit = 1;
          }
          else if (res.authSetting['scope.record'] === false) {
            app.globalData.IsRecorderAccredit = -1;
          }
        }
      });
    }
    
    app.globalData.TempStr2=null;

    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: "#3157BA",
    });
  },
  init: function () {
    //显示练习模式
    var cardType = wx.getStorageSync("CardType");
    if (!cardType)
      cardType = 0;
    this.setData({
      CardType: cardType,
    });

    //题卡主题色
    var arrColorIndex = wx.getStorageSync("ColorIndexArr");
    if (!arrColorIndex) {
      app.globalData.ColorIndex = common.random(1, 4);
    }
    else {
      app.globalData.ColorIndex = arrColorIndex[common.random(0, arrColorIndex.length - 1)];
    }
    //显示答案操作方式
    var clickType = wx.getStorageSync("ClickType");
    if (!clickType)
      clickType = 0;
    app.globalData.ClickType = clickType;

    //选择建议
    var SecondConfigArray = wx.getStorageSync("SecondConfigArray");
    if (SecondConfigArray)
      app.globalData.SecondConfigArray = SecondConfigArray;

    //显示命名方式
    var MemoryLevel = wx.getStorageSync("MemoryLevel");
    if (!MemoryLevel)
      MemoryLevel = 0;
    app.globalData.MemoryLevel = MemoryLevel;

  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });

    if (url == "../marketing/index") {
      this.setData({
        IsShowRemind: false,
      });
      wx.setStorageSync("IsShowMarketingTime", common.formatTime(common.addDate("d", 30, new Date())));
    }

    if (url == "../plan/studyplan" && this.data.DayNumber<=app.globalData.IsShowSettingRemind){
      wx.setStorageSync('IsShowSettingRemind', this.data.DayNumber);
    }
  },
  //得到当天任务
  getTaskTodayList: function () {
    var that = this;
    main.UpdateMiaoguoCardTodayAll(true, function () {
      main.getTaskTodayList(function (data) {
        if (data.IsMaintain == 1) {
          that.setData({
            IsMaintain: 1,
          });
        }
        else {
          var TodayTaskNumber = data.ListNew.length + data.ListReview.length + data.ListHistory.length;
          that.setData({
            DayNumber: data.DayNumber,
            TodayTaskNumber: TodayTaskNumber,
            IsStart: data.IsStart,
          });

          if(app.globalData.MemoryLevel==0 && data.DayNumber>=8){
            var IsShowPatternMenu = wx.getStorageSync("IsShowPatternMenu");
            if (!IsShowPatternMenu){
              wx.navigateTo({
                url: '../other/menu?Type=1',
              });
              wx.setStorageSync('IsShowPatternMenu', 1);
            }
          }

          //是否显示提示任务量设置
          if (that.data.DayNumber<=app.globalData.IsShowSettingRemind){
            var IsShowSettingRemind = wx.getStorageSync("IsShowSettingRemind");
            if (!IsShowSettingRemind || IsShowSettingRemind<that.data.DayNumber){
              that.setData({
                IsShowSettingRemind: 1,
              });
            }
            else{
              that.setData({
                IsShowSettingRemind: 0,
              });
            }
          }

          //判断是否出现推广广告
          common.getStorageValue(that, "IsShowMarketingTime", "2020-01-01 00:00:00", function () {
            if (that.data.IsShowMarketingTime<common.formatTime(new Date())) {
              main.getAwardData(function(data){
                var award=data.Total-data.AwardDayNumber
                that.setData({
                  IsShowRemind: true,
                  AwardDayNumber:award,
                });
              });

              //判断每30天是否是高级模式
              wx.removeStorageSync('IsShowPatternMenu');
            }
          });
        }
      });
    });
  },
  isNoSavedCard: function () {
    var that = this;
    //判断是否有系统故障
    if (app.globalData.userInfo.IsMaintain == 1) {
      that.setData({
        IsMaintain: app.globalData.userInfo.IsMaintain,
      });
      wx.setNavigationBarColor({
        frontColor: "#ffffff",
        backgroundColor: "#0B8457",
      })
    }
    //判断是否有未保存的卡
    else if (wx.getStorageSync("TempCardNoSaved")) {
      wx.navigateTo({
        url: '../other/recover',
      });
    }
    else {
      that.setData({
        IsTempCardNoSaved: false,
      });
      wx.setNavigationBarColor({
        frontColor: "#ffffff",
        backgroundColor: "#3157BA",
      });
    }
  },
  
  gotoCardmain: function () {
    wx.reLaunch({
      url: './cardmain',
    });
  },
  showUserID:function(){
    var that = this;
    if (app.globalData.userInfo.UserID == 1
      || app.globalData.userInfo.UserID == 3 
      || app.globalData.userInfo.UserID == 4) {
        var userid=2;
        if (app.globalData.userInfo.UserID>=3)
          userid = app.globalData.userInfo.UserID;

      that.setData({
        IsShowUserID: true,
        ChangeUserID:userid,
      });
    }
  },
  onKeyInput: function (e) {
    var id = e.detail.value;
    var usertype = e.currentTarget.dataset.type;
    
    var that = this;
    if (usertype=="userid"){
      that.setData({
        ChangeUserID: id,
      });
    }
    else if (usertype=="newuserid"){
      that.setData({
        ChangeNewUserID: id,
      });
    }
    else if (usertype=="introduceruserid"){
      that.setData({
        ChangeIntroducerUserID: id,
      });
    }
    else if (usertype=="versionid"){
      that.setData({
        VersoinID: id,
      });
    }
  },
  setSelectUser: function (e) {
    var that = this;
    if (app.globalData.userInfo.UserID == 1
      || app.globalData.userInfo.UserID == 2 
      || app.globalData.userInfo.UserID == 3 
      || app.globalData.userInfo.UserID == 4) {
      var userid = that.data.ChangeUserID;
      wx.setStorageSync("UserID", userid);
      
      app.globalData.userInfo.UserID = userid;
      var fieldStr="&FieldStr=NickName,ProductServiceTime,AvatarUrl";
      that.getUserInfo(userid,fieldStr,function(data){
        wx.setNavigationBarTitle({
          title: data.NickName
        });
        main.getUserConfig();
        app.globalData.userInfo.ProductServiceTime=data.ProductServiceTime;
        
        if (app.globalData.userInfo.UserID!=2){
          app.globalData.userInfo.NickName=data.NickName;
          app.globalData.userInfo.AvatarUrl=data.AvatarUrl;
        }
      });
      
      that.setData({
        IsShowUserID: false,
      });
      that.onShow();
    }
  },
  getUserInfoFunction:function(e){
    var that = this;
    var userid=e.currentTarget.dataset.id;
    var type1=e.currentTarget.dataset.type;
    var fieldStr="&FieldStr=NickName,AvatarUrl";
    if (userid){
      that.getUserInfo(userid,fieldStr,function(data){
        if (type1==1){
          that.setData({
            IntroducerAvatar: data.AvatarUrl,
            IntroducerNickName: data.NickName,
          });
        }
        else{
          that.setData({
            NewUserAvatar: data.AvatarUrl,
            NewUserNickName: data.NickName,
          });
        }
      });
    }
  },
  getUserInfo:function(userID,fieldStr,callback){
    var url = "GetMiaoguoWXUserInfo?UserID=" + userID + fieldStr;
    main.getData(url, function (data) {
      if (data) {
        callback(data);
      }
    });
  },
  setUserIntroducer: function (e) {
    var that = this;
    if (that.data.ChangeNewUserID && that.data.ChangeIntroducerUserID){
      if (app.globalData.userInfo.UserID == 1
        || app.globalData.userInfo.UserID == 2 
        || app.globalData.userInfo.UserID == 3 
        || app.globalData.userInfo.UserID == 4) {
          var url = "UpdateIntroducer?UserID=" + that.data.ChangeNewUserID + "&Introducer="+that.data.ChangeIntroducerUserID;
          main.getData(url, function (data) {
            that.setData({
              ChangeNewUserID: null,
              ChangeIntroducerUserID: null,
            });
            wx.showToast({
              title: '介绍人更改成功',
            })
          });
      }
    }
  },
  closeRemind:function(){
    this.setData({
      IsShowRemind: false,
    });
    wx.setStorageSync("IsShowMarketingTime", common.formatTime(common.addDate("d", 30, new Date())));
  },
  setVersionStart:function(e){
    var flag=e.currentTarget.dataset.flag;
    var versionid=e.currentTarget.dataset.versionid;
    var url = "UpdateVersionStatus?ProgramID=166&Version="+versionid+"&Flag=" + flag;
    main.getData(url, function (data) {
      wx.showToast({
        title: '修改成功！',
      })
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
  onShareTimeline: function () {
    return this.onShareAppMessage();
  },
})
