import common from '../../utils/util';
import main from '../../utils/main';
import fanObj from '../template/FanObject';
import constant from '../../utils/constant';

const app = getApp();
var historyLimitMax=5;

Page({
  data: {
    CardNumberNew: [],
    CardNumberOld: [],
  },
  onPullDownRefresh: function () {
    var that = this;
    main.UploadUserConfig(function () {
      main.getTaskTodayList(function (data) { 
        that.init();
      });
    });
    wx.stopPullDownRefresh();
  },
  onLoad: function (options) {
    var that = this;

    that.setData({
      Containnerheight: main.getWindowHeight(),
      CardType:options.CardType,
    });
    that.init();
  },
  init: function () {
    var that = this;
    var info = app.globalData.TaskToday;

    historyLimitMax=5;
    if (that.data.CardType==1) {
      historyLimitMax=50;
      wx.setNavigationBarTitle({
        title: "紧急练习模式"
      });
    }
    
    for (var i = 0; i <= 200; i++) {
      that.data.CardNumberNew.push(i);
    }
    
    for (var i = historyLimitMax; i <= 500; i++) {
      that.data.CardNumberOld.push(i);
    }

    that.setData({
      CardNumber: info.CardNumber,
      CardNumberNew: that.data.CardNumberNew,
      CardNumberOld: that.data.CardNumberOld,
    });


    var url = "GetMiaoguoCardTotal?UserID=" + app.globalData.userInfo.UserID + "&CardType=" + that.data.CardType;
    main.getData(url, function (data) {
      if (data) {

        var rate = 0, cardTotal = 0,limitNumber=0;

        if (that.data.CardType == 0) {
          if (info.CardNumber > 0) {
            limitNumber = data.NewLimitTimeToday + data.HistoryLimitTimeToday;
            cardTotal = info.CardNumberCommon;
            if (cardTotal>0)
              rate = limitNumber / cardTotal;
            else
              rate = 0;  
          }
        }
        else if (that.data.CardType == 1) {
          if (info.CardNumber > 0) {
            limitNumber = data.NewLimitTimeToday + data.HistoryLimitTimeToday;
            cardTotal = info.CardNumberUrgent;
            if (cardTotal>0)
              rate = limitNumber / cardTotal;
            else
              rate = 0;
          }
          wx.setNavigationBarTitle({
            title: "紧急练习模式"
          });
        }
        fanObj.init(that, rate, 100, 3.0, "#FE6659", "#FFC1A8", null);


        that.setData({
          LimitCardNumber:limitNumber,
          CardNumberCurrent: cardTotal,
          
          NewCardAll: data.NewCardAll,
          HistoryCardAll: data.HistoryCardAll,
          
          NewToday: data.NewToday,
          
          NewLimitTimeToday: data.NewLimitTimeToday,
          HistoryLimitTimeToday: data.HistoryLimitTimeToday,
          
          NewCardNumberAndLimitToday: data.NewCardNumberAndLimitToday,
          HistoryCardNumberAndLimitToday: data.HistoryCardNumberAndLimitToday,
        });
      }
    });
  },
  onShow:function(){
    var that = this;
    if (that.data.CardType=="0"){
      common.getStorageValue(that, "CardMaxNumberNew", 10, function () {
        that.setData({
          CardMaxNumberNewIndex: that.data.CardMaxNumberNew,
        });
      });
      common.getStorageValue(that, "CardMaxNumberHistory", 100, function () {
        that.setData({
          CardMaxNumberHistoryIndex: that.data.CardMaxNumberHistory - historyLimitMax,
        });
      });
      common.getStorageValue(that, "SortTypeIndex", 0, function () {
        that.setData({
          SortType: constant.arrSortType[that.data.SortTypeIndex],
        });
      });
    }
    else if (that.data.CardType == "1") {
      common.getStorageValue(that, "CardMaxNumberNewUrgent", 10, function () {
        that.setData({
          CardMaxNumberNewIndex: that.data.CardMaxNumberNewUrgent,
        });
      });
      common.getStorageValue(that, "CardMaxNumberHistoryUrgent", 100, function () {
        that.setData({
          CardMaxNumberHistoryIndex: that.data.CardMaxNumberHistoryUrgent - historyLimitMax,
        });
      });
    }

  },
  setTaskLimit: function (e) {
    wx.navigateTo({
      url: './taskLimit?CardType='+this.data.CardType,
    })
  },
  setSortType: function () {
    wx.navigateTo({
      url: './sortRule',
    })
  },
  bindUpdateCardNumberNewMax: function (e) {
    this.data.CardMaxNumberNewIndex = e.detail.value;

    if (this.data.CardType == "0") {
      this.setData({
        CardMaxNumberNewIndex: this.data.CardMaxNumberNewIndex,
        CardMaxNumberNew: this.data.CardNumberNew[this.data.CardMaxNumberNewIndex],
      });
      wx.setStorageSync("CardMaxNumberNew", this.data.CardNumberNew[this.data.CardMaxNumberNewIndex]);
    }
    else if (this.data.CardType == "1") {
      this.setData({
        CardMaxNumberNewIndex: this.data.CardMaxNumberNewIndex,
        CardMaxNumberNewUrgent: this.data.CardNumberNew[this.data.CardMaxNumberNewIndex],
      });
      wx.setStorageSync("CardMaxNumberNewUrgent", this.data.CardNumberNew[this.data.CardMaxNumberNewIndex]);
    }
    this.onPullDownRefresh();
  },
  bindUpdateCardNumberHistoryMax: function (e) {
    this.data.CardMaxNumberHistoryIndex = e.detail.value;

    if (this.data.CardType == "0") {
      this.setData({
        CardMaxNumberHistoryIndex: this.data.CardMaxNumberHistoryIndex,
        CardMaxNumberHistory: this.data.CardNumberOld[this.data.CardMaxNumberHistoryIndex],
      });
      wx.setStorageSync("CardMaxNumberHistory", this.data.CardNumberOld[this.data.CardMaxNumberHistoryIndex]);
    }
    else if (this.data.CardType == "1") {
      this.setData({
        CardMaxNumberHistoryIndex: this.data.CardMaxNumberHistoryIndex,
        CardMaxNumberHistoryUrgent: this.data.CardNumberOld[this.data.CardMaxNumberHistoryIndex],
      });
      wx.setStorageSync("CardMaxNumberHistoryUrgent", this.data.CardNumberOld[this.data.CardMaxNumberHistoryIndex]);
    }
    this.onPullDownRefresh();
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
