const app = getApp();
Page({
  data: {
    Session_list: [],
    now: ""
  },

  onLoad: function (options) {
    const {
      unreadCountMes,
    } = app.globalData;
    const now = Date.parse(new Date());
    this.setData({
      unreadCountMes,
      now,
    });
    getApp().watch('unreadCountMes', this.watchBack);
    app.editTabbar();
  },
  watchBack(name, value) {
    let data = {};
    data[name] = value;
    const now = Date.parse(new Date());
    this.setData({
      ...data,
      Session_list: app.globalData.Session_list,
      now
    });
  },
  onShow: function () {
    const token = wx.getStorageSync('token');
    if (token) {
      const {
        Session_list
      } = app.globalData;
      console.log(Session_list);
      this.setData({
        Session_list
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: "none"
      })
      setTimeout(() => {
        wx.switchTab({
          url: '/page/tabBar/mine/index',
        })
      }, 1000)
    }
  },
  drawEnd: function (e) {
    var item = this.data.Session_list[e.currentTarget.dataset.index]
    if (item.left === 0) {
      item.left = -140
      this.setData({
        Session_list: this.data.Session_list,
      })
    }
  },
  delTap(e) {
    const that = this;
    var {
      index,
      conversationid: conversationID
    } = e.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确定删除该聊天？',
      success: function (sm) {
        if (sm.confirm) {
          app.deleteChat(conversationID, index, that);
        }
      }
    })
  },
  chat(e) {
    const {
      userid: userID,
      showname,
      faceurl,
      index:num
    } = e.currentTarget.dataset;
    var item = this.data.Session_list[num]
    if (item.left === 0) {
      app.globalData.userID = userID;
      wx.navigateTo({
        url: `/page/message/pages/Chat/chat?userID=${userID}&showname=${showname}&faceurl=${faceurl}`,
      })
    } 
    for (var index in this.data.Session_list) {
      var item = this.data.Session_list[index]
      item.left = 0
    }
    this.setData({
      Session_list: this.data.Session_list,
    })
  },
})