<template>
  <div>
    <div class="search">
      <el-input
        v-model="keyword"
        size="mini"
        placeholder="请输入用户昵称"
        clearable
      />
      <el-button type="primary" size="mini" @click="searchMsg">搜索</el-button>
    </div>
    <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, index) in list" :key="index">
          <div class="new-header">
            <img :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 v-if="item.readCount">{{ item.readCount }}</i
                      ><i v-if="item.totalCount">{{ item.totalCount }}</i></span
                    >
                    <span>已读</span>
                  </div>
                </div>
              </div>
              <p>{{ item.createTime }}</p>
            </div>
            <div class="news-opt">
              <div class="flex">
                <el-link type="success" @click="setReplyMsg(item)"
                  >引用</el-link
                >
                <el-link type="success" @click="updateMessageStatus(item)">{{
                  item.status !== 2 ? "公开" : "取消公开"
                }}</el-link>
              </div>
              <div class="flex">
                <el-link type="success" @click="toPrivateChat(item)"
                  >私聊</el-link
                >

                <el-link type="success" @click="prohibition(item)">{{
                  item.isForbidden === 1 ? "取消禁言" : "禁言"
                }}</el-link>
              </div>
            </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>
    <forbidSpeak
      ref="forbidSpeakRef"
      :product-type="41"
      :product-id="groupId"
      :scope="2"
      @callback="forbidSpeakCallback"
    />
  </div>
</template>
<script>
import { removeCommentBlack } from "@/api/videoLive";
import { getMessageList, updateMessageStatus } from "@/api/circle.js";
import forbidSpeak from "../../components/forbidSpeak.vue";
export default {
  components: { forbidSpeak },
  props: {
    groupId: {
      type: Number
    },
    newMsg: {
      type: Object
    },
    modifyStateObj: {
      // 修改了状态的对象
      type: Object
    }
  },
  data() {
    return {
      keyword: "",
      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")
      },
      msgIdsObj: {}, // 以消息id为key
      msgUserIdsObj: {} // 以发送消息的userId为key
    };
  },
  computed: {
    disabled() {
      return this.loading || !this.hasNext;
    }
  },
  watch: {
    groupId() {
      this.keyword = "";
      this.list = [];
      this.msgIdsObj = {};
      this.getMessageList();
    },
    newMsg(msg) {
      if (msg.userType === 2 && !this.msgIdsObj[msg.id]) {
        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];
        }
      } else if (msg.userType === 2 && this.msgIdsObj[msg.id]) {
        Object.assign(this.msgIdsObj[msg.id], msg);
      }
    },
    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: 3 // 查询类型:1全部;2投顾;3用户;4精选
      }).catch(() => {
        this.loading = false;
      });
      if (ret && ret.code === 0) {
        const retList = ret.data.list;
        this.list = this.list.concat(retList);
        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() {
      this.msgIdsObj = {}; // 以消息id为key
      this.msgUserIdsObj = {}; // 以发送消息的userId为key
      this.list = [];
      this.getMessageList();
    },
    setReplyMsg(item) {
      this.$emit("setReplyMsg", item);
    },
    toPrivateChat(item) {
      this.$emit("toPrivateChat", item);
    },
    prohibition(item) {
      if (item.isForbidden === 1) {
        this.$confirm(`您确定取消禁言?`, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            removeCommentBlack({
              userPhone: item.userId,
              productId: this.groupId,
              productType: 41
            }).then(res => {
              if (res.code === 0) {
                this.$message({
                  message: "取消禁言成功",
                  showClose: false,
                  type: "success"
                });
                this.msgUserIdsObj[item.userId].forEach(item => {
                  item.isForbidden = 2;
                });
                this.modifyStateCallback({
                  userId: item.userId,
                  isForbidden: 2,
                  modifyType: 2
                });
              }
            });
          })
          .catch(() => {});
      } else {
        const forbidSpeakRef = this.$refs.forbidSpeakRef;
        forbidSpeakRef.dialogFormVisible = true;
        forbidSpeakRef.formProhibition.userName = item.userName;
        forbidSpeakRef.formProhibition.phone = item.userId;
        forbidSpeakRef.formProhibition.content = item.content;
      }
    },
    forbidSpeakCallback(userId) {
      this.msgUserIdsObj[userId].forEach(item => {
        item.isForbidden = 1;
      });
      this.modifyStateCallback({
        userId: userId,
        isForbidden: 1,
        modifyType: 2
      });
    },
    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.modifyStateCallback({
        //   msgId: item.id,
        //   status: item.status,
        //   modifyType: 1
        // });
      }
    },
    modifyStateCallback(item) {
      this.$emit("modifyStateCallback", item);
    }
  }
};
</script>
<style lang="scss" scoped>
.search {
  display: flex;
  margin-bottom: 10px;
  .el-input {
    margin-right: 10px;
  }
}
.news-list-wrap {
  height: 562px;
  overflow-y: scroll;
  background: #fff;
}
.news-list {
  background: #fff;
  list-style: none;
  padding: 10px;
  margin: 0;
  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: 0 10px;
        }
      }
      p {
        margin: 0;
        color: #999;
      }
    }
    .news-opt {
      display: flex;
      .flex {
        display: flex;
        flex-direction: column;
        margin-left: 5px;
        align-items: center;
        justify-content: space-between;
      }
    }
  }
  .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;
}
</style>
