//index.js
//获取应用实例
var app = getApp();
var calendarSignData;
var date;
var calendarSignDay;
Page({
    data: {
        // hasbeen: [],
        param:[],
        count:'',
        qian:'',
    },
    //事件处理函数
    calendarSign: function () {
        calendarSignData[date] = date;
        console.log(date);
        console.log(calendarSignData);
        calendarSignDay = calendarSignDay + 1;
        console.log(calendarSignDay);
        wx.setStorageSync("calendarSignData", calendarSignData);
        wx.setStorageSync("calendarSignDay", calendarSignDay);

        wx.showToast({
            title: '签到成功',
            icon: 'success',
            duration: 2000
        })
        this.setData({
            calendarSignData: calendarSignData,
            count: ++this.data.count
        })
        //签到
        var that = this;
        // var user_Id = app.globalData.userId;
        wx.request({
            url: 'https://jisu.shenmikj.com/painter/api/index/sign_in',
            data:{
                user_id: wx.getStorageSync('openid')
            },
            success:function(res){
                console.log(res);
                that.setData({
                    calendarSignDay: calendarSignDay
                })
            }
        })
    },
    onLoad: function () {
        var that = this;
        var user_Id = app.globalData.userId;
        wx.request({
            url: 'https://jisu.shenmikj.com/painter/api/index/select_in',
            data: {
                user_id: wx.getStorageSync('openid'),
            },
            success: function (res) {
                console.log(res);
                that.setData({
                    // hasbeen: res.data.data,
                    count:res.data.count
                })
                if (status == 0) {
                    that.setData({
                        count: 0
                    })
                    return false;
                }
                for (var i = 0; i < res.data.data.length; i++) {
                    var param = {};
                    var string1 = "calendarSignData[" + res.data.data[i].create_time + "]";
                    param[string1] = res.data.data[i].create_time;
                    that.setData(param)
                }
            }
        })

        var mydate = new Date();
        var year = mydate.getFullYear();
        var month = mydate.getMonth() + 1;
        date = mydate.getDate();
        console.log("date" + date)
        var day = mydate.getDay();
        console.log(day)
        var nbsp = 7 - ((date - day) % 7);
        console.log("nbsp" + nbsp);
        var monthDaySize;
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            monthDaySize = 31;
        } else if (month == 4 || month == 6 || month == 9 || month == 11) {
            monthDaySize = 30;
        } else if (month == 2) {
            // 计算是否是闰年,如果是二月份则是29天
            if ((year - 2000) % 4 == 0) {
                monthDaySize = 29;
            } else {
                monthDaySize = 28;
            }
        };
        // 判断是否签到过
        if (wx.getStorageSync("calendarSignData") == null || wx.getStorageSync("calendarSignData") == '') {
            wx.setStorageSync("calendarSignData", new Array(monthDaySize));
        };
        if (wx.getStorageSync("calendarSignDay") == null || wx.getStorageSync("calendarSignDay") == '') {
            wx.setStorageSync("calendarSignDay", 0);
        }
        calendarSignData = wx.getStorageSync("calendarSignData")
        calendarSignDay = wx.getStorageSync("calendarSignDay")
        console.log(calendarSignData,'calendarSignData');
        this.setData({
            year: year,
            month: month,
            nbsp: nbsp,
            monthDaySize: monthDaySize,
            date: date,
            calendarSignData: calendarSignData,
            calendarSignDay: calendarSignDay
        })
        //签到得积分
        wx.request({
            url: 'https://jisu.shenmikj.com/painter/api/index/switchs',
            success: function (res) {
                console.log(res);
                that.setData({
                    qian: res.data.data.acc_qiandao
                })
            }
        })
    },
    // onShow:function(){
    //     var that = this;
    //     var user_Id = app.globalData.userId;
    //     wx.request({
    //         url: 'https://jisu.shenmikj.com/painter/api/index/select_in',
    //         data:{
    //             user_id: wx.getStorageSync('openid')
    //         },
    //         success: function (res) {
    //             console.log(res);
    //             that.setData({
    //                 hasbeen: res.data.data,
    //                 count: res.data.count
    //             })
    //         }
    //     })
    // }
})

