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



  /**
   * 页面的初始数据
   */
  data: {
    tree: {},
    treeId: null,
    opinion: [],
    sendInfo: '',
    userMessage: ''
  },

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

  //----------------------------获取文章详情---------------------------
  tree() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/tree/get',
      method: 'GET',
      data: {
        id: that.data.treeId,     //树洞id
      },
      success: (res) => {
        // console.log(res.data);
        if (res.data.success) {
          that.setData({
            tree: res.data.msg,
          })
        }else{
          that.errShowToast(res.data.msg);
        }
      },
      fail: (res) => {},
    })
  },
  //----------------------------获取评论列表---------------------------
  opinion() {
    var that = this;
    wx.request({
      url: app.globalData.myUrl + '/opinion/list',
      method: 'GET',
      data: {
        treeId: that.data.treeId, //树洞id
      },
      success: (res) => {
        // console.log(res.data);
        if (res.data.success) {
          that.setData({
            opinion: res.data.msg,
          })
        }else{
          that.errShowToast(res.data.msg);
        }
      },
      fail: (res) => {},
    })
  },

    //-------------------------删除指定文章----------------------------
    deleteTree(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 + '/tree/delete',
        method: 'GET',
        data: {
          id: that.data.treeId, //文章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) => {},
      })
    },

  //-----------------------------前往登录--------------------------------
  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;
    }
  },
  
  //-----------------------------发表评论--------------------------------
  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 + '/opinion/deliver',
      method: 'GET',
      data: {
        content:that.data.userMessage,  //评论内容
        treeId: that.data.treeId, //文章id
        token:app.globalData.token
      },
      success: (res) => {
        // console.log(res);
        if (res.data.success) {
          that.sucShowToast(res.data.msg)
        }
        that.opinion();
      },
      fail: (res) => {},
    })
    this.setData({
      userMessage:''
    })
  },

  //---------------------------评论删除-----------------------------
  deleteOpinion(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+'/opinion/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.opinion();
        }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({
      treeId: options.id,
      hasLogin:app.globalData.hasLogin,
      // treeId: 33
    });
    this.tree();
    this.opinion();
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})