// packageI/GoodsComment/GoodsComment.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    last_page: 1,
    micro_communities: [],
    commentPage: 1,
    comment_last_page: 1,
    noMoreComment: false,
    third_content: [],
    istype: "comment"

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.gid) {
      this.setData({
        goodsId: options.gid
      });
    }
    if (options.istype) {
      this.setData({
        istype: options.istype
      });
    }
    if (this.data.istype == 'comment') {
      wx.setNavigationBarTitle({
        title: '全部评价',
      });
      this.getCommentData();
    } else {
      wx.setNavigationBarTitle({
        title: '更多微贴',
      });
      this.getMic();
    }
  },
  // streetClose() {
  //   wx.navigateBack({
  //     delta: 1,
  //   })
  // },
  goMicro(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageC/micro_communities/microCommentDetails/microCommentDetails?stickId=' + id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.istype == 'comment') {
      this.getMoreComment();
    } else {
      this.getMoreMic();
    }
  },

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

  },
  getMoreMic() {
    if (this.data.page >= this.data.last_page) {
      console.log('没有更多');
      return;
    }
    wx.showLoading({
      title: '加载中',
    });
    this.setData({
      page: this.data.page + 1
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.goods.index");
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        goods_id: this.data.goods_info.get_goods.id,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['micro_communities.list']: that.data.micro_communities.list.concat(res.data.data),
            last_page: res.data.last_page
          });
        }
        wx.hideLoading();
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMic() {
    // 单个商品微社区帖子
    this.setData({
      page: 1
    });
    let that = this;
    let urlStr = app.getNetAddresss("plugin.micro-communities.api.goods.index");
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page,
        goods_id: this.data.goodsId,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            ['micro_communities.list']: res.data.data,
            last_page: res.data.last_page
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getMoreComment() {
    if (this.data.commentPage >= this.data.comment_last_page) {
      console.log('没有更多了');
      return;
    }
    let that = this;
    let urlStr = app.getNetAddresss("goods.comment.get-comment");
    urlStr += "&goods_id=" + this.data.goodsId;
    urlStr += "&page=" + this.data.commentPage;
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            commentPage: that.data.commentPage + 1,
            comment_last_page: res.data.last_page
          });
          that.setData({
            third_content: [...that.data.third_content, ...res.data.data],
            streetShow: true,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getCommentData() {
    // 单个商品评论
    let that = this;
    let urlStr = app.getNetAddresss("goods.comment.get-comment");
    urlStr += "&goods_id=" + this.data.goodsId;
    urlStr += "&page=" + this.data.commentPage;
    app._getNetWork({
      showToastIn: false,
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          if (res.data.data.length < 20) {
            that.setData({
              noMoreComment: true,
            });
          }
          that.setData({
            commentPage: that.data.commentPage + 1,
            comment_last_page: res.data.last_page
          });
          console.log(that.data.comment_last_page, 'aaaaaa');
          if (!that.data.third_content) {
            that.setData({
              third_content: [],
            });
          }
          that.setData({
            third_content: [...that.data.third_content, ...res.data.data],
            streetShow: true,
          });
          if (app._isTextEmpty(that.data.third_content)) {
            // console.log("空没评论");
            that.setData({
              thirKong: false,
            });
          } else {
            // console.log("不空");
            that.setData({
              thirKong: true,
            });
          }
        } else {
          that.setData({
            is_third_content: false,
          });
        }
      },
      fail: function (res) {
        that.setData({
          is_third_content: false,
        });
        console.log(res);
      },
    });
  },

  // 预览图片
  previewImage(evt){
    let {images,src} = evt.currentTarget.dataset;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: images // 需要预览的图片http链接列表
    });
  },

  toContentInfo (e) {
    let data = e.currentTarget.dataset.jsondata;
    let page_url = '/packageD/member/CommentDetails/CommentDetails';
    page_url = `${page_url}?order_id=${data.order_id}&goods_id=${data.goods_id}&comment_id=${data.id}&order_goods_id=${data.order_goods_id}&uid=${data.uid}`;
    wx.navigateTo({
      url: page_url,
    });
  }
});