// pages/productinfo/productinfo.js
import productApi from '../../api/product'
import commentsApi from '../../api/comments'
Page({

    /**
     * 页面的初始数据
     */
    data: {
     
        shopId: 0,
        productId: 0,
        product: {},
        commentsList:[],
        commentsNum:0

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        wx.showLoading({
            title: '加载中',
        })
        var shopId = options.shopId;
        var productId = options.productId;
        var product = await productApi.getProductById(productId);
        var comments = await commentsApi.getCommentsByProduct(1,10,shopId, productId);
        //对返回的评论列表进行处理
        var list = this.standardCommentsList(comments.list);
        console.log(list)
        this.setData({
            shopId,
            productId,
            product: product.info,
            commentsList: list,
            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 () {

    },

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

    },
    gotoAllComments: function (e) {
        var shopId = this.data.shopId;
        var productId = this.data.productId;
        wx.navigateTo({
          url: '../comments/comments?title=全部评论&commentstype=all&shopId='+shopId+'&productId='+productId,
        })
    },
    gotoAppointment:function (e) {
        var shopId = this.data.shopId;
        var productId = this.data.productId;
        wx.navigateTo({
          url: '../appointment/appointment?shopId='+shopId+'&productId='+productId,
        })
    }
})