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

const app = getApp()

Page({
  data: {
    showInfo: true,
    pagination: {
      page: 1,
      pagesize: 100
    },
    imgUrl: app.globalData.imgUrl,
    allowComments: false
  },
  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() {
    console.log(1234)
    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,
      commentFocus: 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)
  },
  clickComments() {
    if (!this.data.showComments) {
      this.loadArticlecomments()
    }
    this.setData({
      showComments: !this.data.showComments
    })
  },
  clickCommemntInput() {
    this.setData({
      commentFocus: true
    })
  },
  clickVideo() {
    this.setData({
      showInfo: !this.data.showInfo,
      showComments: false
    })
  },
  loadArticledetails() {
    icommunity.loadArticledetails({
      reqdata: {
        id: this.data.article.id
      }
    }, r => {
      this.setData({
        article: r.info
      })
    })
  },
  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()
  }
})