<template>
  <div class="news-list-wrap">
    <ul
      v-infinite-scroll="getMessageList"
      class="news-list"
      :infinite-scroll-disabled="disabled"
      infinite-scroll-immediate="false"
    >
      <li v-for="item in list" :key="item.id">
        <div class="new-header">
          <img
            v-if="item.userType === 1"
            :src="item.advisor ? item.advisor.avatar : defaultAvatar.teacher"
          />
          <img v-else-if="item.userType === 3" :src="defaultAvatar.assistant" />
          <img v-else-if="item.userType === 2" :src="defaultAvatar.student" />
          <div class="news-info">
            <div class="news-info-top">
              <div class="news-user">
                <h5>{{ item.userName }}</h5>
                <label
                  v-if="[1, 3].includes(item.userType)"
                  :class="[item.userType === 1 ? 'blue' : 'orange']"
                  >{{ item.userType === 1 ? "老师" : "助教" }}
                </label>
                <div v-if="item.userType !== 2">
                  <span>
                    <i>{{ item.readCount ? item.readCount : 0 }} </i
                    ><i>{{ item.totalCount ? `/${item.totalCount}` : "人" }} </i
                    >已读
                  </span>
                </div>
              </div>
              <div class="news-opt">
                <el-link
                  v-if="type !== 5"
                  type="success"
                  @click="setMessageRecommend(item)"
                  >{{
                    item.isRecommend === 1 ? "取消精选" : "设为精选"
                  }}</el-link
                >
                <el-link
                  v-if="item.userType === 2 && type !== 5"
                  type="success"
                  @click="setReplyMsg(item)"
                  >引用</el-link
                >
                <el-link
                  v-if="type !== 5 || (type === 5 && item.userType !== 2)"
                  type="success"
                  @click="updateMessageStatus(item)"
                  >{{ item.status !== 2 ? "公开" : "取消公开" }}</el-link
                >
              </div>
            </div>
            <p>{{ item.createTime }}</p>
          </div>
        </div>
        <div class="new-content">
          <p v-if="item.contentType === 1">{{ item.content }}</p>
          <img v-else :src="item.content" alt="" />
        </div>
      </li>
    </ul>
    <p v-if="loading" class="load-tip">加载中...</p>
    <p v-if="!hasNext" class="load-tip">没有更多了...</p>
    <p v-else-if="!loading && hasNext" class="load-tip">下拉加载更多...</p>
  </div>
