import {
  icommunity
} from '../../../api/api.js'

const app = getApp()

Page({
  data: {
    pagination: {
      page: 1,
      pagesize: 100
    },
    imgList: [],
    imgUrl: app.globalData.imgUrl,
    allowComments: false,
    swiperHeight: 0
  },
  clickPic(e) {
    wx.previewImage({
      current: this.data.article.upload_url_list[e.currentTarget.dataset.i],
      urls: this.data.article.upload_url_list
    })
  },
  clickFavor() {
    icommunity.loadArticleFavorToggle({
      reqdata: {
        id: this.data.article.id
      }
    }, r => {
      this.loadArticledetails()
    })
  },
  clickCollection() {
    icommunity.loadArticleCollectionToggle({
      reqdata: {
        id: this.data.article.id
      }
    }, r => {
      this.loadArticledetails()
    })
  },
  clickCommentconfirm() {
    if (!this.data.comment) {
      return wx.showToast({
        icon: 'loading',
        title: '请输入内容',
      })
    }
    icommunity.loadAddArticlecomment({
      reqdata: {
        id: this.data.article.id,
        content: this.data.comment
      }
    }, r => {
      wx.showToast({
        icon: 'success',
        title: '提交成功',
      })
      this.setData({
        comment: '',
        isFocused: false
      })
      this.loadArticlecomments(true)
    })
  },
  inputComment(e) {
    this.setData({
      comment: e.detail.value,
    })
  },
  blurComment() {
    this.setData({
      isFocused: false
    })
  },
  focusComment() {
    this.setData({
      isFocused: true
    })
  },
  loadArticlecomments(hideLoading) {
    icommunity.loadArticlecomments(this.data.pagination, r => {
      this.setData({
        comments: r.list,
        ['article.num_comment']: r.list.length
      })
    }, hideLoading)
  },
  hideComments() {
    this.setData({
      showComments: false
    })
  },
  clickComments() {
    if (!this.data.showComments) {
      this.loadArticlecomments()
    }
    this.setData({
      showComments: !this.data.showComments
    })
  },
  loadArticledetails() {
    icommunity.loadArticledetails({
      reqdata: {
        id: this.data.article.id
      }
    }, r => {
      console.log(r)
      this.setData({
        imgList:r.object.filePath.split(','),
        article: r.object
      })
    })
  },
  onLoad(args) {
    this.setData({
      allowComments: app.config.open_comment == 1,
      article: app.data.nav.communityArticle,
      ['pagination.reqdata.id']: app.data.nav.communityArticle.id
    })
    this.loadArticledetails()
  },
  // onReady() {
  //   setTimeout(() => {
  //     this.swiperAutoHeight()
  //   }, 300)
  // },
  swiperAutoHeight() {
    let imgs = this.data.article.upload_url_list

    for (let i in imgs) {
      wx.createSelectorQuery()
        .select(`#img${i}`).boundingClientRect()
        .select(`#fake${i}`).boundingClientRect().exec(rect => {
          let h = rect[1].top - rect[0].top
          console.log(h)
          this.setData({
            swiperHeight: h > this.data.swiperHeight ? h : this.data.swiperHeight
          })
        })
    }
  }
})