// pages/shopinfo/shopinfo.js
import storeApi from '../../api/store'
import productApi from '../../api/product'
import commentsApi from '../../api/comments'
Page({

    /**
     * 页面的初始数据
     */
    data: {

        shopInfo: {},
        commentsList: [],
        productList: [],
        shopId: 0,
        pageNo: 1,
        pageSum: '',
        loadingEnd: false,
        commentsNum: 0

    },

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

        this.setData({
            shopId: options.id
        })
        await this.getShopInfo();
        await this.getCommentsList()

    },
    getCommentsList: async function () {
        var page = this.data.pageNo;
        var shopId = this.data.shopId;
        var comments = await commentsApi.getCommentsByStore(page, 2, shopId);
        //对返回的评论列表进行处理
        var commList = this.standardCommentsList(comments.list);
        var commentsList = this.data.commentsList;
        var list = commentsList.concat(commList)
        this.setData({
            commentsList: list,
            pageSum: comments.pageCount,
            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 pageNo = self.data.pageNo;
        pageNo += 1;

        self.setData({
            pageNo
        })
        if (self.data.pageNo <= self.data.pageSum) {
            self.getCommentsList()
            return
        }
        self.setData({
            loadingEnd: true
        })
    },

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

    },
    getShopInfo: async function () {
        const self = this;
        var shopId = self.data.shopId;
        const productList = await productApi.getProductList(1, 100);
        const shopInfo = await storeApi.getStoreById(shopId);
        console.log(productList)
        self.setData({
            productList,
            shopInfo: shopInfo.info
        })
    },
    showProduct: function (e) {
        console.log(e.currentTarget.dataset.productid)
        var productId = e.currentTarget.dataset.productid;
        var shopId = this.data.shopInfo.id;
        // console.log('shop='+shopId)
        wx.navigateTo({
            url: '../product/product?shopId=' + shopId + '&productId=' + productId,
        })
    }
})