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

const app = getApp();

var historyLimitTime;

Page({
  data: {
    MenuArr: constant.arrStudyPlay,
    IsSelected:"",
    LimitTimeArr:[
      {Name:"今天",DayNumber:0},
      {Name:"明天",DayNumber:1},
      {Name:"3天后",DayNumber:3},
      {Name:"1周后",DayNumber:7},
      {Name:"1个月后",DayNumber:30},
    ],
  },
  onLoad: function (options) {
    var that = this;
    var today=common.formatTime(new Date(),"-",true);
    var limitTime=today;
    if (options.LimitTime){
      limitTime=options.LimitTime; 
    }
    historyLimitTime=limitTime;
    
    that.setData({
      Containnerheight: main.getWindowHeight(),
      CardType:options.CardType,
      CardTypeHistory:options.CardType,
      MiaoguoCardID:options.ID,
      IsSave:options.IsSave || 0,
      Today:today,
      LimitTime:limitTime,
      LimitTimeStr: common.formatDateCHS(limitTime),
      CardNumberUrgent: app.globalData.TaskToday.CardNumberUrgent,
    });
    if (app.globalData.IsIPhoneX) {
      that.setData({
        IsIPhoneX: "_IsIPhoneX",
      });
    }
  },
  changeCardType:function(e){
    var that = this;
    var cardType=e.currentTarget.dataset.cardtype;
    if (cardType!=that.data.CardType){
      if (cardType==1 || (that.data.CardTypeHistory==-1 && cardType==0)){
        var limitTime=common.formatTime(common.addDate("d",0,new Date()),"-",true);
        that.setData({
          LimitTime: limitTime,
          LimitTimeStr: common.formatDateCHS(limitTime),
          IsSelected:"_Selected"
        });
      }
      else{
        var limitTime=historyLimitTime;
        if (that.data.CardTypeHistory==-1)
          limitTime=common.formatTime(common.addDate("d",0,new Date()),"-",true);
        that.setData({
          LimitTime: limitTime,
          LimitTimeStr: common.formatDateCHS(limitTime),
          IsSelected:"",
        });
      }

      if (cardType == 1 && that.data.CardNumberUrgent>=200){
        wx.showToast({
          title: '紧急练习容量已满',
        })
      }
      else{
        that.setData({
          CardType: cardType,
        });
      }
    }
  },
  close:function(){
    wx.navigateBack({
      delta: 1,
    });
  },
  save:function(){
    var that = this;

    if (that.data.IsSave==0){
      finish(that);
    }
    else{
      var url = 'UpdateMiaoguoCardType?UserID=' + app.globalData.userInfo.UserID;
      url += "&CardType=" + that.data.CardType;
      url += "&ID=" + that.data.MiaoguoCardID;
  
      if (that.data.CardType>=0){
        if (historyLimitTime!=that.data.LimitTime){
          url+="&LimitTime="+that.data.LimitTime;
        }
      }
  
      main.getData(url, function (data) {
        finish(that);
        app.globalData.TempIsEditCardType=0;
      });
    }

    function finish(that){
      var list = app.globalData.CardList;
      for (var i = 0; i < list.length; i++) {
        if (list[i].MiaoguoCardID == that.data.MiaoguoCardID){
          list[i].CardType = that.data.CardType;
          list[i].LimitTime=that.data.LimitTime;
          list[i].LimitTimeStr=common.formatDateCHS(that.data.LimitTime);
        }
      }
      app.globalData.CardList = list;
      app.globalData.IsUpdateStudyPlan = 1;
      
      if (that.data.CardType==1){
        app.globalData.TaskToday.CardNumberUrgent++;
      }
      app.globalData.TempIsEditCardType=1;
      that.close();
    }
  },
  setLimitTime:function(e){
    var that=this;
    var limitTime="";
    var IsSelected="";
    if (e.detail.value){
      limitTime=e.detail.value;
      IsSelected="_Selected";
    }
    else{
      var DayNumber=e.currentTarget.dataset.daynumber;
      if (DayNumber==-1){
        limitTime=historyLimitTime;
        if (that.data.CardTypeHistory==-1)
          limitTime=common.formatTime(common.addDate("d",0,new Date()),"-",true);
        
        wx.showToast({
          title: '日期已还原',
          mask: true,
          image: "../images/universalpic_restore_white_120x120.png",
        });
        IsSelected="";
      }
      else{
        limitTime=common.formatTime(common.addDate("d",DayNumber,new Date()),"-",true);
        IsSelected="_Selected";
      }
    }
    that.setData({
      LimitTime: limitTime,
      LimitTimeStr: common.formatDateCHS(limitTime),
      IsSelected:IsSelected,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
