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

const app = getApp();

Page({
  data: {
    List: [],
  },
  onLoad: function (options) {
    var that = this;

    var userid = app.globalData.userInfo.UserID;
    if (options.UserID)
      userid = options.UserID;
      
    var EndTime = "";
    if (options.EndTime){
      EndTime = options.EndTime;
      EndTime = common.formatDateENG(EndTime,"-");
      
      var today=new Date();
      EndTime=new Date(EndTime);
      var dayNumber = common.diffDate("d", today, EndTime)+2;
      that.setData({
        EndTime: options.EndTime,
        DayNumber: dayNumber,
      });
    }

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

    this.init();

    app.globalData.Homework=0;
  },
  onPullDownRefresh: function () {
    this.init();
    wx.stopPullDownRefresh();
  },
  init:function(){
    var that = this;
    main.getData("GetMiaoguoCheckList?UserID=" + that.data.UserID, function (data) {
      if (data) {

        common.getStorageValue(that, "HomeworkListClick", [0, 0, 0], function () {

          if (that.data.HomeworkListClick[1] == 0) {
            data.List[1].Finished = 0;
            data.FinishedCount -= 10;
          }
          if (that.data.HomeworkListClick[2] == 0) {
            data.List[2].Finished = 0;
            data.FinishedCount -= 10;
          }

          that.setData({
            FinishedCount: data.FinishedCount,
            List: data.List,
            NickName: data.NickName,
            AvatarUrl: data.AvatarUrl,
          });
        });
      }
    });
  },
  clickOK:function(e){
    var that = this;
    var index=e.currentTarget.dataset.index;
    common.getStorageValue(that, "HomeworkListClick", [0, 0, 0], function () {
      that.data.List[index].Finished = 1;
      that.data.FinishedCount += 10;

      that.setData({
        FinishedCount: that.data.FinishedCount,
        List: that.data.List,
      });

      that.data.HomeworkListClick[index]=1;
      wx.setStorageSync("HomeworkListClick", that.data.HomeworkListClick);
    });
  },
  goto: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
  },
  onShareAppMessage: function () {
    return {
      title: "作业清单",
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID +'&type=homework',
      imageUrl: app.globalData.uploadImageUrl +"web/program_screenshot_checklist.png",
    }
  },
})
