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,
        itemdata: [],

    },

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


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

    },


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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        Apps.islogin();
        console.log("==================");
        console.log(wx.getStorageSync('jmparm'))
        this.getpaper(wx.getStorageSync('jmparm')); //班级列表 1全部，2我的班级
        this.setData({
            active: wx.getStorageSync('jmparm') == '' ? 1 : wx.getStorageSync('jmparm'),
            typeid: wx.getStorageSync('jmparm') == '' ? 1 : wx.getStorageSync('jmparm')
        })
        //头部消息
        this.msglist();
    },

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

    },
    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 () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    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',
        })
    },
    communityinfoclick: function (e) {

        wx.navigateTo({
            url: '/pages/communityinfo/communityinfo?id=' + e.currentTarget.id,
        })
    },
    communityinfocommentclick: function (e) {

        wx.navigateTo({
            //communityinfocomment
            url: '/pages/communityinfo/communityinfo?id=' + e.currentTarget.id + '&jztype=2',
        })
    },
    messagelistclick: function (e) {
        //资讯列表分类
        wx.navigateTo({
            url: '/pages/messagelist/messagelist?typeid=' + e.currentTarget.id,
        })
    },
    messageclick: function (e) {
        wx.navigateTo({
            url: '/pages/messageencyclopedias/messageencyclopedias?id=' + e.currentTarget.id,
        })

        // wx.navigateTo({
        //   url: '/pages/indexclass/indexclass?id=' + e.currentTarget.id,
        // })
    },
    msglist: function () {

        const that = this;
        let url = Apps.globalData.baseUrl + '/msglist/msgindexlist';
        var param = {
            id: '123456789'
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            that.setData({
                messagelist: res.data.sort()

            });

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

    // 触底加载
    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();
        }

    },
    getpaper: function (params) {
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexclass/indexclasslist';
        var param = {
            typepid: params,
            user_id: wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
          console.log("----------");
            console.log(res.data);
            // wx.setStorageSync('jmparm',2);
            that.setData({
                itemdata: res.data
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    examingbtnclick: function (params) {
        console.log(params.currentTarget.id);
        console.log("---------------------2")
        console.log(wx.getStorageSync('jmparm'));
        if(wx.getStorageSync('jmparm')==2){

          wx.setStorageSync('jmparm',1);

              wx.navigateTo({
                                url: '/pages/indexclass/indexclass?id=' +params.currentTarget.id,
                              })
        }else{
          wx.navigateTo({
            url: '/pages/indexclassintro/indexclassintro?id='+params.currentTarget.id,
          })

        }
    
       
        
    },
    challengeclickleft(e) {
        console.log(e.currentTarget.id);
        wx.setStorageSync('jmparm', e.currentTarget.id)
        this.setData({
            challengeactive: e.currentTarget.id
        })
        this.getpaper(e.currentTarget.id);
    }
})