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

  /**
   * 页面的初始数据
   */
  data: {
    userid:'',
    content:'',
    chatList:[]
  },

  deliver(){
    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('你还没有登录喔');
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl+'/chat/deliver',
      data: {
        token: app.globalData.token,
        content:"",
        fromUserId:that.data.userid
      },
      success: (res) => {
        // console.log(res);
        if (!res.data.success) {
          that.errShowToast(res.data.msg)
        }
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  }, 

  /**
   * 获取输入框信息
   */
  confirmPassword(e) {
    // console.log(e.detail.value);
    this.setData({
      content:e.detail.value
    })
  },

  /**
   * 发送信息
   */
  deliver(){
    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('你还没有登录喔');
          }
        }
      })
      return;
    }
    if (that.data.userid == "" || that.data.content == "") {
      that.errShowToast("请输入评论内容");
      return;
    }
    wx.request({
      url: app.globalData.myUrl+'/chat/deliver',
      data: {
        token: app.globalData.token,
        content:that.data.content,
        toUserId:that.data.userid
      },
      success: (res) => {
        that.setData({
          content:""
        })
        that.load();
        if (!res.data.success) {
          that.errShowToast(res.data.msg)
        }
        // console.log(res);
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },

   /**
    * 加载聊天信息
    */
  load(){
    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('你还没有登录喔');
          }
        }
      })
      return;
    }
    wx.request({
      url: app.globalData.myUrl+'/chat/get',
      data: {
        token: app.globalData.token,
        fromUserId:that.data.userid
      },
      success: (res) => {
        // console.log(res.data.msg);
        if (res.data.success) {
          that.setData({
            chatList:res.data.msg
          })
        }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) {
    // console.log(options);
    this.setData({
      userid:options.userid
    })
    this.load();
  },

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

  },

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

  },

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

  },

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

  },

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

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

  },

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

  }
})