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: {
    IsChangeStudyPLan: false,
    IsNullCard:false,
    BtnArray: [{
      Name: "日常练习",
      Name2: "",
      Selected: 1
    }, {
      Name: "紧急练习",
      Name2: "",
      Selected: 0
    }, {
      Name: "整体练习",
      Name2: "串联将日常练习方案和紧急练习方案的任务量",
      Selected: 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(),
    });

    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;

      that.setData({
        BtnArray: that.data.BtnArray,
      });
    });
    var studyPlan = app.globalData.TaskToday;
    var taskNumber = studyPlan.ListNew.length + studyPlan.ListReview.length + studyPlan.ListHistory.length;
    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);

  },
  btnSelected: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

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

    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#0B8457',
    });

  },
  changePlan: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;

    if (((id == 1 || id == 2) && cardNumberUrgent == 0) 
    || ((id == 0 || id == 2) && cardNumberCommon == 0)) {
      that.setData({
        IsNullCard: true,
        IsChangeStudyPLan: false,
      });
    } else {
      if (id!=that.data.CardType){
        that.setData({
          CardType: id,
        });
        wx.setStorageSync("CardType", that.data.CardType);
  
        main.UploadUserConfig(function () {
          main.getTaskTodayList(function (data) {
            that.init();
            that.close();
          });
        });
      }
      else{
        that.close();
      }
    }
  },
  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,
    });
  },
  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/searchCardList?type=1&Count=' + list.length,
        });
      });
    } else {
      var url = 'GetMiaoguoCardList2?UserID=' + app.globalData.userInfo.UserID + "&IsToday=2&CardType=" + that.data.CardType;
      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/searchCardList?type=2&Count=' + list.length,
          });
        }
      });
    }

  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})