<!--
 * @File: index
 * @Author: 裴浩宇
 * @Date: 2023/6/6 10:01
 * @Description: 聊天室
-->
<template>
  <div class="page">
    <div class="chat-room">
      <div :style="chat" class="chat" @click="previewImage($event)">
        <div class="record" ref="record">
          <div class="phone-info">
            <div class="phone-online-number">
              {{ `在线人数（${memberList.length}）` }}
            </div>
            <i class="el-icon-edit-outline" @click="openInfo"/>
          </div>
          <div class="more" v-if="recordList.length > 50 && showRecordList.length < recordList.length"
               @click="handleGetMore">
            <svg-icon icon-class="time"/>
            查看更多消息
          </div>
          <div class="record-one" :class="Number(one.userId) === Number(userInfo.id) ? 'is-me' : ''"
               v-for="(one, index) in showRecordList"
               :key="one.userId + index">
            <div class="left">
              <el-avatar :size="36" shape="square" :src="one.avatar">
                <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
              </el-avatar>
            </div>
            <div class="right">
              <div class="nick-name" v-if="one.userId === userInfo.id">
                <el-tooltip class="item" effect="dark" content="回复" placement="top">
                  <i class="el-icon-s-comment" @click="handleReply(one)"></i>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" :content="one.senTime" placement="top">
                  <span class="time">{{ parseTime(one.senTime, '{h}:{i}') }}</span>
                </el-tooltip>
                （{{ one.location && one.location.ip }}）
                {{ one.nickName }}
              </div>
              <div class="nick-name" v-else>
                {{ one.nickName }}（{{ one.ip }}）
                <el-tooltip class="item" effect="dark" :content="one.senTime" placement="top">
                  <span class="time">{{ parseTime(one.senTime, '{h}:{i}') }}</span>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="回复" placement="top">
                  <i class="el-icon-s-comment" @click="handleReply(one)"></i>
                </el-tooltip>
              </div>
              <div class="message" v-html="one.message"></div>
            </div>
          </div>
        </div>
        <div class="input-box">
          <div class="function-button">
            <el-popover
              placement="top"
              title="选择表情"
              trigger="click">
              <emoji @select-icon="handleSelectEmoji"/>
              <i class="el-icon-star-off" slot="reference"></i>
            </el-popover>
            <el-popover
              placement="top"
              title="选择文件"
              trigger="click">
              <file @select-file="handleSelectFile"/>
              <i class="el-icon-folder" slot="reference"></i>
            </el-popover>
          </div>
          <div class="textarea" contenteditable
               ref="message" @keyup.enter="sendMessage"></div>
          <el-button
            :loading="sendMessageLoading"
            size="mini"
            @click="sendMessage"
            type="primary">
            发 送
          </el-button>
        </div>
      </div>
      <div :style="roomInfo" class="room-info">
        <div class="user-info">
          <i class="el-icon-arrow-left return" @click="returnChat"/>
          <div class="message-board-right" v-loading="userInfoLoading">
            <div class="user-head">
              <el-image :src="userInfo.avatar" fit="scale-down">
                <div slot="error" class="image-slot">
                  获取图片
                </div>
              </el-image>
            </div>
            <div class="customer-name">
              <div class="label">昵称：</div>
              <div class="name">
                <el-tooltip :content="userInfo.nickName" placement="top" effect="light">
                  <span>{{ userInfo.nickName }}</span>
                </el-tooltip>
              </div>
              <div class="ip">（{{ userInfo.location?.ip }}）</div>
            </div>
          </div>
        </div>
        <div class="member">
          <div class="online-number">
            {{ `在线人数（${memberList.length}）` }}
          </div>
          <div class="member-list">
            <div class="member-one" v-for="one in memberList" :key="one.userId">
              <el-avatar :size="24" shape="square" :src="one.avatar">
                <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
              </el-avatar>
              <div class="nick-name">{{ one.nickName }}（{{ one.location && one.location.ip }}）</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!--  预览图片  -->
    <el-dialog :visible.sync="imgVisible" :close-on-press-escape="true" class="img-model" :modal="false">
      <model-image :previewImage="imgUrl" :imgSrcList="photoUrlList" v-if="imgVisible"/>
    </el-dialog>
  </div>
</template>

<script>
import { getMessageRecord, loginChat, sendMessage, signOut } from '@/api'
import { WEBSOCKET_MESSAGE_TYPE } from '@/assets/js/common'

