import {
  icommunity
} from '../../../api/api.js';
const app = getApp()
Page({
  data: {
    article: {
      fileType: 1,
      imgs: []
    },
    imgUrl: app.globalData.imgUrl,
  },
  clickUpload() {
    if (!this.data.article.articleTitle) {
      return wx.showToast({
        title: '请输入标题'
      })
    }
    if (this.data.article.imgs.length <= 0) {
      return wx.showToast({
        title: '请添加图片'
      })
    }
    let {article} = this.data
    article.filePath = article.imgs.map(i=>i.url).join(',')
    delete article.imgs
    icommunity.loadPubArticle({reqdata:article}, r => {
      wx.showToast({
        mask: true,
        title: '提交成功',
        success() {
          setTimeout(wx.navigateBack, 1800)
        }
      })
    })
  },
  inputTitle(e) {
    this.setData({
      ['article.articleTitle']: e.detail.value
    })
  },
  inputContent(e) {
    this.setData({
      ['article.articleContent']: e.detail.value
    })
  },
  clickImg(e) {
    this.data.article.imgs.splice(e.currentTarget.dataset.i, 1)
    this.setData({
      ['article.imgs']: this.data.article.imgs
    })
  },
  clickAddpic() {
    wx.chooseImage({
      count: 1,
      success: r => {
        icommunity.uploadPics(r.tempFilePaths[0], r => {
          wx.showToast({
            title: '上传成功！'
          })
          this.data.article.imgs.push(r)
          this.setData({
            ['article.imgs']: this.data.article.imgs
          })
        })
      },
    })
  },
  onLoad(args) {}
})