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

    /**
     * 页面的初始数据
     */
    data: {
        baseurl: baseUrl.globalData.baseimgurl,
        messagelist: [],
        startPageX: 0,
        currentView: DEFAULT_PAGE,
        imagecount: 4,
        indeximagecount: 4,
        active: 1,
        challengeactive: 1,
        toView: `card_${DEFAULT_PAGE}`,
        list: [],
        replylist: [],
        imageslists: [] //图片预览
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '我的怼贴',
        })
        baseUrl.islogin();
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //头部消息
        this.msglist();
        //留言类型
        this.wrodtypelist();
        //回复留言列表
        this.getwords(1);
    },

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

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    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) {
        console.log(e.currentTarget.id);
        this.setData({
            active: e.currentTarget.id
        })
        this.getwords(e.currentTarget.id);
    },
    duizhiticlick: function () {

        wx.navigateTo({
            url: '/pages/allpapers/allpapers',
        })
    },
    messagelistclick: function () {
        wx.navigateTo({
            url: '/pages/messagelist/messagelist',
        })
    },
    communityinfoclick: function (e) {
        //社区留言信息
        console.log(e.currentTarget.id);
        wx.navigateTo({
            url: '/pages/communityinfo/communityinfo?id=' + e.currentTarget.id,
        })
    },
    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,
        })
    },
    msglist: function () {

        const that = this;
        let url = baseUrl.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);
        });
    },
    wrodtypelist: function () {

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

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    getwords: function (ee) {

        const that = this;
        let url = baseUrl.globalData.baseUrl + '/wordslist/getwordsbyuserid';
        var param = {
            typeid: ee,
            user_id: wx.getStorageSync('wxuserid') == null ? 3 : wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            that.setData({
                replylist: res.data
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    wordsclick: function (ee) {
        console.log(ee);
        console.log(baseUrl.globalData.currentuserinfo);
        let e1 = ee.currentTarget.id;
        let e2 = wx.getStorageSync('wxuserid');
        const that = this;
        let url = baseUrl.globalData.baseUrl + '/wordslist/wordsclickcount';
        var param = {
            words_id: e1,
            user_id: e2,
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            that.setData({
                replylist: res.data
            });
            //头部消息
            that.msglist();
            //留言类型
            that.wrodtypelist();

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    preview2: function (params) {
        console.log(params);
        let that = this;
        let currentUrl = params.currentTarget.dataset.src;    
        let arrindex = params.currentTarget.dataset.id;
        let indexpic = params.currentTarget.dataset.cid;
        console.log(indexpic);
        console.log(arrindex);
        let rearr = that.data.replylist[arrindex].pics;
        console.log(rearr);
        let arr = [];
        rearr.forEach(function (item, index) {
            arr[index] = that.data.baseurl + item
        });
        wx.previewImage({
            current: arr[indexpic], // 当前显示图片的http链接
            urls: arr // 需要预览的图片http链接列表
        })
    }
})