export default {
  name: 'Chat',
  components: {
    file: () => import('./file.vue'),
    emoji: () => import('./emoji.vue'),
    ModelImage: () => import('@/components/ModelImage')
  },
  data () {
    return {
      // websocket
      websocket: {},
      // 查看更多聊天记录
      more: 0,
      // 图片标志
      imgVisible: false,
      // 图片地址
      imgUrl: '',
      // 图片url列表
      photoUrlList: [],
      // 聊天样式
      chat: '',
      // 个人信息样式
      roomInfo: '',
      // 发送信息加载标志
      sendMessageLoading: false,
      // 个人信息
      userInfo: this.$store.state.userInfo,
      // 聊天信息内容
      recordList: [],
      // 在线人数
      memberList: [],
      // 用户信息加载
      userInfoLoading: false
    }
  },
  computed: {
    /**
     * 展示消息
     */
    showRecordList () {
      return this.recordList.slice(-50 - this.more)
    }
  },
  mounted () {
    if (!this.$store.state.userInfo?.id) {
      // 判断当前用户是否已拉取完user_info信息
      this.$store.dispatch('GetInfo').then(res => {
        this.initWebSocket()
      })
    }
  },
  methods: {
    // 日期格式化
    parseTime (time, pattern) {
      if (arguments.length === 0 || !time) {
        return null
      }
      const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
      let date
      if (typeof time === 'object') {
        date = time
      } else {
        if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
          time = parseInt(time)
        } else if (typeof time === 'string') {
          time = time.replace(new RegExp(/-/gm), '/')
        }
        if ((typeof time === 'number') && (time.toString().length === 10)) {
          time = time * 1000
        }
        date = new Date(time)
      }
      const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay()
      }
      const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
        let value = formatObj[key]
        // Note: getDay() returns 0 on Sunday
        if (key === 'a') {
          return ['日', '一', '二', '三', '四', '五', '六'][value]
        }
        if (result.length > 0 && value < 10) {
          value = '0' + value
        }
        return value || 0
      })
      return time_str
    },
    /**
     * 滚动最下面
     */
    scroll () {
      const imgInterval = setInterval(() => {
        let imgFlag = true
        document.querySelectorAll('.record img').forEach(img => {
          try {
            if (img.height === 0) {
              imgFlag = false
            }
          } catch (e) {
            throw new Error()
          }
        })
        if (imgFlag) {
          clearInterval(imgInterval)
          const ele = this.$refs.record
          ele.scrollTop = ele.scrollHeight
        }
      }, 100)
    },
    /**
     * 回复
     */
    handleReply (message) {
      const html = this.$refs.message
      const reply = document.createElement('div')
      const reg = /<img.*?src=["|']?(.*?)["|']*?>/g
      message.message = message.message.replace(reg, '[图片]')
      reply.classList.add('chat-reply')
      reply.setAttribute('contentEditable', 'false')
      reply.innerHTML = `<div class="reply-icon"></div>回复 ${message.nickName}：${message.message}`
      if (!html.innerHTML) {
        html.innerHTML = `<div>${html.innerHTML}</div>`
      }
      if (html.children[0] && html.children[0].getAttribute('class') === 'chat-reply') {
        html.children[0].innerHTML = `<div class="reply-icon"></div>回复 ${message.nickName}：${message.message}`
      } else {
        html.insertBefore(reply, html.children[0])
        html.innerHTML += '<br>'
      }
    },
    /**
     * 设置光标位置
     */
    handlerFocus (el) {
      if (typeof window.getSelection !== 'undefined' && typeof document.createRange !== 'undefined') {
        const range = document.createRange()
        range.selectNodeContents(el)
        range.collapse(false) // 将光标定位到内容的末尾
        const sel = window.getSelection()
        sel.removeAllRanges()
        sel.addRange(range)
      } else if (typeof document.body.createTextRange !== 'undefined') {
        const textRange = document.body.createTextRange()
        textRange.moveToElementText(el)
        textRange.collapse(false) // 将光标定位到内容的末尾
        textRange.select()
      }
    },
    /**
     * 选择表情
     */
    handleSelectEmoji (emoji) {
      if (emoji.position) {
        this.$refs.message.innerHTML += `<span contentEditable="false" class="item-icon" style="background-position: ${emoji.position}"></span>`
      } else {
        this.$refs.message.innerHTML += emoji
      }
      this.handlerFocus(this.$refs.message)
      this.$refs.message.scrollTop = this.$refs.message.scrollHeight
    },
    /**
     * 选择文件
     */
    handleSelectFile (file) {
      if (file.isPicture) {
        this.$refs.message.innerHTML += `<div class="image"><img src="${file.url}" alt="${file.name}"></div><br>`
      } else {
        this.$refs.message.innerHTML +=
          `<div class="chat-file" contentEditable="false">
    <div class="top">
        <i class="el-icon-folder"></i>
        ${file.name}
    </div>
    <div class="bottom">
        <a href="${file.url}">下载</a>
    </div>
</div><br>`
      }
      this.handlerFocus(this.$refs.message)
      this.$refs.message.scrollTop = this.$refs.message.scrollHeight
    },
    /**
     * 获取消息
     */
    getMessageRecord () {
      getMessageRecord().then(res => {
        this.recordList = res.data
        this.recordList.map(record => {
          const userInfo = this.memberList.find(item => item.userId === record.userId)
          if (!userInfo) return record
          record.nickName = userInfo.nickName
          record.avatar = userInfo.avatar
          record.ip = userInfo.location.ip
          return record
        })
        this.scroll()
      })
    },
    /**
     * 查看更多
     */
    handleGetMore () {
      this.more += 10
    },
    /**
     * 预览图片
     * @param event
     */
    previewImage (event) {
      if (event.target.nodeName === 'IMG') {
        this.photoUrlList = []
        // eslint-disable-next-line no-undef
        const obj = $('.markdown-body img')
        for (let i = 0; i < obj.length; i++) {
          this.photoUrlList.push(obj[i].getAttribute('src'))
        }
        this.imgVisible = true
        this.imgUrl = event.target.getAttribute('src')
      }
    },
    /**
     * 发送信息
     */
    sendMessage () {
      if (this.sendMessageLoading) {
        return
      }
      if (this.websocket.readyState !== 1) {
        this.initWebSocket()
      }
      if (!this.$refs.message.innerHTML.replace(/<div><br><\/div>/g, '')) {
        this.$message.error('发送内容不能为空，请重新输入')
        this.$refs.message.innerHTML = ''
      } else {
        let message = this.$refs.message.innerHTML.replace(/<div><br><\/div>/g, '')
        message = message.replace(/<br>/g, '')
        this.$refs.message.innerHTML = ''
        this.sendMessageLoading = true
        sendMessage({ message: message }).then(res => {
          if (res.code === 200) {
            this.sendMessageLoading = false
          }
        })
      }
    },
    /**
     * 返回聊天
     */
    returnChat () {
      this.chat = 'display: flex!important;'
      this.roomInfo = 'display: none!important;'
    },
    /**
     * 打开信息
     */
    openInfo () {
      this.chat = 'display: none!important;'
      this.roomInfo = 'display: flex!important;'
    },
    /**
     * 退出登录
     */
    signOut () {
      signOut().then(res => {
        this.memberList = res.data
        this.recordList = []
        this.memberList = []
        this.onbeforeunload()
      })
    },
    /**
     * 初始化webSocket
     */
    initWebSocket () {
      // WebSocket
      if ('WebSocket' in window) {
        this.websocket = new WebSocket(`${process.env.VUE_APP_SOCKET}/${this.userInfo.id}`)
        // 连接错误
        this.websocket.onerror = this.setErrorMessage
        // 连接成功
        this.websocket.onopen = this.setOnopenMessage
        // 收到消息的回调
        this.websocket.onmessage = this.setOnmessageMessage
        // 连接关闭的回调
        this.websocket.onclose = this.setOncloseMessage
      } else {
        alert('当前浏览器 Not support websocket')
      }
      // 监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
      window.onbeforeunload = this.onbeforeunload
    },
    setErrorMessage () {
      console.log('WebSocket连接发生错误   状态码：' + this.websocket.readyState)
    },
    setOnopenMessage () {
      console.log('WebSocket连接成功    状态码：' + this.websocket.readyState)
      this.userInfoLoading = true
      loginChat().then(res => {
        if (res.data.length > 0) {
          this.memberList = res.data.filter(item => {
            return item.avatar && item.nickName
          })
          this.userInfoLoading = false
          this.getMessageRecord()
        } else {
          this.$message.error('网络异常，请清理缓存刷新当前页面')
        }
      })
    },
    setOnmessageMessage (event) {
      // 根据服务器推送的消息做自己的业务处理
      const data = JSON.parse(event.data)
      switch (data.webSocket) {
        case WEBSOCKET_MESSAGE_TYPE.CHAT_MESSAGE:
          // eslint-disable-next-line no-case-declarations
          const message = data.message
          // 消费消息
          // eslint-disable-next-line no-case-declarations
          const userInfo = this.memberList.find(item => Number(item.userId) === Number(message.userId))
          message.nickName = userInfo.nickName
          message.avatar = userInfo.avatar
          message.ip = userInfo.location.ip
          this.recordList.push(message)
          this.scroll()
          break
        case WEBSOCKET_MESSAGE_TYPE.LOGIN:
          // 登录
          // eslint-disable-next-line no-case-declarations
          let flag = true
          this.memberList.forEach(item => {
            if (data.message.userId === item.userId) {
              flag = false
            }
          })
          if (flag) {
            this.memberList.push(data.message)
          }
          break
        case WEBSOCKET_MESSAGE_TYPE.LOG_OUT:
          // 退出登录
          this.memberList = this.memberList.filter(item => {
            return item.userId !== data.userId
          })
          break
      }
    },
    setOncloseMessage () {
      console.log('WebSocket连接关闭    状态码：' + this.websocket.readyState)
    },
    onbeforeunload () {
      this.closeWebSocket()
    },
    closeWebSocket () {
      this.websocket.close()
    }
  },
  beforeDestroy () {
    this.signOut()
  }
}
</script>

