const DEFAULT_PAGE = 0;
const Apps = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseurl: Apps.globalData.baseimgurl,
        messagelist: [],
        startPageX: 0,
        currentView: DEFAULT_PAGE,
        imagecount: 4,
        indeximagecount: 4,
        active: 1,
        challengeactive: 1,
        toView: `card_${DEFAULT_PAGE}`,
        list: [],
        replylist: [],
        imageslists: [], //图片预览
        showview: false,
        upload_picture_list: [],
        imagesListp: [],
        imagestr: [],
        id: 1,
        accesstoken: '',
        typeid: wx.getStorageSync('jmparm') == null ? 1 : wx.getStorageSync('jmparm'),
        page: 1, //页面数,
        pagecount: 0,
        h: '00',
        m: '00',
        s: '00',
        //存储计时器
        setInter: '',
        num: 1,
        resdata:[], //班级信息
        userlistdata:[], //用户数据
        usersingledata:[]

    },
    // 计时器
    queryTime() {
        const that = this;
        var hou = that.data.h;
        var min = that.data.m;
        var sec = that.data.s;
        that.data.setInter = setInterval(function () {
            sec++;
            if (sec >= 60) {
                sec = 0;
                min++;
                if (min >= 60) {
                    min = 0;
                    hou++;
                    that.setData({
                        h: (hou < 10 ? '0' + min : min)
                    });
                } else {
                    that.setData({
                        m: (min < 10 ? '0' + min : min)
                    });
                }
            } else {
                that.setData({
                    s: (sec < 10 ? '0' + sec : sec)
                });
            }
            var numVal = that.data.num + 1;
            that.setData({ num: numVal });
            // console.log('setInterval==' + that.data.num);
        }, 1000)
    },
    taskStart() {
        this.queryTime()
    },
    taskEnd() {
        //清除计时器  即清除setInter
        clearInterval(this.data.setInter)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


        if (options.length > 0) {
            this.setData({
                active: options.id == '' ? 1 : options.id,
                page: 1,
                typeid: wx.getStorageSync('jmparm')
            })
        }

    },
    displayview: function () {
        wx.navigateTo({
            url: '/pages/createwords/createwords?typeid=' + this.data.typeid,
        })

    }, contentclick: function (params) {

        let id = params.currentTarget.id;
        let xid = params.currentTarget.dataset.xid;

        this.setData({
            wordstextname: '@' + id
        });

        this.addinccomment(xid)
        wx.showToast({
            title: '@成功,请移底部留言',//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
        })

        //  this.pageScrollToBottom();

    }, addinccomment: function (params) {
        //增加评论数
        console.log(params);
        let userid = wx.getStorageSync('wxuserid');
        const that = this;
        let url = Apps.globalData.baseUrl + '/wordslist/addcommentrepycount';
        var param = {
            id: params,
            userid: userid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            // this.wrodtypelist(this.data.courseid)

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    getroominfo:function(params) {

        const classid=this.data.active;
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexstudyroom/getroomrennum';
        var param = {
            classid: classid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log("res===================");
            console.log(res);
            that.setData({
                resdata:res.data
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
        
    },  
    addroomuser:function () {
       //添加用户与自习室
       const classid=this.data.active;
       const that = this;
       let url = Apps.globalData.baseUrl + '/indexstudyroom/addroomuser';
       var param = {
           classid: classid,
           user_id: wx.getStorageSync('wxuserid')
       };
       //此处为使用封装的post请求
       util.get(url, param).then((res) => {
           console.log("res===================");
           console.log(res);

       }).catch((errMsg) => {
           console.log(errMsg);
       });

    },
    updateroomusertime:function (parms) {
        //更新停留时间
        const classid=this.data.active;
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexstudyroom/updateroomusertime';
        var param = {
            classid: classid,
            user_id: wx.getStorageSync('wxuserid'),
            times:parms
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log("ssssssssssssssss===================");
            console.log(res);
 
        }).catch((errMsg) => {
            console.log(errMsg);
        });
 
     },
     getroomuser:function(params) {
        
        //得到房间的用户
        //更新停留时间
        const classid=this.data.active;
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexstudyroom/getroomuser';
        var param = {
            classid: classid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log("aaaaaaaaaaaaaaa==================");
            console.log(res);
            that.setData({
                userlistdata:res.data
            })
 
        }).catch((errMsg) => {
            console.log(errMsg);
        });
     },


     getroomsingleuser:function(params) {
        
        //得到房间的用户
        //更新停留时间
        const classid=this.data.active;
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexstudyroom/getroomsingleuser';
        var param = {
            classid: classid
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log("aaaaaaaaaaaaaaa==================");
            console.log(res);
            that.setData({
                usersingledata:res.data
            })
 
        }).catch((errMsg) => {
            console.log(errMsg);
        });
     },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.taskStart();
        Apps.islogin();
        this.setData({
            active: wx.getStorageSync('jmparm') == '' ? 1 : wx.getStorageSync('jmparm'),
            typeid: wx.getStorageSync('jmparm') == '' ? 1 : wx.getStorageSync('jmparm')
        })
        // //头部消息
        // this.msglist();
        // //留言类型
        // this.wrodtypelist();
        // //回复留言列表  
        // this.getwords();
        this.getroominfo(); //得到自习室的信息
        this.addroomuser(); //添加自习室
        this.getroomuser();// 得到用户数
        this.getroomsingleuser(); //得到最后一个用户信息
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
            console.log("11111111111111111");
            this.taskEnd();
    },
    getacctoken: function () {
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/acctoken';
        var param = {
            pareid: '1213'
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {

            wx.setStorageSync('accesstoen', res.access_token)
            that.setData({
                accesstoken: res.access_token
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log("22222222");
        console.log(this.data.num);
        this.updateroomusertime(this.data.num);
        //清除计时器  即清除setInter
        clearInterval(this.data.setInter);
    },

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

    },

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

    },

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

    },
    touchStart(e) {
        this.startPageX = e.changedTouches[0].pageX;
    },
    touchEnd(e) {

        const moveX = e.changedTouches[0].pageX - this.startPageX;
        const maxPage = this.data.list.length - 1;
        if (Math.abs(moveX) >= 150) {
            if (moveX > 0) {
                this.currentView = this.currentView !== 0 ? this.currentView - 1 : 0;
            } else {
                this.currentView = this.currentView !== maxPage ? this.currentView + 1 : maxPage;
            }
        }
        this.setData({
            toView: `card_${this.currentView}`
        });
    },
    touchecard(e) {

        wx.setStorageSync('jmparm', e.currentTarget.id);
        this.setData({
            active: e.currentTarget.id,
            typeid: e.currentTarget.id
        })
        this.getwords();
    },
    duizhiticlick: function () {
        wx.navigateTo({
            url: '/pages/allpapers/allpapers',
        })
    },
    messagelistclick: function () {
        wx.navigateTo({
            url: '/pages/messagelist/messagelist',
        })
    },

    // 触底加载
    onReachBottom: function () {

        this.setData({
            page: this.data.page + 1
        })
        let newpage = (this.data.page + 1); //页数触底加1
        console.log(newpage);
        if (newpage * 10 >= this.data.pagecount) {

            wx.showToast({
                title: '触底了,没有可加载的数据了',
                icon: 'none',
                duration: 1500
            })

        } else {
            this.getwords();
        }

    }
})