</template>
<script>
import { getMessageList } from "@/api/circle.js";
import { setMessageRecommend, updateMessageStatus } from "@/api/circle";
export default {
  props: {
    groupId: {
      type: Number
    },
    type: {
      // 查询类型:1全部;2投顾;3用户;4精选;5私聊
      type: Number
    },
    newMsg: {
      // 新的群消息
      type: Object
    },
    userId: {
      // 私聊用户id
      type: Number | String
    },
    privateNewMsg: {
      // 新的私聊消息
      type: Object
    },
    modifyStateObj: {
      // 修改了状态的对象
      type: Object
    }
  },
  data() {
    return {
      keyword: "",
      msgIdsObj: {},
      msgUserIdsObj: {}, // 以发送消息的userId为key
      list: [],
      loading: true,
      hasNext: true,
      defaultAvatar: {
        teacher: require("@/assets/images/defaultAvatar/teacher.png"),
        student: require("@/assets/images/defaultAvatar/student.png"),
        assistant: require("@/assets/images/defaultAvatar/assistant.png")
      }
    };
  },
  computed: {
    disabled() {
      return this.loading || !this.hasNext;
    }
  },
  watch: {
    groupId() {
      this.searchMsg("");
    },
    newMsg(msg) {
      if (this.msgIdsObj[msg.id]) {
        Object.assign(this.msgIdsObj[msg.id], msg);
      } else if (
        this.type === 1 ||
        (this.type === 2 && msg.userType !== 2) ||
        (this.type === 4 && msg.isRecommend === 1)
      ) {
        // this.type === 4 && msg.isRecommend === 1 精选
        // this.type === 2 && msg.userType === 1 用户互动
        // this.type === 1 全部
        this.list.unshift(msg);
        this.msgIdsObj[msg.id] = msg;
        if (this.msgUserIdsObj[msg.userId]) {
          this.msgUserIdsObj[msg.userId].push(msg);
        } else {
          this.msgUserIdsObj[msg.userId] = [msg];
        }
      }
    },
    privateNewMsg(msg) {
      if (
        msg.userType !== 2 ||
        (msg.userType === 2 && msg.privateUserId === this.userId)
      ) {
        this.list.unshift(msg);
      }
    },
    userId(value) {
      if (value) {
        this.searchMsg("");
      }
    },
    modifyStateObj(value) {
      // modifyType: 1审核通过 2禁言操作
      if (value.modifyType === 1 && this.msgIdsObj[value.msgId]) {
        this.msgIdsObj[value.msgId].status = value.status;
      } else if (value.modifyType === 2 && this.msgUserIdsObj[value.userId]) {
        this.msgUserIdsObj[value.userId].forEach(item => {
          item.isForbidden = value.isForbidden;
        });
      }
    }
  },
  methods: {
    async getMessageList() {
      this.loading = true;
      const ret = await getMessageList({
        groupId: this.groupId,
        keyword: this.keyword,
        lastId:
          this.list && this.list.length
            ? this.list[this.list.length - 1].id
            : "",
        size: 10,
        type: this.type,
        userId: this.userId
      }).catch(() => {
        this.loading = false;
      });
      if (ret && ret.code === 0) {
        const retList = ret.data.list;
        this.list = this.list.concat(ret.data.list);
        retList.forEach(msg => {
          this.msgIdsObj[msg.id] = msg;
          if (this.msgUserIdsObj[msg.userId]) {
            this.msgUserIdsObj[msg.userId].push(msg);
          } else {
            this.msgUserIdsObj[msg.userId] = [msg];
          }
        });
        this.hasNext = ret.data.hasNext;
      }
      this.loading = false;
    },
    searchMsg(keyword) {
      if (this.groupId) {
        this.keyword = keyword;
        this.list = [];
        this.msgIdsObj = {};
        this.msgUserIdsObj = {};
        this.getMessageList();
      }
    },
    setReplyMsg(item) {
      this.$emit("setReplyMsg", item);
    },
    async setMessageRecommend(item) {
      const ret = await setMessageRecommend({
        isRecommend: item.isRecommend === 1 ? 2 : 1,
        messageId: item.id
      });
      if (ret && ret.code === 0) {
        this.$message({
          message: "设置成功",
          type: "success"
        });
        item.isRecommend = item.isRecommend === 1 ? 2 : 1;
      }
    },
    async updateMessageStatus(item) {
      const ret = await updateMessageStatus({
        id: item.id,
        status: item.status === 1 ? 2 : 1
      });
      if (ret && ret.code === 0) {
        this.$message({
          message: "操作成功",
          showClose: false,
          type: "success"
        });
        // item.status = item.status === 1 ? 2 : 1;
        // this.$emit("modifyStateCallback", {
        //   msgId: item.id,
        //   status: item.status,
        //   modifyType: 1
        // });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.news-list-wrap {
  height: 600px;
  overflow-y: scroll;
  background: #fff;
}
.news-list {
  list-style: none;
  padding: 10px;
  margin: 0;
  flex: 1;
  li {
    margin: 0;
    border-bottom: 1px dashed #999;
    padding-top: 20px;
  }
  .new-header {
    display: flex;
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
    }
    .news-info {
      font-size: 14px;
      margin-left: 8px;
      flex: 1;
      .news-info-top {
        display: flex;
        justify-content: space-between;
      }
      .news-user {
        display: flex;
        align-items: center;
        color: #666;
        margin-bottom: 8px;
        h5 {
          margin: 0;
          font-size: 14px;
          color: #666;
        }
        i {
          font-style: normal;
        }
        label {
          font-size: 14px;
        }
        span {
          margin-left: 20px;
          color: #03a9f4;
          i:last-child {
            margin-right: 4px;
          }
        }
      }
      p {
        margin: 0;
        color: #999;
      }
    }
  }
  .new-content {
    p {
      font-size: 16px;
      line-height: 24px;
      margin: 0;
      padding: 20px 0;
    }
    img {
      max-width: 100px;
      max-height: 100px;
      margin: 20px 0;
    }
  }
}
.load-tip {
  text-align: center;
  line-height: 50px;
}
.news-opt {
  .el-link {
    margin-left: 5px;
  }
}
</style>
