const util = require('../../utils/util.js');
const config = require('../../utils/config.js')
const app = getApp();
Page({
  data: {
    userInfo: app.globalData.userInfo,
    list: [],
    childs: [],
    lists: [],
    finished: false,
    pageSize: 10,
    pageIndex: 1,
    finishedText: '',
    img: config.imgPath,
  },
  onShow: function () {
    this.setData({ userInfo: app.globalData.userInfo, finished: false, pageIndex: 1, finishedText: '' });
    this.getChilds();
    this.listInfo();
  },
  getChilds() {
    let that = this;
    if (!app.globalData.userInfo) return false;
    util.ajax({ func: "v2/user/grow/childs", load: false }, function (res) {
      if (res.code == 0) {
        res.data.forEach((item, index) => {
          res.data[index].avatar = item.avatar ? item.avatar : '/images/default_logo.jpg';
        });
        that.setData({ childs: res.data });
      } else
        util.showTips(res.reason)
    })
  },
  listInfo() {
    let that = this;
    util.ajax({ func: 'v2/comments/grow/list', load: false }, function (res) {
      if (res.code == 0) {
        let lists = res.data;
        lists.forEach((item, index) => {
          lists[index].tags = item.content.match(/#([^#]+)#/g);
          lists[index].content = item.content.replace(/#([^#]+)#/g, "").replace(/<br\/>/g, '\n');
          lists[index].ftags = item.fcontent ? item.fcontent.match(/#([^#]+)#/g) : [];
          lists[index].fcontent = item.fcontent ? item.fcontent.replace(/#([^#]+)#/g, "").replace(/<br\/>/g, '\n') : '';
        })
        that.setData({ lists })
        that.getList(1);
      } else
        util.showTips(res.reason)
    })
  },
  setLike(e) {
    let info = e.currentTarget.dataset.info, index = e.currentTarget.dataset.index;
    if (info.likeStatus == 1) return false;
    let that = this;
    if (app.globalData.userInfo) {
      util.ajax({
        func: "v2/user/comments/like",
        data: { cid: info.cid },
        method: "POST",
        load: true
      }, function (res) {
        if (res.code == 0) {
          let list = that.data.list;
          list[index].likeStatus = 1;
          list[index].likeCnt = list[index].likeCnt + 1;
          if (list[index].commentsLikes.length >= 6) list[index].commentsLikes.pop();
          list[index].commentsLikes.unshift({ avatar: that.data.userInfo.avatar });
          that.setData({ list })
        } else {
          util.showTips(res.reason)
        }
      })
    } else {
      // 静默登录
      util.silentLogin().then(resr => {
        util.ajax({
          func: "v2/user/comments/like",
          data: { cid: info.cid },
          method: "POST",
          load: true
        }, function (res) {
          if (res.code == 0) {
            let list = that.data.list;
            list[index].likeStatus = 1;
            list[index].likeCnt = list[index].likeCnt + 1;
            if (list[index].commentsLikes.length >= 6) list[index].commentsLikes.pop();
            list[index].commentsLikes.unshift({ avatar: that.data.userInfo.avatar });
            that.setData({ list })
          } else {
            util.showTips(res.reason)
          }
        })
      })
    }
  },
  getList(type) {
    wx.hideLoading();
    let data = this.data.lists.slice(this.data.pageSize * (this.data.pageIndex - 1), this.data.pageSize * this.data.pageIndex);
    let list = type ? data : [...this.data.list, ...data];
    this.setData({ list });
    if (this.data.list.length >= this.data.lists.length || this.data.list.length < this.data.pageSize) {
      this.setData({ finishedText: this.data.list.length < this.data.pageSize ? '' : "~~已经到底了~~", finished: true })
    } else {

    }
  },
  clickImg(arr, index) {
    ImagePreview({ images: arr, startPosition: index, lazyLoad: true, closeOnPopstate: true, showIndex: false, showIndicators: true });
  },
  childsBinderror(e) {
    var childs = this.data.childs, index = e.currentTarget.dataset.index;
    childs[index].avatar = '/images/default_logo.jpg'
    this.setData({ childs });
  },
  binderror(e) {
    var list = this.data.list, index = e.currentTarget.dataset.index;
    list[index].ownerAvatar = '/images/default_logo.jpg'
    this.setData({ list });
  },
  imgListBinderror(e) {
    var list = this.data.list, index = e.currentTarget.dataset.index, i = e.currentTarget.dataset.i;
    list[index].imageList[i] = '/images/noimg.png';
    this.setData({ list });
  },
  activeLogoBinderror(e) {
    var list = this.data.list, index = e.currentTarget.dataset.index;
    list[index].article.logo = '/images/noimg.png'
    this.setData({ list });
  },
  userBinderror(e) {
    var list = this.data.list, index = e.currentTarget.dataset.index, i = e.currentTarget.dataset.i;
    list[index].commentsLikes[i].avatar = '/images/default_logo.jpg'
    this.setData({ list });
  },
  previewImage(e) {
    let index = e.currentTarget.dataset.index, i = e.target.dataset.i, list = this.data.list;
    this.setData({ onshow: false });
    wx.previewImage({
      current: list[index].imageList[i], // 当前显示图片的http链接
      urls: list[index].imageList // 需要预览的图片http链接列表
    })
  },
  onReachBottom() {
    if (this.data.finished) return false
    wx.showLoading();
    this.setData({ pageIndex: this.data.pageIndex + 1 });
    this.getList();
  },
  onShareAppMessage() {
    return {
      title: '这里，藏着孩子的未来',
      path: '/pages/home/growup'
    }
  }
})