<template>
  <div class="chat-room">
    <div class="mini-wrap moblie-wrap">
      <div class="mini-body">
        <div class="box">
          <back-bar :title="isPrivate ? group.privateName : group.groupName+'('+group.membersNum+')'"
                    :url="!isPrivate && group.url ?group.url:''"
          >
            <span class="group-set-icon el-icon-more" v-if="!isPrivate &&isJoinGroup!=0" @click="$router.push(`/groupSet/${group.groupId}`)"></span>
          </back-bar>

          <chat-pin v-bind="pinMsg" @pinMsgClose="pinMsgClose" @scrollToView="scrollToView"></chat-pin>
          <div class="box-bd" ref="msgBox">
            <div ref="scrollWrap"
              @scroll.prevent="handleScroll"
              class="scroller"
            >
              <div ref="msgWrap" class="msg-wrap">
                <div class="msg-top-more" v-if="lockEnd">
                  <em>{{ $t('chat.noMore') }}</em>
                </div>
                <div class="msg-top-load" v-if="lockMore && !lockEnd">
                  <i class="msg-loading-icon"></i>
                </div>
                <msg-item v-for="(item, key) in group.chatList"
                  :key="key"
                  :isMobile="true"
                  :isAdmin="isAdmin"
                  v-bind="item"
                  :msgItem="item"
                  @quoteMsg="quoteMsg"
                  @deleteMsg="deleteMsg"
                ></msg-item>
              </div>
            </div>
            <at-me :atList="atList" class="mini" @scrollToMsg="scrollToMsg"></at-me>
            <div class="msg-unread"
              @click="doSetRead"
              v-show="unreadNums > 0 && enableScroll && !isBottom">
              <em><i class="el-icon-d-arrow-right"></i>{{unreadNums}}{{ $t('chat.unreadMsg') }}</em>
            </div>
          </div>
          <div class="box-ft" v-if="isJoinGroup==0">
            <div @click="joinGroup()" class="btn-join">{{ $t('chat.joinGroup') }}</div>
          </div>
          <div class="box-ft" v-else>
            <chat-at
              ref="chatAt"
              v-if="atShow"
              @atperson="atPerson"
              :curInd="atInd"
              :filterList="filterMembers">
            </chat-at>
            <div class="input-con">
              <div class="more-icon" @click.stop="handleMoreClick"></div>
              <form class="input-wrap" @submit="handleSend">
                <textarea
                  @input="handleInput"
                  @keydown.up.prevent="handleUp"
                  @keydown.down.prevent="handleDown"
                  @keydown.left="handleLeft"
                  @keydown.right="handleRight"
                  @keydown.delete="handleDel"
                  @keydown.esc="handleEsc"
                  cols="1"
                  ref="chatInput"
                  rows="1"
                  @keydown.enter="handleKeyDown"
                  placeholder="Write a message"
                  v-model="inputMsg"
                  @focus="handleFocus"
                  @blur="handleBlur"
                  :style="{height:inputHeight}"
                />
              </form>
              <div class="emoji-icon" @click.stop="handleEmojiClick"></div>
              <div class="btn-send" @click="handleSend">{{$t('chat.send')}}</div>
            </div>

            <toolbar ref="toolbar" :toolShow="toolShow" @handleFile="handleFile"></toolbar>

            <emoji @addEmoji="addEmoji" :emojiShow="emojiShow" v-show="emojiShow" @closeEmojiList="closeEmojiList"></emoji>
          </div>
        </div>
        <div class="pub-loading" v-show="isLoadingRoom"></div>
      </div>
    </div>
  </div>
</template>

<script>
import backBar from '@/components/backBar'
import msgItem from '@/components/msgItem'
import emoji from '@/components/emoji'
import chatAt from '@/components/chatAt'
import atMe from '@/components/chatAt/atme'
import chatPin from '@/components/chatPin'
import toolbar from '@/components/chatInput/toolbar'
import { chatAtMixin, chatInputMixin } from '@/mixins'
import { chatMixin, inputMixin } from '@/mixins/chat'

export default {
  name: 'h5ChatRoom',
  mixins: [chatAtMixin, chatInputMixin, chatMixin, inputMixin],
  components: {
    msgItem,
    emoji,
    chatAt,
    chatPin,
    atMe,
    backBar,
    toolbar
  },
  data () {
    return {
      emojiShow: false,
      toolShow: false,
      inputHeight: 18
    }
  },
  watch: {
    inputMsg (val, newval) {
      let ele = this.$refs.chatInput
      this.inputHeight = 'auto'

      this.$nextTick(() => {
        this.inputHeight = Math.max(18, Math.min(ele.scrollHeight, 75)) + 'px'
      })
    }
  },
  methods: {
    handleEmojiClick () {
      this.toolShow = false
      this.emojiShow = !this.emojiShow

      this.checkNeedToBottom()
    },
    handleMoreClick () {
      this.emojiShow = false
      this.toolShow = !this.toolShow

      this.checkNeedToBottom()
    },
    checkNeedToBottom () {
      if (!this.isBottom) return

      this.$nextTick(() => {
        this.resizeToBottom()
      })
    },
    /**
     * @des 引用某条消息
     */
    quoteMsg (msg) {
      this.inputMsg = msg
    }
  }
}
</script>

<style lang="scss" scoped>
@charset "UTF-8";

$chatBg: #34363c;
$chatContBg: #eeeeee;
$chatUiFont: #ffffff;
$offsetBottom: 5px;
$offsetRight: 5px;

