<template>
  <!-- v-if="isConnect" -->
  <div class="chat-message">
    <!-- 会话列表界面 -->
    <div class="chat-header">
      <i class="el-icon-close" @click="$listeners.close()"></i>

      <div class="header-card">
        <el-tooltip
          effect="dark"
          content="会话"
          placement="bottom"
          :open-delay="500"
        >
          <i
            class="icon icon-chat"
            :class="active === 'conversation' && 'active'"
            @click="active = 'conversation'"
          ></i>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          content="联系人"
          placement="bottom"
          :open-delay="500"
        >
          <i
            class="icon icon-message"
            :class="active === 'contacts' && 'active'"
            @click="
              () => {
                firstLoad = true;
                active = 'contacts';
              }
            "
          ></i>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          content="班级"
          placement="bottom"
          :open-delay="500"
        >
          <i
            class="icon icon-class"
            :class="active === 'class' && 'active'"
            @click="
              () => {
                firstLoad = true;
                active = 'class';
              }
            "
          ></i>
        </el-tooltip>
      </div>
    </div>
    <el-container style="margin-top: 56px">
      <el-aside
        width="280px"
        style="border-right: 1px solid #f7f7f7;position: relative;"
      >
        <conversation-list
          ref="conversationList"
          v-show="active === 'conversation'"
        />
        <template v-if="firstLoad">
          <contacts-list
            ref="contactsList"
            v-show="active === 'contacts'"
            @select="onSelect"
          />
          <class-list
            ref="classList"
            v-show="active === 'class'"
            @select="onSelect"
          />
        </template>
      </el-aside>
      <!-- 判断是否有会话 -->
      <el-container
        v-show="currentConversation.targetId && active === 'conversation'"
      >
        <el-main style="padding: 0" id="el-main">
          <message-list ref="messageList"></message-list>
        </el-main>
        <el-footer
          style="height: auto; border-top: 1px solid #f7f7f7; padding: 0 20px;"
        >
          <message-editor ref="messageEditor"></message-editor>
        </el-footer>
      </el-container>
      <el-empty
        v-if="!currentConversation.targetId || active !== 'conversation'"
        :image="emptyImg"
        style="width: 100%;height: 100%"
        description=" "
      ></el-empty>
    </el-container>
  </div>
</template>

<script>
import ConversationList from "../components/conversation-list.vue";
import MessageList from "../components/message-list.vue";
import MessageEditor from "../components/message-editor.vue";
import ContactsList from "../components/contacts-list.vue";
import ClassList from "../components/class-list.vue";
import { core, CoreEvent } from "../imkit";
import * as RongIMLib from "@rongcloud/imlib-next";
import { custom_service } from "./chat.js";
import ItemVue from "../Sidebar/Item.vue";
// 接入时需要将 '请更换您应用的 appkey' 替换为您的应用的 appkey
let libOption = { appkey: "c9kqb3rdc451j" };

// 初始化 SDK
core.init({
  service: custom_service,
  libOption: libOption,
  conversationPullCount: 20
});

export default {
  components: {
    ConversationList,
    MessageList,
    MessageEditor,
    ContactsList,
    ClassList
  },
  data() {
    return {
      emptyImg: require("../imkit/images/no-conversation.svg"),
      active: "conversation", // 会话 conversation; 联系人 contacts; 班级 class
      firstLoad: false, // 是否加载了一次
      isConnect: false, // 连接中
      currentConversation: {
        channelId: "",
        conversationType: 0,
        targetId: ""
      }
    };
  },
  mounted() {
    core.connect(this.$store.state.user.imToken).then(res => {
      if (res.code === RongIMLib.ErrorCode.SUCCESS) {
        console.log("链接成功, 链接用户 id 为: ", res.data.userId);
        this.isConnect = true;
      } else {
        console.warn("链接失败, code:", res.code);
      }
    });
    core.on(CoreEvent.SWITCH_CONVERSATION, this.switchConversation);

    // 注册消息监听器
    // core.on("tapConversation", function(e) {
    //   console.log("tapConversation", e);
    // });
  },
  methods: {
    onSelect(item) {
      console.log(item, "item");
      core.selectConversation({
        conversationType: item.conversationType,
        targetId: item.targetId + "",
        channelId: item.channelId
      });
      this.active = "conversation";
    },
    switchConversation(conversation) {
      console.log(conversation, "switchConversation chat-model");
      this.currentConversation = conversation
        ? conversation
        : {
            channelId: "",
            conversationType: 0,
            targetId: ""
          };
    }
  },
  destoryed() {
    core.off(CoreEvent.SWITCH_CONVERSATION, this.switchConversation);
  }
};
</script>

<style lang="scss" scoped>
.chat-message {
  height: 100%;
  display: flex;
}

.chat-header {
  position: absolute;
  left: 0;
  right: 0;
  background: #f7f7f7;
  width: 100%;
  height: 56px;

  .el-icon-close {
    position: absolute;
    top: 0;
    right: 12px;
    line-height: 56px;
    font-size: 18px;
    font-weight: 600;
    text-align: center;
    color: #333;
    cursor: pointer;
  }

  .header-card {
    cursor: pointer;
    line-height: 56px;
    text-align: center;
    font-size: 30px;
    color: #333;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 56px;
    .icon-chat {
      background: url("../images/icon_chat_default.svg") no-repeat;
      background-size: contain;

      &:hover,
      &.active {
        background: url("../images/icon_chat.svg") no-repeat;
        background-size: contain;
      }
    }

    .icon-message {
      background: url("../images/icon_message_default.svg") no-repeat;
      background-size: contain;
      &:hover,
      &.active {
        background: url("../images/icon_message.svg") no-repeat;
        background-size: contain;
      }
    }

    .icon-class {
      background: url("../images/icon_class_default.svg") no-repeat;
      background-size: contain;
      &:hover,
      &.active {
        background: url("../images/icon_class.svg") no-repeat;
        background-size: contain;
      }
    }

    .icon {
      margin: 0 25px;
      width: 24px;
      height: 24px;
      display: inline-block;
    }
  }
}

.chat-conversation {
  float: left;
  width: 20vw;
  height: 100%;
  overflow: hidden;
}
.chat-container {
  width: calc(100% - 20vw);
}

.chat-massage-list {
  height: calc(100% - 150px);
}
.chat-massage-editor {
  height: 150px;
}
/deep/.no-data::before {
  background-size: cover;
}
</style>
