<template>
	<view class="detail-page-container topic-detail-page">
		<view class="topic-box">
			<view class="topic-container">
				<view class="topic-title-num">
					<view class="title">{{topicName}}</view>
					<view class="num">话题动态数 {{list.length}}</view>
				</view>
				<image src="/static/images/topic.png" class="topic-img"></image>
			</view>
		</view>
		<scroll-view class="scroll-view-topic" scroll-y :show-scrollbar="false" v-if="list.length>0"
			@scrolltolower="loadMore">
			<template v-for="(item,index) in list">
				<dynamic-item :form="item" @refresh="handleRefresh" @deleteSuccess="handleRefresh" @refreshCurrent="refreshCurrent"></dynamic-item>
			</template>
		</scroll-view>
		<scroll-view class="scroll-view-topic" scroll-y :show-scrollbar="false" v-else>
			<list-empty></list-empty>
		</scroll-view>
		<view class="publisb-box">
			<view class="publish-btn" @click="handleToPublish">
				<image src="/static/images/publish-blue.png" class="icon"></image>
				<view class="text">发表动态</view>
			</view>
		</view>
	</view>
</template>

<script>
	import DynamicItem from '@/components/Dynamic-Item.vue';
	import ListEmpty from '@/components/ListEmpty.vue';
	import {
		topicDynamics,
		dynamicDetail
	} from '@/api/api.js';
	export default {
		data() {
			return {
				list: [],
				topicName: '',
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					topicId: ''
				},
				total: 0
			}
		},
		components: {
			DynamicItem,
			ListEmpty
		},
		onLoad(options) {
			let {
				topicId,
				topicName
			} = options;
			this.topicName = topicName
			if (topicId) {
				this.queryParams.topicId = topicId
				this.getDetail()
			}
		},
		onPullDownRefresh() {
			this.refreshContentList()
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		methods: {
			getDetail() {
				uni.showLoading({
					title: '加载中...'
				})
				topicDynamics(this.queryParams).then(res => {
					this.list = [...this.list, ...res.rows];
					this.total = res.total;
				}).finally(() => {
					uni.hideLoading()
				})
			},
			handleToPublish() {
				uni.navigateTo({
					url: `/pages/publish/publish?topicId=${this.queryParams.topicId}&topicName=${this.topicName}`
				})
			},
			handleRefresh(){
				this.list = [];
				this.queryParams.pageNum = 1;
				this.getDetail();
			},
			// 下拉刷新
			refreshContentList() {
				this.list = [];
				this.queryParams.pageNum = 1;
				this.getDetail();
			},
			loadMore() {
				if (this.list.length < this.total) {
					this.queryParams.pageNum += 1;
					this.getDetail()
				}
			},
			// 指定更新
			refreshCurrent(momentId) {
				dynamicDetail(momentId).then(res => {
					if (res.code == 200) {
						const targetIndex = this.list.findIndex(item => item.id === momentId);
						if (targetIndex === -1) return; // 未找到则退出
						// 使用 Vue.set 或展开运算符确保响应式
						this.list.splice(targetIndex, 1, {
							...res.data
						});
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/styles/common.scss';

	.topic-detail-page {
		background-color: white;
		overflow: hidden;

		.topic-box {
			padding: 32rpx 34rpx 2rpx 32rpx;

			.topic-container {
				width: 92vw;
				height: 180rpx;
				border-radius: 12rpx;
				background: linear-gradient(89.31deg, rgba(230, 246, 254, 1) 1.2%, rgba(186, 239, 255, 1) 99.53%);
				text-align: center;
				padding: 0 30rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.topic-title-num {
					width: calc(100% - 180rpx);
					text-align: left;

					.title {
						color: rgba(16, 16, 16, 1);
						font-size: 36rpx;
					}

					.num {
						color: rgba(162, 170, 184, 1);
						font-size: 24rpx;
						margin-top: 12rpx;
					}
				}

				.topic-img {
					width: 160rpx;
					height: 160rpx;
				}
			}
		}

		.scroll-view-topic {
			@include scroll-view-container(calc(100vh - 340rpx))
		}

		.publisb-box {
			height: 124rpx;
			background-color: rgba(244, 244, 244, 1);

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

			.publish-btn {
				width: 240rpx;
				height: 80rpx;
				border-radius: 12rpx;
				background-color: rgba(255, 255, 255, 1);
				text-align: center;
				border: 1px solid rgba(230, 230, 230, 1);
				display: flex;
				align-items: center;
				justify-content: center;

				.icon {
					width: 36rpx;
					height: 36rpx;
					margin-right: 16rpx;
				}

				.text {
					color: rgba(0, 0, 0, 1);
					font-size: 32rpx;
				}
			}
		}
	}
</style>