<style lang="scss" scoped>
@media screen and (max-width: 1000px) {
  .chat-room {
    height: 94% !important;
    width: 94% !important;

    .user-info {
      .return {
        display: flex !important;
        font-size: 1.2rem;
        padding: 0.8rem 0.5rem;
        position: absolute;
      }
    }

    .phone-info {
      display: flex;
      align-items: center;
      justify-content: space-between;
      color: #909090;
      margin-bottom: 1rem;

      i {
        font-size: 1.2rem;
      }
    }

    .room-info {
      display: none !important;
    }
  }
}

.phone-info {
  display: none;
}

#chat-room {
  display: flex;
  height: 90vh;
  justify-content: center;
  align-items: center;
}

.chat-room {
  display: flex;
  height: 82vh;
  border: 1px solid #d9d9d9;
  border-radius: 5px;
  background-color: #F5F6F7;

  .chat {
    flex: 7;
    border-right: 1px solid #d9d9d9;
    display: flex;
    flex-flow: column;
    width: 70%;

    ::v-deep .item-icon {
      width: 24px;
      height: 24px;
      display: inline-block;
      background-image: url('../../assets/images/emoji/emoji.png');
      background-size: 1100% 1000%;
      background-position: 0 0;
      cursor: pointer;
    }

    ::v-deep .chat-file {
      background: #FFF;
      border-radius: 0.3rem;
      border: 1px solid #EBEBEB;
      margin-top: 0.5rem;
      user-select: none;

      &:first-child {
        margin-top: 0;
      }

      .top {
        display: flex;
        align-items: center;
        padding: 1rem;
        color: #666;

        i {
          font-size: 2rem;
          color: #5A8DEE;
          margin-right: 0.5rem;
        }
      }

      .bottom {
        padding: 0.5rem 1rem;
        border-top: 1px solid #EBEBEB;
        color: #5A8DEE;
        font-size: 0.9rem;
        text-align: right;

        &:hover {
          text-decoration: underline;
        }
      }
    }

    ::v-deep .chat-reply {
      padding: 0.5rem;
      color: #8F959E;
      background-color: #e8e9eb;
      border-radius: 0.2rem;

      .reply-icon {
        width: 0.2rem;
        height: 1rem;
        background-color: #BBBFC4;
        display: inline-block;
        margin-right: 0.5rem;
      }
    }

    .record {
      flex: 5;
      border-bottom: 1px solid #d9d9d9;
      height: 28rem;
      overflow-y: auto;
      padding: 1rem 1rem 0 1rem;

      .more {
        display: flex;
        align-items: center;
        justify-content: center;
        color: #2875FF;
        cursor: pointer;
        user-select: none;

        .svg-icon {
          margin-right: 0.5rem;
        }
      }

      .record-one {
        display: flex;
        margin-bottom: 1rem;

        &:hover {
          .el-icon-s-comment {
            display: block !important;
          }
        }

        .left {
          margin-right: 0.5rem;
        }

        .right {
          .nick-name {
            color: #999;
            font-size: 0.9rem;
            margin-left: 0.5rem;
            display: flex;
            align-items: center;
            height: 1.2rem;

            .el-icon-s-comment {
              display: none;
              font-size: 1.4rem;
              margin-left: 0.5rem;
              cursor: pointer;

              &:hover {
                color: #5A8DEE;
              }
            }
          }

          .message {
            margin-top: 0.5rem;
            padding: 0.5rem;
            background-color: #fff;
            border-radius: 5px;
            width: fit-content;
            display: flex;
            align-items: flex-end;
            flex-wrap: wrap;
            flex-direction: column;

            ::v-deep img {
              max-width: 10rem;
              cursor: pointer;
              max-height: 10rem;
            }
          }
        }
      }

      .is-me {
        flex-flow: row-reverse !important;

        .message {
          -moz-border-radius-topright: 0;
        }

        .right {
          display: flex;
          flex-direction: column;
          align-items: flex-end;

          .nick-name {
            text-align: right;
            margin-right: 0.5rem;

            .el-icon-s-comment {
              margin-right: 0.5rem;
            }
          }

          .message {
            margin-left: 0 !important;
            margin-right: 0.5rem;
            background-color: #5A8DEE;
            color: #FFFFFF;
          }

          .message:before {
            display: none;
          }
        }
      }
    }

    .input-box {
      flex: 2;
      padding: 0 1rem 1rem 1rem;
      display: flex;
      flex-flow: column;
      overflow: hidden;

      .function-button {
        padding-top: 0.6rem;

        i {
          font-size: 1.5rem;
          padding: 0.5rem;
          margin-right: 0.4rem;

          &:hover {
            background-color: #E7E8E9;
          }
        }
      }

      .textarea {
        flex: 8;
        padding: 0.5rem 0;
        overflow: scroll;

        ::v-deep img {
          max-width: 10rem;
        }
      }

      .button {
        flex: 2;
        display: flex;
        justify-content: flex-end;
        padding-top: 0.5rem;
      }
    }

  }

  .room-info {
    flex: 3;
    display: flex;
    flex-flow: column;

    .user-info {
      flex: 2;
      border-bottom: 1px solid #d9d9d9;

      .return {
        display: none;
      }

      .message-board-right {
        display: flex;
        padding: 1rem;
        flex-flow: column;
        align-items: center;
        justify-content: space-around;

        .user-head {

          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;

          .el-image {
            border-radius: 1rem;
          }
        }

        .customer-name {
          height: 2rem;
          width: 100%;
          margin-top: 0.5rem;
          display: flex;
          align-items: center;

          .label {
            white-space: nowrap;
          }

          .name {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            color: #5A8DEE;
          }
        }

        .button {
          padding-top: 0.5rem;
          display: flex;
        }

        .your-header {
          width: 6rem;
          height: 6rem;
          border: 1px solid #999;
          border-radius: 5px;
          display: flex;
          flex-flow: column;
          align-items: center;
          justify-content: center;

          #headerPhoto {
            width: 7rem;
            position: absolute;
            transform: scale(0.7);
            margin-top: 1rem;
          }

          ::v-deep .image-slot {
            font-size: 0.4rem;
            transform: scale(0.8);
            text-align: center;
          }

          .close-icon {
            position: absolute;
            margin: -5rem -5rem 0 0;
            color: red;
          }

          .header-picture {
            width: 100%;
            height: 100%;
            padding: 0.5rem;
            display: flex;
            justify-content: center;
            white-space: nowrap;

            img {
              width: 100%;
              height: 100%;
            }
          }
        }
      }

    }

    .member {
      flex: 5;
      padding: 1rem;

      .online-number {

      }

      .member-list {
        .member-one {
          display: flex;
          align-items: center;
          padding-top: 0.5rem;

          .nick-name {
            margin-left: 0.5rem;
          }
        }
      }
    }
  }
}

/* 图片预览样式开始*/
.el-image-viewer__canvas {
  width: 60%;
}

.el-image-viewer__next {
  right: calc(40vw + 4rem);
}

.img-model {
  ::v-deep .el-dialog {
    margin: 20vh 25vw !important;
    width: 50vw;
    height: 60vh;
    background-color: rgb(222, 222, 222) !important;

    .el-dialog__body {
      height: 90%;
      overflow: auto;
    }
  }
}
</style>
