// pages/data/data.js
//获取应用实例
const app = getApp()
Page({



  /**
   * 页面的初始数据
   */
  data: {
    article: {},
    articleId: null,
    comment: [],
    sendInfo: '',
    userMessage: '',
    showDialog: false,
    showDialogtwo: false,
    imgs:'',
    names:'',
    noSignIn:'您还未登录,请前往登录',
    commentOne:'我来抢楼',
    fuId:''
  },
  // 点击回复评论
  clickComment(e){
    // console.log(e);
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/comment/deliver',
      method:'GET',

      data:{
        fuId:e.currentTarget.dataset.fuid
      }
    })
  },


  // 文章详细里面的图片放大功能
  bigImage(e){
    wx.previewImage({
      current: 'e.currentTarget.dataset.imgsrc', // 当前显示图片的http链接
      urls: [e.currentTarget.dataset.imgsrc] // 需要预览的图片http链接列表
    })
  },


  /**
   * 发私信
   */
  fasixin(e){
    // console.log(e.currentTarget.dataset.userid);
    let userid = e.currentTarget.dataset.userid;
    wx.navigateTo({
      url: '/pages/letter/letter?userid='+userid,
    })
  },

  // 单击头像弹窗
  /**
  * 控制 pop 的打开关闭
  * 该方法作用有2:
  * 1：点击弹窗以外的位置可消失弹窗
  * 2：用到弹出或者关闭弹窗的业务逻辑时都可调用
  */
 toggleDialog() {
  this.setData({
    showDialog: !this.data.showDialog
  });
},
toggleDialogtwo(e) {
  // console.log(e);
  this.setData({
    showDialogtwo: !this.data.showDialogtwo,
    imgs:e.currentTarget.dataset.imgs,
    names:e.currentTarget.dataset.names,
    userid:e.currentTarget.dataset.userid
  });
},

  //----------------------------获取文章详情---------------------------
  article() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/Article/get',
      method: 'GET',
      data: {
        id: that.data.articleId, //文章id
      },
      success: (res) => {
        // console.log(res.data);
        that.setData({
          article: res.data,
        })
      },
      fail: (res) => {},
    })
  },

    //-----------------------------前往登录--------------------------------
    goToLogin(){
      if (!app.globalData.hasLogin) {
        wx.showModal({
          title: '提示',
          content: '是否前往登录',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后
              wx.switchTab({
                url: '/pages/own/own',
              })
            } else { //这里是点击了取消以后
              that.errShowToast('你还没有登录喔');
            }
          }
        })
        return;
      }
    },
    
  //----------------------------获取评论列表---------------------------
  comment() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/comment/list',
      method: 'GET',
      data: {
        articleId: that.data.articleId, //文章id
      },
      success: (res) => {
        // console.log(res.data);
        that.setData({
          comment: res.data.msg,
        })
      },
      fail: (res) => {},
    })
  },

    //-------------------------删除指定文章----------------------------
    deleteArticle(e) {
      var that = this;
      // console.log(e.currentTarget.id);
      if (!app.globalData.hasLogin) {
        wx.showModal({
					title: '提示',
					content: '你还没有登录喔',
					success: function (res) {
						if (res.confirm) { //这里是点击了确定以后
							wx.switchTab({
								url: '/pages/own/own',
							})
						} else { //这里是点击了取消以后
							that.errShowToast('你还没有登录喔');
						}
					}
				})
        return;
      }
      wx.request({
        url: app.globalData.myUrl + '/Article/delete',
        method: 'GET',
        data: {
          id: that.data.articleId, //文章id
          token: app.globalData.token, //用户令牌
        },
        success: (res) => {
          if (res.data.success) {
            that.sucShowToast(res.data.msg);
            wx.navigateBack({
              delta: 1
            })
          } else {
            that.errShowToast(res.data.msg);
          }
        },
        fail: (res) => {},
      })
    },
  
  //-----------------------------发表评论--------------------------------
  bindMessage: function (e) {
    var msg = e.detail.value.replace(/(\s*$)/g, "");
    this.setData({
      userMessage: msg
    })
  },

  cleanInput: function () {
    // console.log(this.data.userMessage);
    var that = this;
    if (!app.globalData.hasLogin) {
      wx.showModal({
        title: '提示',
        content: '你还没有登录喔',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            wx.switchTab({
              url: '/pages/own/own',
            })
          } else { //这里是点击了取消以后
            that.errShowToast('你还没有登录喔');
            that.setData({
              userMessage:''
            })
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl + '/comment/deliver',
      method: 'GET',
      data: {
        content:that.data.userMessage,  //评论内容
        articleId: that.data.articleId, //文章id
        token:app.globalData.token
      },
      success: (res) => {
        // console.log(res);
        if (res.data.success) {
          that.sucShowToast(res.data.msg)
        }
        that.comment();
      },
      fail: (res) => {},
    })
    this.setData({
      userMessage:''
    })
  },

  //---------------------------评论删除-----------------------------
  deleteComment(e){
    var that = this;
    // console.log(e.currentTarget.id);
    if (!app.globalData.hasLogin) {
      wx.showModal({
        title: '提示',
        content: '你还没有登录喔',
        success: function (res) {
          if (res.confirm) { //这里是点击了确定以后
            wx.switchTab({
              url: '/pages/own/own',
            })
          } else { //这里是点击了取消以后
            that.errShowToast('你还没有登录喔');
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl+'/comment/delete',
      data: {
        id: e.currentTarget.id,
        token: app.globalData.token
      },
      method: 'GET',
      success: (res) => {
        // console.log(res);
        if (res.data.success) {
          that.sucShowToast(res.data.msg);
          that.comment();
        }else{
          that.errShowToast(res.data.msg);
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },

  //---------------------------函数封装------------------------------
  errShowToast: function (toastText) {
    wx.showToast({
      title: toastText,
      image: '/images/password.png',
      duration: 2000
    });
  },
  sucShowToast: function (toastText) {
    wx.showToast({
      title: toastText,
      duration: 2000
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      articleId: options.id,
      hasLogin:app.globalData.hasLogin,
      // articleId: 33
    });
    this.article();
    this.comment();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})