const util = require("../../../utils/util")
const app = getApp()

Page({
  data: {
    articalId:0,
    isLike:0,
    info:{},
    confirmModal: false,
    publishModal: false,
    content:'',
    categorys:{},
    userInfo:{},
    isLeader:0,
    logs:[]
  },
  preview( e ){
    var imgs = [];
    let brirf = this.data.info.brief||''
    brirf.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, function (match, capture) {
      imgs.push(capture);
    });
    if( imgs.length > 0 ){
      wx.previewImage({
        current: imgs[0],
        urls: imgs
      })
    }
  },
  openDialog( e ){
    let param = {content:''}
    param[e.currentTarget.dataset.id] =true
    this.setData( param )
  },
  closeDialog( e ){
    let param = {content:''}
    param[e.currentTarget.dataset.id] =false
    this.setData( param )
  },
  onLoad: function (options) {
    let articalId = +options.articalId||2135;
    this.setData({articalId})
  },
  onShow: function(){
    app.checkLogin( this.loadData )
  },
  inputContent(e){
    let content = e.detail.value
    this.setData({ content });
  },
  confirmArtical: function (e) {
    let articalId = this.data.info.articalId
    let content = this.data.content;
    let action = 1
    if( !content ){
      util.showMsg("请留下宝贵意见")
      return 
    }
    let info = this.data.info;
    app.formPost( "artical/confirmArtical", {articalId,content,action}).then( res=>{
      if( res.code == 200 ){
        info.status = 2
        app.showMsg( "确认成功")
        this.setData({info, confirmModal:false})
      }
    })
  },
  publishArtical:function (e) {
    let articalId = this.data.info.articalId
    let content = this.data.content;
    let action = 1
    if( !content ){
      util.showMsg("请留下宝贵意见")
      return 
    }
    app.formPost( "artical/publishArtical", {articalId,content,action}).then( res=>{
      if( res.code == 200 ){
        info.status = 3
        app.showMsg( "发布成功")
        this.setData({info, confirmModal:false})
      }
    })
  },
  loadData(){
    let articalId = this.data.articalId
    app.formPost( "weixin/getArticalInfo", {articalId}).then( res=>{
      if( res.code == 200 ){
        let {isLike,info,logs, isLeader} = res.data
        this.setData({isLike,info,logs,isLeader})
      }
    })
  },
  downloadUrl( url ){
    wx.downloadFile({
      url: url,
      timeout: 0,
      success: (res) => {
        var rr=res.tempFilePath;
        wx.openDocument({
          filePath:rr,
          showMenu: true,
          success(res) {
            wx.showToast({
              title: '保存成功',
              icon: 'success',
              duration: 2000
            })
          }
        })
      },
      fail: (res) => {
        console.log("res_fail", res)
      },
      complete: (res) => {
        console.log("res_complete", res)
      },
    })
  },

  download(){
    let articalId = this.data.info.articalId;
    app.formPost( "artical/downloadFile", {articalId}).then( res=>{
      if( res.code == 200 ){
        this.downloadUrl( res.data )
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {title, articalId} = this.data.info;
    return {
      title: title,
      path: `/pages/artical/info/index?articalId=${articalId}`
    }
  }
})