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

const app = getApp();
var cardNumberCommon = 0;
var cardNumberUrgent = 0;

Page({
  data: {
    ImagePath: app.globalData.uploadImageUrl,
    IsChangeStudyPLan: false,
    IsNullCard:false,
    BtnArray: [{
      Name: "日常练习模式",
      Name2: "",
      CSS0:["HelpBtn11","richang_normal_b_60x60"],
      CSS1:["HelpBtn12","richang_normal_a_60x60"],
      CSS2:["HelpBtn13","richang_disabled_60x60"],
      Selected: 1
    }, {
      Name: "紧急练习模式",
      Name2: "",
      CSS0:["HelpBtn11","jinji_normal_b_60x60"],
      CSS1:["HelpBtn12","jinji_normal_a_60x60"],
      CSS2:["HelpBtn13","jinji_disabled_60x60"],
      Selected: 0
    }, {
      Name: "日常+紧急练习模式",
      Name2: "把日常和紧急的任务量整合起来练习",
      CSS0:["HelpBtn11"],
      CSS1:["HelpBtn12"],
      CSS2:["HelpBtn13"],
      Selected: 0
    }],
    IsShowHelp:false,
    HelpID:0,
  },
  onPullDownRefresh: function () {
    var that = this;
    main.getTaskTodayList(function (data) {
      that.init();
    });
    wx.stopPullDownRefresh();
  },
  onLoad: function (options) {
    var that = this;

    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

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

    that.init();
  },
  onShow: function () {
    this.init();
  },
  init: function () {
    var that = this;
    cardNumberCommon = app.globalData.TaskToday.CardNumberCommon;
    cardNumberUrgent = app.globalData.TaskToday.CardNumberUrgent;

    common.getStorageValue(that, "CardType", 0, function () {
      that.data.BtnArray[0].Selected = 0;
      that.data.BtnArray[1].Selected = 0;
      that.data.BtnArray[2].Selected = 0;

      that.data.BtnArray[that.data.CardType].Selected = 1;

      if (cardNumberCommon==0)
        that.data.BtnArray[0].Selected=2;
      if (cardNumberUrgent==0)
        that.data.BtnArray[1].Selected=2;
      if (cardNumberCommon==0 || cardNumberUrgent==0)
        that.data.BtnArray[2].Selected=2;
      
      that.setData({
        BtnArray: that.data.BtnArray,
        CurrentPlan:that.data.BtnArray[that.data.CardType].Name,
      });
    });
    var studyPlan = app.globalData.TaskToday;
    var newLength=studyPlan.ListNew.length || 0;
    var reviewLength=studyPlan.ListReview.length || 0;
    var historyLength=studyPlan.ListHistory.length || 0;
    var taskNumber = newLength + reviewLength + historyLength;
    var progress = 0;
    if (studyPlan.AlreadyPracticeNumber + taskNumber > 0)
      progress = Math.round(100 * studyPlan.AlreadyPracticeNumber / (taskNumber + studyPlan.AlreadyPracticeNumber));
    that.setData({
      StudyPlan: studyPlan,
      TaskNumber: taskNumber,
      Progress: progress,
    });

    var backColor = "#f0f0f0";

    var rate = 0;
    if (studyPlan.CommonNewLength > 0 || studyPlan.CommonOldLength > 0) {
      rate = studyPlan.CommonNewLength / (studyPlan.CommonNewLength + studyPlan.CommonOldLength);
      backColor = "#0094EE";
    }

    fanObj.init(that, rate, 100, 3.0, "#00C4EE", backColor, null);

    var backColor2 = "#f0f0f0";
    var rate2 = 0;
    if (studyPlan.UrgentNewLength > 0 || studyPlan.UrgentOldLength > 0) {
      rate2 = studyPlan.UrgentNewLength / (studyPlan.UrgentNewLength + studyPlan.UrgentOldLength);
      backColor2 = "#FF9900";
    }
    fanObj.init2(that, rate2, 100, 3.0, "#FFCC00", backColor2, null);

  },
  changePlan: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var disabled = e.currentTarget.dataset.disabled;
    if (disabled<=1){
      that.data.BtnArray[0].Selected = 0;
      that.data.BtnArray[1].Selected = 0;
      that.data.BtnArray[2].Selected = 0;

      that.data.BtnArray[id].Selected = 1;

      if (cardNumberCommon==0)
        that.data.BtnArray[0].Selected=2;
      if (cardNumberUrgent==0)
        that.data.BtnArray[1].Selected=2;
      if (cardNumberCommon==0 || cardNumberUrgent==0)
        that.data.BtnArray[2].Selected=2;
      
      that.setData({
        BtnArray: that.data.BtnArray,
      });
    }
  },
  changePlanOK: function (e) {
    var that = this;
    var id=0;
    if (that.data.BtnArray[1].Selected==1)
      id=1;
    else if (that.data.BtnArray[2].Selected==1)
      id=2;
    
    that.setData({
      CardType: id,
      CurrentPlan:that.data.BtnArray[id].Name,
    });
    wx.setStorageSync("CardType", that.data.CardType);

    main.UploadUserConfig(function () {
      main.getTaskTodayList(function (data) {
        that.init();
        that.closeHelp();
      });
    });
  },
  close: function () {
    var that = this;
    that.setData({
      IsChangeStudyPLan: false,
      IsNullCard: false,
    });
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#274DAD',
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });

    this.setData({
      IsShowHelp:false,
    });
  },
  gotoTaskList: function (e) {
    wx.showLoading({
      title: '请稍候',
    });
    setTimeout(function () {
      wx.hideLoading();
    }, 5000);

    var that = this;
    //未练完的题卡
    if (e.currentTarget.dataset.id == 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: '../main/mainlist?type=1&Count=' + list.length,
        });
      });
    } else {
      var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID + "&IsToday=2&CardType=" + that.data.CardType+"&OrderType=ac.LastTime desc";
      main.getData(url, function (data) {
        if (data) {
          var list = [];

          for (var i = 0; i < data.List.length; i++) {
            data.List[i].LastTimeStr = common.formatDateCHS(data.List[i].LastTime);

            list.push(data.List[i]);
          }
          app.globalData.CardList = list;
          wx.navigateTo({
            url: '../main/mainlist?type=2&Count=' + data.length,
          });
        }
      });
    }

  },
  showHelp:function(e){
    var id=e.currentTarget.dataset.id;
    
    this.setData({
      IsShowHelp:true,
      HelpID:id,
    });
  },
  closeHelp:function(){
    this.setData({
      IsShowHelp:false,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})