<template>
  <view class="safe-area-inset-bottom p-2">
    <view
      class="p-32 white b-r-8 flex-col"
      v-for="(item, i) in list"
      :key="item.id"
    >
      <view class="flex-row flex-ali-center flex-jus-sp">
        <view class="flex-row flex-ali-center flex-jus-sp">
          <view style="position: relative">
            <u-image
              width="80rpx"
              height="80rpx"
              shape="circle"
			  :src="item.user.avatar || '../../static/image/djr.png'"
            >
              <u-loading slot="loading"></u-loading>
            </u-image>
            <!-- <u-badge :offset="[0,-8]" :count="2"></u-badge> -->
          </view>
          <view class="m-l-2 flex-col flex-jus-sp">
            <view class="flex-row flex-ali-center">
              <d-text
                :text="item.user.name || '用户'"
                className="fs-32 tips-c"
              ></d-text>
              <d-text :text="timeago(item.last.created_at*1000)" color="#969799" className="m-l-2"></d-text>
            </view>
            <d-text :text="getMsg(item.last.content).data.text" className="fs-32 main-c"></d-text>
          </view>
        </view>
        <view>
          <u-button
            size="mini"
            shape="circle"
            :ripple="true"
            type="primary"
            @click="sessionIn(item.id)"
            >接入会话
          </u-button>
        </view>
      </view>
    </view>
    <view class="flex-row flex-jus-center" v-if="!loading && list.length == 0">
      <d-empty text="暂无待接入用户" mode="search"></d-empty>
    </view>
    <d-loading-page :loading="loading"></d-loading-page>
  </view>
</template>

<script>
import { sessionIn,waitInList } from "@/api/all.js";

export default {
  data() {
    return {
      loading: true,
      list: [],
      info: "",
    };
  },
  onShow() {
    this.waitInList()
  },
  methods: {
    getMsg(val) {
      return this.$u.test.jsonString(val)
        ? JSON.parse(val)
        : {
            type: "text",
            data: {
              text: val,
            },
          };
    },
    timeago(dateTimeStamp) {
      let result = "";
      var minute = 1000 * 60; //把分，时，天，周，半个月，一个月用毫秒表示
      var hour = minute * 60;
      var day = hour * 24;
      var week = day * 7;
      var halfamonth = day * 15;
      var month = day * 30;
      var now = new Date().getTime(); //获取当前时间毫秒
      console.log(now);
      var diffValue = now - dateTimeStamp; //时间差

      if (diffValue < 0) {
        return;
      }
      var minC = diffValue / minute; //计算时间差的分，时，天，周，月
      var hourC = diffValue / hour;
      var dayC = diffValue / day;
      var weekC = diffValue / week;
      var monthC = diffValue / month;
      if (monthC >= 1 && monthC <= 3) {
        result = " " + parseInt(monthC) + "月前";
      } else if (weekC >= 1 && weekC <= 3) {
        result = " " + parseInt(weekC) + "周前";
      } else if (dayC >= 1 && dayC <= 6) {
        result = " " + parseInt(dayC) + "天前";
      } else if (hourC >= 1 && hourC <= 23) {
        result = " " + parseInt(hourC) + "小时前";
      } else if (minC >= 1 && minC <= 59) {
        result = " " + parseInt(minC) + "分钟前";
      } else if (diffValue >= 0 && diffValue <= minute) {
        result = "刚刚";
      } else {
        var datetime = new Date();
        datetime.setTime(dateTimeStamp);
        var Nyear = datetime.getFullYear();
        var Nmonth =
          datetime.getMonth() + 1 < 10
            ? "0" + (datetime.getMonth() + 1)
            : datetime.getMonth() + 1;
        var Ndate =
          datetime.getDate() < 10
            ? "0" + datetime.getDate()
            : datetime.getDate();
        var Nhour =
          datetime.getHours() < 10
            ? "0" + datetime.getHours()
            : datetime.getHours();
        var Nminute =
          datetime.getMinutes() < 10
            ? "0" + datetime.getMinutes()
            : datetime.getMinutes();
        var Nsecond =
          datetime.getSeconds() < 10
            ? "0" + datetime.getSeconds()
            : datetime.getSeconds();
        result = Nyear + "-" + Nmonth + "-" + Ndate;
      }
      return result;
    },
    async waitInList() {
      this.loading=true
      let res = {};
      res = await waitInList();
      this.loading=false
      if (res.errcode == 0) {
        this.list = res.data;
      } else {
        this.$toast(res.msg);
      }
    },
    sessionIn(id) {
      console.log(id,'0090')
      uni.showLoading({
        title: "接入中...",
      });
      sessionIn({
        ims_id: id,
      }).then((res) => {
        if (res.errcode == 0) {
          this.info = res.data;
          this.waitInList()
          uni.hideLoading();
        } else {
          this.$toast(res.msg);
        }
      });
    },
  },
};
</script>
<style>
page {
  background-color: #f9fafb;
}
</style>
<style lang="scss" scoped></style>
