<template>
	<view class="msg-list">
		<view v-for="(msg, index) in messages" :key="index"
			:class="{ sent: msg.type === 'sent', received: msg.type === 'received' }">
			<view class="msg">
				{{ msg.content }}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'MsgList',
		props: {
			messages: {
				type: Array,
				default: () => [],
			},
		},
		methods: {
			// 新增事件处理函数，用于通知父组件滚动到底部
			scrollToBottom() {
				this.$emit('scroll-bottom');
			},
		},
		mounted() {
			this.$el.addEventListener('scroll', () => {
				if (this.$el.scrollTop + this.$el.clientHeight >= this.$el.scrollHeight) {
					this.scrollToBottom();
				}
			});
		},
	};
</script>

<style scoped>
	.msg-list {
		display: flex;
		flex-direction: column;
		gap: 10px;
	}

	.sent {
		align-self: flex-end;
		background-color: #DCF8C5;
		border-radius: 20px;
		padding: 10px;
	}

	.received {
		align-self: flex-start;
		background-color: #E5E7EB;
		border-radius: 20px;
		padding: 10px;
	}

	.msg {
		white-space: pre-wrap;
		word-wrap: break-word;
	}
</style>
