<template>
  <!-- 聊天窗口 -->
  <div class="chat">
    <div class="chat__head">
      <div class="chat__head--title"> 会话窗口</div>
    </div>
    <div ref="content" class="chat__container" :style="{height: listHeight+'px'}">
      <ul class="chat__container--list">
        <div v-for="(nor, index) in textList" :key="index" style="margin-top: 5px;">
          <li v-if="nor.self" style="position: relative">
            <div class="userName" style="position: absolute; right: 4px; top: 4px; font-size: 14px;">
              {{ nor.chatTime }} {{ nor.userName }}</div>
            <div class="news">
              <span>{{ nor.value }}</span>
            </div>
          </li>
          <li v-if="nor.other" style="position: relative">
            <div class="userName" style="position: absolute; left: 12px; top: 4px; font-size: 14px;">
              {{ nor.userName }} {{ nor.chatTime }}</div>
            <div class="answers">
              <span>{{ nor.value }}</span>
            </div>
          </li>
          <li v-if="nor.join" style="font-size: 12px; text-align: center;">{{ nor.roomTip }}</li>
        </div>
      </ul>
    </div>
    <div class="chat__footer">
      <textarea id="dope" v-model="text" class="chat__footer--text" style="width: 99%;height: 47px; border: none;outline: none;" @keyup="handleSetInputState" />
      <button class="chat__footer--send" :disabled="disabled" @click="handleSendText">{{ $t('trainRoom.sendText') }}</button>
    </div>
  </div>
</template>

<script>
import { chatWithText } from '@/api/chat';

export default {
    props: {
        room: {
            type: Object,
            required: true
        },
        members: {
            type: Array,
            required: true
        },
        height: {
            type: Number,
            required: true
        }
    },
    data() {
        return {
            textList: [],
            text: '',
            sending: false,
            background: '',
            disabled:true
        };
    },
    computed: {
        listHeight() {
            return this.height - 90;
        },
        group() {
            return this.$route.query.group;
        },
        userId() {
            return this.$store.state.user ? this.$store.state.user.id : '';
        }
    },
    watch: {
        '$store.state.socket.chatContent': function (val) { // 房间内语音聊天
            if (val.chatInfo) {
                this.handelTextList(val);
            }
        }
    },
    methods: {
        async handleSetInputState() {
            if (!this.text.trim()) {
                this.disabled = true;
            } else {
                this.disabled = false;
            }
        },
        async handleSendText() {
            try {
                if (this.text.trim()) {
                    await chatWithText(this.text, this.group);
                    this.text = '';
                    this.disabled = true;
                }
            } catch (error) {
                console.error(error);
            }
        },
        async handelTextList(params) {
            if (!params.inSimulation) {
                this.textList.push(params);
                this.textList.sort((a, b) => {
                    return a.date - b.date;
                });
            }
            this.sending = false;
            this.$store.dispatch('socket/setChatContent', {});
            this.$nextTick(() => {
                if (this.$refs.content) {
                    this.$refs.content.scrollTop = this.$refs.content.scrollHeight;
                }
            });
        }
    }
};
</script>

<style scoped lang="scss">
    ul,
    li {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    ::-webkit-scrollbar {
        width: 0px;
    }

    ::-webkit-scrollbar-thumb {
        background-color: #c7c7c7;
    }

    .chat {
        border: 1px solid #ccc;
        height: 100%;
        background-color: #fff;
        display: flex;
        flex-direction: column;
        justify-content: space-between;

		&__head {
			height: 30px;
			display: flex;
			align-items: center;
			background: #f0f0f0;
			border-bottom: 1px solid #ccc;
			padding: 10px;

			&--title {
				font-size: 16px;
				font-weight: bold;
			}
		}

        &__container {
            -webkit-box-flex: 1;
            overflow-y: scroll;
            padding-right: 8px;
            padding-left: 2px;
			&--list {
				list-style: none;
				margin: 0;
				padding: 10px 0;

				li {
					width: 100%;
					overflow: hidden;
					padding-top: 8px;
				}

				.news {
					max-width: 55%;
					width: auto;
					height: auto;
					background: #2683f5;
					padding: 6px 10px;
					margin-top: 15px;
					line-height: 20px;
					font-size: 14px;
					border-radius: 4px;
					position: relative;
					float: right;
					color: #fff;
					text-align: left;
				}

				.answers {
					max-width: 55%;
					width: auto;
					height: auto;
					background: #eee;
					padding: 5px 10px;
					margin-top: 15px;
					line-height: 22px;
					font-size: 14px;
					border-radius: 5px;
					margin-left: 10px;
					position: relative;
					float: left;
					text-align: left;
				}
			}
        }

        &__footer {
            width: 100%;
            height: 83px;
            border-top: 1px solid #ccc;
            position: relative;

			&--text {
				width: 99%;
				height: 75px;
				border: none;
				outline: none;
				resize: none;
				font-size: 16px;
			}

            &--send {
                width: 70px;
                height: 25px;
                background: #0188fb;
                border: 0;
                position: absolute;
                bottom: 10px;
                right: 10px;
                color: #fff;
                cursor: pointer;
                font-size: 12px;
                line-height: 25px;
                text-align: center;
                &:disabled{
                    background: #6dbcff;
                    cursor: no-drop;
                }
            }
        }
    }
</style>
