<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='对话记录' bgColor="transparent"></cus-header>
		<div class="list" v-if="list.length">
			<div class="l_item" v-for="(item,index) in list" :key="index">
				<div class="time">{{item.date}}</div>
				<div v-for="(pre,idx) in item.items" :key="idx">
					<div class="pre adf my" v-if="pre.chatType===1">
						<div class="text my">{{pre.content}}</div>
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/6edb131a-66f9-4a2a-a865-7b74e3dd52ed.png"></image>
					</div>
					<div class="pre adf ai" v-else-if="pre.chatType===2">
						<image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/bcde7118-fc4e-4fa6-96ca-ebe1e0beba2f.png"></image>
						<div class="text ai">{{pre.content}}</div>
					</div>
				</div>
			</div>
		</div>
		<template v-else>
			<page-empty :height="'calc(100vh - 200rpx)'"></page-empty>
		</template>
	</view>
</template>

<script>
	import pageEmpty from '@/components/pageEmpty/index.vue' 
	export default {
		components:{pageEmpty},
		data(){
			return {
				agentId:'',
				deviceId:'',
				sessionId:'',
				list:[]
			}
		},
		onLoad(option) {
			this.agentId = option?.agentId;
			this.deviceId = option?.deviceId;
			this.sessionId = option?.sessionId;
			this.getList();
		},
		methods:{
			getList(){
				this.$api.get(`/agent/${this.agentId}/${this.deviceId}/chat-history/${this.sessionId}`).then(res=>{
					if(res.data.code!==0) return this.$showToast(res.data.msg)
					this.list = this.groupedMessages(res.data.data||[])
				})
			},
			groupedMessages(data) {
			    const groups = {};
			    data.forEach(msg => {
			        const dateKey = msg.createdAt; 
			        if (!groups[dateKey]) {
			          groups[dateKey] = {
			            date: dateKey,
			            items: []
			          }
			        }
			        groups[dateKey].items.push(msg);
			    });
			
			    return Object.values(groups).sort((a, b) => 
			        new Date(a.date) - new Date(b.date)
			    );
			}
		}
	}
</script>

<style scoped lang="less">
	.page{
		background: #F7F6F9;
		padding: 0 30rpx 40rpx;
		box-sizing: border-box;
		
		.list{
			.l_item{
				margin-top: 48rpx;
				.time{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #8D8D8D;
					line-height: 24rpx;
					text-align: center;
				}
				.pre{
					margin-top: 32rpx;
					image{
						width: 78rpx;
						height: 78rpx;
						border-radius: 50%;
					}
					.text{
						max-width: calc(100% - 98rpx);
						padding: 24rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						line-height: 48rpx;
						border-radius: 20rpx;
						box-sizing: border-box;
					}
					&.my{
						justify-content: flex-end;
						.text{
							background: #72832B;
							color: #FFFFFF;
						}
						image{
							margin-left: 20rpx;
						}
					}
					&.ai{
						.text{
							background: #FFFFFF;
							color: #252525;
						}
						image{
							margin-right: 20rpx;
						}
					}
				}
			}
		}
	}
</style>