// pages/comments/comments.js
import commentsApi from '../../api/comments'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        commentsList: [],
        shopId: 0,
        productId: 0,
        commentsType: '',
        pageNo: 1,
        pageSum: '',
        loadingEnd: false,
        
    },

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


        wx.setNavigationBarTitle({
            title: options.title
        })
        if (options.commentstype == 'my') {

            await this.getMyCommentsList();

        }
        if (options.commentstype == 'all') {
            var shopId = options.shopId;
            var productId = options.productId;
            await this.getAllCommentsList(shopId,productId)
        }
    },
    // 获取产品的全部评论
    getAllCommentsList: async function (shopId, productId) {
        var page = this.data.pageNo;
        var comments = await commentsApi.getCommentsByProduct(page, 10, shopId, productId);
        //对返回的评论列表进行处理
        var commList = this.standardCommentsList(comments.list);
        var commentsList = this.data.commentsList;
        var list = commentsList.concat(commList)
        this.setData({
            commentsList: list,
            shopId,
            productId,
            commentsType: 'all',
            pageSum: comments.pageCount,
        })
    },
    //获取我的全部评论
    getMyCommentsList: async function () {
        var page = this.data.pageNo;
        var comments = await commentsApi.getPersonComments(page, 10, 1);
        var commList = this.standardCommentsList(comments.list);
        var commentsList = this.data.commentsList;
        var list = commentsList.concat(commList)
        this.setData({
            commentsList: list,
            commentsType: 'my',
            commentsNum: comments.count
        })
    },
    standardCommentsList: function (list) {
        list.forEach(function (item, i, array) {
            let urls = item.imgUrls;
            let imgs = urls.split(',');
            item.imgs = imgs;
            let time = item.createTime.trim().split(" ")[0];
            item.commentTime = time
        })
        return list;
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var self = this;
        var commentsType = this.data.commentsType;
        var pageNo = this.data.pageNo;
        pageNo += 1;
        this.setData({
            pageNo
        })
        if (commentsType == 'my') {
            if(self.data.pageNo <= self.data.pageSum){
                this.getMyCommentsList()
                return
            }
            self.setData({
                loadingEnd: true
            })
        }
        if(commentsType=='all'){
            var shopId = self.data.shopId;
            var productId = self.data.productId;
            if(self.data.pageNo <= self.data.pageSum){
                this.getAllCommentsList(shopId,productId)
                return
            }
            self.setData({
                loadingEnd: true
            })
        }
    },

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

    }
})