.group-set-icon{
  position: absolute;
  right: 0;
  top: 0;
  height: px2rem(90);
  width: px2rem(100);
  text-align: center;
  font-size: px2rem(42);
  line-height: px2rem(90);
  color: #333333;
}

.mini-wrap{
  z-index: 123456789;
  height: 100%;
}

.mini-body{
  height: 100%;
  box-sizing: border-box;
}

.box{
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.chat-room{
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: 100%;
}

.box-bd{
  height: 0;
  flex: 1;
  position: relative;
  background: $chatContBg;
  .msg-unread{
    position: absolute;
    width: 120px;
    left: 50%;
    bottom: 10px;
    border-radius: 20px;
    background: rgba(#000000, 0.5);
    padding: 0 5px;
    text-align: center;
    line-height: 32px;
    margin-left: -65px;
    cursor: pointer;
    &:hover{
      background: rgba(#000000, 0.3);
    }
    em{
      color: #ffffff;
      font-size: 14px;
    }
    i{
      transform: rotate(90deg);
      margin-right: 5px;
    }
  }
}
.scroller{
  height: 100%;
  overflow-x: hidden;
  overflow-y: scroll;
  -webkit-overflow-scrolling: touch;
  &::-webkit-scrollbar {
    width: 8px;
    height: 6px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    background-color: rgba($color: #8d8a8a, $alpha: 0.2);
  }
  &::-webkit-scrollbar-track {
    background-color: transparent;
  }
}

.msg-wrap{
  box-sizing: border-box;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding-bottom: 16px;
  overflow: hidden;
}

.box-ft{
  position: relative;
  background: $chatContBg;
  border-top: 1px solid #d6d6d6;
  .btn-join{
    color: #438aff;
    font-size: 16px;
    text-align: center;
    padding: px2rem(30) 0;
    background: #FFF;
  }
  .input-con{
    position: relative;
    display: flex;
    align-items: flex-end;
    background-color: #fafafa;
    padding: 6px 0;
  }
  .input-wrap{
    flex: 1;
    padding: 10px 0;
    background-color: #ffffff;
    border-radius: 4px;
  }
  .more-icon{
    width: 38px;
    height: 38px;
    background: url('../../../assets/more-icon.png') center center no-repeat;
    background-size: 22px 22px;
    cursor: pointer;
    &:hover{
      opacity: .6;
    }
  }
  .emoji-icon{
    width: 38px;
    height: 38px;
    background: url('../../../assets/m-face-icon.png') center center no-repeat;
    background-size: 22px 22px;
    cursor: pointer;
    &:hover{
      opacity: .6;
    }
  }
  .btn-send{
    margin-right: 4px;
    font-size: 12px;
    color: #ffffff;
    padding: 0 10px;
    height: 28px;
    line-height: 28px;
    margin-bottom: 3px;
    background: #2b9ff6;
    border-radius: 3px;
    cursor: pointer;
    &:hover{
      opacity: 0.8;
    }
  }
  form{
    @include flex(1);
  }
  textarea {
    display: block;
    width: 100%;
    height: 18px;
    max-height: 175px;
    font-size: 14px;
    color: #000000;
    line-height: 16px;
    padding: 1px;
    padding-left: 8px;
    margin: 0;
    border: none;
    outline: none;
    background: none;
    box-sizing: border-box;
    resize:  none;
  }
  .emoji-wrap{
    background: #f2f2f2;
    box-shadow: 1px 1px 50px rgba(0,0,0,.3);
    z-index: 10;
  }
  .input-ctrl{
    span{
      width: 120px;
      margin: 4px auto;
      height: 30px;
      line-height: 30px;
      color: #ffffff;
      font-size: 12px;
      text-align: center;
      display: block;
      background-image: -webkit-linear-gradient( 90deg, rgb(25,145,235) 0%, rgb(46,161,248) 100%);
      border-radius: 3px;
      &.enable{
        cursor: pointer;
        &:hover{
          opacity: 0.7;
        }
      }
    }
  }
}

.msg-top-more{
  margin-top: 10px;
  text-align: center;
  i{
    font-size: 30px;
  }
  em{
    font-size: 12px;
    line-height: 20px;
    color: #999999;
  }
}
.msg-loading-icon{
  display: block;
  background: url('../../../assets/loading-icon.png') no-repeat;
  background-size: 100%;
  width: 16px;
  height: 16px;
  margin: 12px auto;
  animation: rotate 2s linear infinite;
}

// 手机端适配
.moblie-wrap{
  .box-hd{
    height: 30px;
  }
  .box-ft{
    @media #{$iphonex} {
      padding-bottom: 34px;
      background-color: #fafafa;
    }
    .emoji-wrap{
      bottom: 54px;
    }
    .btn-send{
      height: 36px;
      line-height: 36px;
      padding: 0 10px;
      font-size: 16px;
      margin-bottom: 0;
    }
    .input-ctrl span {
      height: 40px;
      line-height: 40px;
      font-size: 16px;
    }
    .input-con{
      padding: 8px 0;
      align-items: center;
    }
    .input-wrap{
      padding: 10px 0;
      margin: 0 6px;
      .textarea{
        height: 26px;
        line-height: 20px;
      }
    }
    .more-icon{
      background-size: 30px 30px;
    }
    .emoji-icon{
      background-size: 30px 30px;
      margin-right: 4px;
    }
  }
}
</style>
