// pages/signi/signi.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    year: '',
    month: '',
    day: '',
    weekArr: ['日', '一', '二', '三', '四', '五', '六'],
    dateArr: [],
    firstDay: '',
    lastDay: '',
    param: null,
    clockNum: 3,
    todayMonth: '', // 今月
    todayYear: '', // 今年
    todayDate: '', // 今天
    sign_status: '', // 签到状态
    sign_total: '', // 累计签到天数 
    continue_days: '', // 连续签到天数 
    cumulative: '', // 累计奖励
    sign_log: [], // 签到天数
    sign_name: '', // 签到名字
    sharedata: {},
    // 日期的初始数据
    // year: '',
    // month: '',
    m: '',
    w: '',
    src: '',
    signlog: '',
    isshow: '', // 判断是否签到
    obj: [], // 存值
    tapnow:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getData()
    this.getSign();
    // 日历
    // 初始化年份和月数
    this.getData(1);
    this.getnewDate();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  signnow(e) {
    var that = this;
    if(!that.data.tapnow){
      console.log('点击限制！！！');
      return;
    }
    if (this.data.sign_status) {
      wx.showToast({
        title: '今日已经' + this.data.sign_name,
        icon: 'none'
      });
    } else {
      that.setData({
        tapnow : false
      });
      let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign.sign');
      app._getNetWork({
        url: urlStr,
        success: function (res) {
          if (res.data.result == 1) {
            wx.showLoading({
              title: '加载中',
              mask: true
            });
            that.getSign();
            that.getnewDate();
          } else if (res.data.result == 0) {
            wx.showToast({
              icon: 'none',
              title: res.data.msg,
              duration: 1500
            });

            setTimeout(function() {
              that.setData({
                tapnow : true
              });
            }, 1500); //延迟时间
          }
        }
      });
    }
  },
  initShare() {
    let that = this;
    let urlStr = app.getNetAddresss('member.member.wxJsSdkConfig');
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: _url,
      success: function (resdata) {
        var res = resdata;
        if (res.result == 1) {
          that.share(response.data);
        }
      },
      fail: function (res) {
        console.log(res.msg);
      }
    });
  },
  getSign() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
    app._getNetWork({
      url: urlStr,
      success: function (res) {
        if (res.data.result == 1) {
          wx.hideLoading();
          wx.setStorage({
            key: 'sign_log',
            data: res.data.data.sign_log
          });
          that.setData({
            sign_status: res.data.data.sign_status,
            sign_total: res.data.data.sign_total,
            continue_days: res.data.data.continue_days,
            cumulative: res.data.data.cumulative,
            sign_log: res.data.data.sign_log,
            sign_name: res.data.data.sign_name
          });
          wx.setStorage({
            key: 'cumulative',
            data: res.data.data.cumulative
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      }
    });
  },
  // 获取时间
  // getData() {
  //   let that = this;
  //   let urlStr = app.getNetAddresss('plugin.sign.Frontend.Controllers.share.index');
  //   app._getNetWork({
  //     url: urlStr,
  //     success: function (res) {
  //       if (res.result == 1) {
  //         that.sharedata = res.data.share;
  //         that.initShare();
  //       }
  //     },
  //     fail: function (res) {
  //       console.log(res.msg);
  //     }
  //   });
  // },
  // 跳转到签到记录
  gosignInfo() {
    wx.navigateTo({
      url: '/packageA/member/signrecord/signrecord'
    });
  },
  // 右侧标题被点击时
  onClickRight() {
    wx.navigateTo({
      url: '/packageA/member/signrules/signrules'
    });
  },
  // 日历
  getnewDate() {
    var that = this;
    let d = new Date(that.data.year, that.data.month, 0);
    that.setData({
      m: d.getDate()
    });
    // 吧天数放进数组对象中
    let m = d.getDate();
    let obj = [];
    for (let i = 1; i <= m; i++) {
      obj.push({
        day: i
      });
    }
    this.setData({
      obj});
    let data = {
      month: that.data.month
    };
    that.getsigninfo(data);

    // that.data.m = d.getDate()
    let wx = new Date(that.data.year, that.data.month - 1, 1);
    let w1 = wx.getDay();
    // that.data.w = w1 * 14.27
    that.setData({
      w: w1 * 14.27
    });
  },
  put() {
    var that = this;
    var month = that.data.month;
    if (month <= 1) {
      that.setData({
        year: that.data.year - 1,
        month: 12
      });
    } else {
      var month1 = that.data.month;
      that.setData({
        month: month1 - 1
      });
    }
    // that.getData(1)
    // ==============================================
    let data = {
      month: that.data.month
    };
    that.getsigninfo(data);
    that.getnewDate();
  },
  getsigninfo(data) {
    // 请求签到日期的数组
    var that = this;
    let urlStr = app.getNetAddresss('plugin.sign.Frontend.Modules.Sign.Controllers.sign-log.index');
    app._postNetWork({
      url: urlStr,
      showToastIn: false,
      data: data,
      success: function (res) {
        that.setData({
          signlog: res.data.data.sign_log
        });
        let obj = that.data.obj;
        
        var nowyear = new Date().getFullYear();
        let signArr = res.data.data.sign_log;
        console.log(obj, signArr,'998855');
        for (let j = 0; j < obj.length; j++) {
          for (let o = 0; o < signArr.length; o++) {
            if ((obj[j].day-1) == signArr[o] && that.data.year == nowyear) {
              obj[j].bool = true;
            }
          }
        }
        that.setData({
          obj});
      }
    });
  },
  add() {
    var that = this;
    let month = that.data.month;
    if (that.data.month >= 12) {
      let yea = that.data.year;
      that.setData({
        year: yea + 1,
        month: 1
      });
    } else {
      var mon = month;
      that.setData({
        month: mon + 1
      });
    }
    that.getnewDate();
  },
  getData(num) {
    var that = this;
    let date = new Date();
    let year = date.getFullYear();
    that.setData({
      year});
    let month = date.getMonth() + num;
    that.setData({
      month});
    let d = new Date(year, month, 0);
    that.setData({
      m: d.getDate()
    });
    let wx = new Date(year, month - 1, 1); // 获取当月第一天是星期几
    let w1 = wx.getDay();
    that.setData({
      w: w1 * 14.27
    });
  }
});
