// pages/picture/picture.js
Page({

    // 前往下一页
    picture() {
        wx.navigateTo({ url: '../picture/picture' });
    },
    /**
     * 页面的初始数据
     */
    data: {
        img_url:wx.getStorageSync('img_url'),
        swiperCurrent:0,
        autoplay: false,
        interval: 3000,
        duration: 800,
        //数据
        name:"王凯杰",
        nm_number:"237",
        //列表
        works_list:[],
        user_id:''
    },
    swiperChange: function (e) {
        // console.log(e);
        this.setData({
            swiperCurrent: e.detail.current
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        that.countDown()
        var userId = options.id;
        that.setData({
            user_id: userId,
            img_url:wx.getStorageSync('img_url'),
        });
        that.getUserInfo(userId);
        that.getUserProduction(userId);
    },
    getUserProduction:function(userId){
        var that = this;
        wx.request({
            url: wx.getStorageSync('urlPath').getUserProduction,
            header:{
                'token' : wx.getStorageSync('token')
            },
            data:{
                user_id: userId
            },
            success(res){
                if(res.data.code == 0){
                    if(res.data.data.length > 0){
                        var vote_list = [];
                        var urlPath = wx.getStorageSync('urlPath');
                        res.data.data.forEach(function(item){
                            vote_list.push({
                                ids: item.id,
                                img: urlPath.host + item.pic,
                                text: item.explain,
                                name: item.user_name,
                                number: item.votes,
                                uid:item.uid
                            });
                        });
                        that.setData({
                            works_list:vote_list
                        });
                    }
                }
            }
        })
    },
    getUserInfo:function(userId){
        var that = this;
        wx.request({
            url: wx.getStorageSync('urlPath').getUserById,
            header:{
                'token' : wx.getStorageSync('token')
            },
            data:{
                user_id: userId
            },
            success(res){
                if(res.data.code == 0){
                    console.log(res.data.data);
                    that.setData({
                        name:res.data.data.name,
                        nm_number:res.data.data.allVoteNumber
                    });
                }
            }
        })
    },
// 倒计时
    countDown:function(){
        var that=this;
        var nowTime = new Date().getTime();//现在时间（时间戳）
        var endTime = new Date(that.data.endTime).getTime();//结束时间（时间戳）
        var time = (endTime-nowTime)/1000;//距离结束的毫秒数
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let hou = parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        // console.log(day + "," + hou + "," + min + "," + sec)
        day = that.timeFormin(day),
        hou = that.timeFormin(hou),
        min = that.timeFormin(min),
        sec = that.timeFormin(sec)
        that.setData({
            day: that.timeFormat(day),
            hou: that.timeFormat(hou),
            min: that.timeFormat(min),
            sec: that.timeFormat(sec)
        })
        // 每1000ms刷新一次
        if (time>0){
            that.setData({
                countDown: true
            })
            setTimeout(this.countDown, 1000);
        }else{
            that.setData({
                countDown:false
            })
        }
    },
    //小于10的格式化函数（2变成02）
    timeFormat(param) {
        return param < 10 ? '0' + param : param;
    },
    //小于0的格式化函数（不会出现负数）
    timeFormin(param) {
        return param < 0 ? 0: param;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    vote(e){
        var that = this;
        var id = e.currentTarget.dataset.id;
        if(id > 0){
            wx.request({
                url: wx.getStorageSync('urlPath').vote,
                header:{
                    'token' : wx.getStorageSync('token')
                },
                data:{
                    id : id
                },
                success(res){
                    if(res.data.code == 0){
                        wx.showToast({
                            title: '投票成功'
                        });
                        that.onLoad({id:that.data.user_id});
                    }else{
                        wx.showToast({
                            title:res.data.msg,
                            icon:'none'
                        });
                    }
                }
            })
        }
    },
})