const util = require("../../../utils/util.js");
Page({
  data: {
    datas: [],
    queryData:{
      pageIndex:1,
      pageSize: 20,
      sort: 1,
      keyword: '',
      aid: ""
    },
    score:0,
    loadMore:true
  },
  onLoad(options) {
    this.data.queryData.aid = options.aid;
    this.data.queryData.sort = options.sort || 1;
    this.data.queryData.keyword = options.keyword || '';
    this.setData({ queryData: this.data.queryData });
    this.getFeedbook();
  },
  getFeedbook(load) {//获取评论
    let that = this;
    util.ajax({
      func: "comments/list",
      data: that.data.queryData,
      load: load
    }, function (res) {
      if (res.code == 0) {
        let loadMore = true;
        let newData = res.data.comments;
        let oldData = !load ? newData : [...that.data.datas.comments, ...newData];
        if (newData.length < that.data.queryData.pageSize && oldData.length > that.data.queryData.pageSize) {
          loadMore = false;
        }
        for (let i in oldData) {
          oldData[i].contented = oldData[i].content.replace(/#([^#]+)#/g, "").replace(/<br\/>/g,'\n');
          oldData[i].fcomments.fcontent = oldData[i].fcomments.fcontent.replace(/<br\/>/g,'\n')
          oldData[i].option = oldData[i].content.match(/#([^#]+)#/g);
        }
        that.setData({ datas: { tagList: res.data.tagList, comments: oldData }, loadMore, score: res.data.score });
      } else {
        util.showTips(res.reason);
      }
    })
  },
  setTag(e){
    let data = e.currentTarget.dataset;
    this.data.queryData.sort = data.sort;
    this.data.queryData.keyword = data.keyword;
    this.data.queryData.pageIndex = 1;
    this.setData({ queryData: this.data.queryData, loadMore: false});
    this.getFeedbook();
  },
  previewImage(e) {
    let index = e.currentTarget.dataset.index, i = e.target.dataset.i, comments = this.data.datas.comments;
    wx.previewImage({
      current: comments[index].imageList[i], // 当前显示图片的http链接
      urls: comments[index].imageList // 需要预览的图片http链接列表
    })
  },
  previewFimages(e) {
    let index = e.currentTarget.dataset.index, i = e.target.dataset.i, comments = this.data.datas.comments;
    wx.previewImage({
      current: comments[index].fcomments.fimages[i], // 当前显示图片的http链接
      urls: comments[index].fcomments.fimages // 需要预览的图片http链接列表
    })
  },
  onPullDownRefresh: function () {
    var self = this;
    wx.showNavigationBarLoading();
    setTimeout(function () {
      self.data.queryData.pageIndex = 1;
      self.setData({ queryData: self.data.queryData });
      self.getFeedbook(false);
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },
  onReachBottom: function () {
    var self = this;
    if (self.data.loadMore == false) return false;

    setTimeout(function () {
      self.data.queryData.pageIndex = self.data.queryData.pageIndex + 1;
      self.setData({ queryData: self.data.queryData });
      self.getFeedbook(true);
    }, 1000);
  },
})