<template>
  <view class="forum">
    <view class="bg">
      <image
        class="bgImg"
        src="https://vv-1317974657.cos.ap-shanghai.myqcloud.com/util/HomeBackground.png"
        mode="scaleToFill"
      />
    </view>
    <view class="title">消息</view>
  </view>
  <view class="content">
    <scroll-view show-scrollbar="false" scroll-y="true" class="scroll">

      <view class="card" v-for="item in Conversationobj.conversations" :key="item.userId">
        <uni-swipe-action>
        <uni-swipe-action-item>
          <view class="cardContent" @click="goChat(item.userId,item.data.nickname,item.data.avatar)">
            <image
                class="cardImg"
                :src="item.data.avatar"
                mode="scaleToFill"
            />
            <view class="cardUnread" v-if="item.unread>0">{{ item.unread > 99 ? '99+':item.unread }}</view>
            <view class="cardnameandtimeandNews">
                <view class="cardnameandtime">
                    <view class="cardname">{{ item.data.nickname }}</view>
                    <view class="cardtime">{{TimeFormatting(item.lastMessage.timestamp)}}</view>
                </view>
                <view class="cardNews">
                    {{ item.lastMessage.payload.text }}
                </view>
            </view>

          </view>
          <template v-slot:right>
            <view class="rightPosition">
                <view class="topPosition">置顶</view>
                <view class="Delete">删除</view>
            </view>
          </template>
        </uni-swipe-action-item>
    </uni-swipe-action>
      </view>

    </scroll-view>
  </view>
  <!-- tanber -->
  <view class="tabBar">
    <tabBar :tabIndex="3"></tabBar>
  </view>
</template>

<script>
import tabBar from "../../components/tabBar/tabBar";
import TimeFormatting from "../../components/TimeFormatting.js";
import {
  getConversationList,
} from "../../components/goEasyTool/tool.js";
import GoEasy from 'goeasy'
export default {
  data() {
    return {
      Conversationobj: {},
    };
  },
  onLoad() {
    //获取会话列表
    getConversationList(this.$goeasy).then((res) => {
      console.log(res);
      this.Conversationobj = res;
    });
    //监听会话列表变化
    var im = this.$goeasy.im;
    im.on(GoEasy.IM_EVENT.CONVERSATIONS_UPDATED, this.onConversationsUpdated);
  },
  methods: {
    //监听会话列表变化
    onConversationsUpdated(conversations){
      this.Conversationobj = conversations;
    },
    TimeFormatting:TimeFormatting,
    //跳转聊天页面
    goChat(userId,nickname,avatar) {
      wx.navigateTo({
        url: `/pages/index/chat/chat?userId=${userId}&nickname=${nickname}&avatar=${avatar}`,
      });
    },
  },
  components: {
    tabBar,
  },
};
</script>

<style scoped>
.bg {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: -1;
}
.bgImg {
  width: 100%;
  height: 100%;
}
.title {
  position: absolute;
  top: 120rpx;
  left: 335rpx;
  font-size: 34rpx;
  color: #100e0f;
  font-weight: bold;
}
.content {
  position: absolute;
  top: 200rpx;
  left: 0rpx;
  right: 0rpx;
  bottom: 100rpx;
}
.scroll {
  width: 90%;
  height: 100%;
  padding: 0% 5% 0% 5%;
}
.card {
  width: 100%;
  height: 150rpx;
  background-color: #fff;
  border-radius: 20rpx;
  margin-top: 20rpx;
}
.cardContent{
    height: 150rpx;
    width: 100%;
    display: flex;
    align-items: center;
}
.cardImg{
    height: 110rpx;
    width: 110rpx;
    border-radius: 20rpx;
    margin-left: 20rpx;
}
.cardUnread{
  width: 30rpx;
  height: 30rpx;
  border-radius: 15rpx;
  font-size: 18rpx;
  color: #ffffff;
  background-color: #f53123;
  text-align: center;
  line-height: 30rpx;
  margin-top: -100rpx;
  margin-left: -15rpx;
}
.cardnameandtimeandNews{
    width: 500rpx;
    height: 110rpx;
    margin-left: 20rpx;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.cardnameandtime{
    width: 100%;
    display: flex;
    justify-content: space-between;
}
.cardNews{
    width: 500rpx;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 10rpx;
    font-size: 28rpx;
    color: #333;
}
.cardname{
    font-size: 30rpx;
    color: #100e0f;
    font-weight: bold;
}
.cardtime{
    font-size: 24rpx;
    color: #999;
}
.rightPosition{
    height: 150rpx;
    width: 300rpx;
    display: flex;
}
.topPosition{
    height: 150rpx;
    width: 150rpx;
    background-color: #f5a623;
    text-align: center;
    line-height: 150rpx;
}
.Delete{
    height: 150rpx;
    width: 150rpx;
    background-color: #f54323;
    text-align: center;
    line-height: 150rpx;
    border-top-right-radius: 20rpx;
    border-bottom-right-radius: 20rpx;
}
</style>
