<template>
	<view class="content" v-if="is_show == 1">
		<view class="post-content">
			<view class="post-header">
				<image :src="post.author_avatar" mode="aspectFill" class="avatar"></image>
				<view class="user-info">
					<text class="username">{{post.author_name}}</text>
					<text class="time">{{post.create_time}}</text>
				</view>
				<!--  -->
				<button class="share-button" open-type="share">
					<image class="share" src="/static/icon/share.png" mode=""></image>
				</button>

			</view>

			<text class="content">{{post.content}}</text>

			<view class="image-list">
				<image :src="img" mode="aspectFill" class="image" v-for="(img, index) in post.images" :key="index"
					@click="previewImage(post.images, index)"></image>
			</view>
		</view>

		<view class="comment-section">
			<view class="comment-header">
				<text class="comment-title">最新评论({{comments.length}})</text>
			</view>
			<view class="comment-list">
				<view class="comment-item" v-for="(comment, index) in comments" :key="index">
					<image :src="comment.author_avatar|| 'https://api.myjie.cn/static/img/logo.png'" mode="aspectFill" class="comment-avatar"></image>
					<view class="comment-content">
						<view class="comment-header">
							<text class="comment-username">{{comment.author_name || '微信用户'}}</text>
							<view class="delete-btn" @click="deleteComment(comment.id)"
								v-if="comment.uid == userInfo.uid">
								<text>删除</text>
							</view>
						</view>
						<view class="comment-text">{{comment.content}}</view>
						<view class="comment-time">{{comment.create_time}}</view>
					</view>
				</view>
			</view>
		</view>
		<view class="comment-input">
			<input placeholder="发表评论" v-model="commentText" class="input" :maxlength="-1"/>
			<view class="submit-btn" @click="submitComment">
				<text>发表评论</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex"
	import {
		getPostDetail,
		addComment,
		deleteComment
	} from '@/api/index.js'
	import {
		share
	} from '@/api/wx.js'
	export default {
		data() {
			return {
				is_show: 0,
				id: 0,
				post: {

				},
				comments: [],
				commentText: ''
			};
		},
		computed: {
			...mapState('user', ['userInfo', 'hasLogin']),
		},
		// #ifdef MP
		onShareAppMessage(options) {

			let shareObj = {
				title: this.post.title, // 默认是小程序的名称(可以写slogan等)
				path: '/pages/user/forum/detail?id=' + this.id, // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: this.post.images[0],
				desc: this.post.content,
				success: function(res) {
					// 转发成功之后的回调
					if (res.errMsg == 'shareAppMessage:ok') {}
				},
				fail: function(res) {
					// 转发失败之后的回调
					if (res.errMsg == 'shareAppMessage:fail cancel') {
						// 用户取消转发
					} else if (res.errMsg == 'shareAppMessage:fail') {
						// 转发失败，其中 detail message 为详细失败信息
					}
				}
			};
			// 判断是否可以邀请
			if (this.userInfo.uid) {
				shareObj.path += '&spread=' + this.userInfo.uid;
			}
			return shareObj;
		},
		// #endif
		methods: {
			getShow() {
				share({}).then(({
					data
				}) => {
					this.is_show = data.is_show
				});
			},
			deleteComment(id) {
				deleteComment({
					id: id,
					uid: this.userInfo.uid
				}).then(res => {
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});
					this.getDetail();
				})
			},
			// 返回
			navBack() {
				uni.navigateBack();
			},

			// 预览图片
			previewImage(images, index) {
				uni.previewImage({
					urls: images,
					current: index,
					indicator: 'number',
					success: function(res) {
						console.log('预览图片成功');
					},
					fail: function(res) {
						console.log('预览图片失败', res);
					}
				});
			},
			// 提交评论
			submitComment() {
				if (!this.commentText.trim()) {
					uni.showToast({
						title: '请输入评论内容',
						icon: 'none'
					});
					return;
				}
				addComment({
					post_id: this.id,
					content: this.commentText
				}).then(res => {
					uni.showToast({
						title: '评论成功',
						icon: 'success'
					});
					this.getDetail()
					this.commentText = '';
				})
			},
			// 删除评论
			deleteComment(id) {
				uni.showModal({
					title: '提示',
					content: '确定要删除这条评论吗？',
					success: (res) => {
						if (res.confirm) {
							deleteComment({
								id: id,
								uid: this.userInfo.uid
							}).then(res => {
								uni.showToast({
									title: '删除成功',
									icon: 'success'
								});
								this.getDetail();
							})
						}
					}
				});
			},
			getDetail() {
				if(!this.id) return;
				getPostDetail({
					id: this.id
				}).then(res => {
					this.post = res.data
					if (this.post.images && typeof(this.post.images) == 'string') {
						this.post.images = this.post.images.split(',')
					}
					this.comments = res.data.comments.list
				})
			}
		},
		onLoad(opt) {
			this.getShow()
			this.id = opt.id
			this.getDetail()
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #F5F5F5;
	}

	.content {
		min-height: 100%;
		padding-bottom: 100rpx;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		height: 80rpx;
		background-color: #FFFFFF;

		.back {
			width: 40rpx;
			height: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}

		.share {
			width: 40rpx;
			height: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}

	.post-content {
		background-color: #FFFFFF;
		padding: 20rpx;
		margin-bottom: 10rpx;

		.post-header {
			display: flex;
			align-items: center;
			margin-bottom: 15rpx;

			.avatar {
				width: 60rpx;
				height: 60rpx;
				border-radius: 50%;
				margin-right: 15rpx;
			}

			.share {
				width: 30rpx;
				height: 30rpx;
			}

			.user-info {
				flex: 1;

				.username {
					font-size: 28rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 5rpx;
					display: block;
				}

				.time {
					font-size: 22rpx;
					color: #999999;
				}
			}
		}

		.content {
			font-size: 26rpx;
			color: #333333;
			line-height: 36rpx;
			margin-bottom: 20rpx;
		}

		.image-list {
			display: flex;
			flex-wrap: wrap;

			.image {
				width: 31%;
				height: 200rpx;
				margin: 1%;
				border-radius: 10rpx;
			}
		}
	}

	.comment-section {
		background-color: #FFFFFF;
		padding: 20rpx;

		.comment-header {
			margin-bottom: 20rpx;

			.comment-title {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
			}
		}

		.comment-list {

			.comment-item {
				display: flex;
				margin-bottom: 20rpx;
				border-bottom: #EEEEEE 1rpx solid;
				padding: 20rpx;

				.comment-avatar {
					width: 50rpx;
					height: 50rpx;
					border-radius: 50%;
					margin-right: 15rpx;
				}

				.comment-content {
					flex: 1;

					.comment-header {
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 10rpx;

						.comment-username {
							font-size: 24rpx;
							// color: #333333;
							color: #999999;
						}

						.delete-btn {
							font-size: 22rpx;
							color: #f65486;
						}


					}

					.comment-time {
						font-size: 20rpx;
						color: #999999;
					}

					.comment-text {
						font-weight: bold;
						font-size: 24rpx;
						color: #333333;
						line-height: 32rpx;
						padding: 20rpx 0;
					}
				}
			}
		}
	}

	.comment-input {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		height: 80rpx;
		background-color: #FFFFFF;
		border-top: 1rpx solid #EEEEEE;
		display: flex;
		align-items: center;
		padding: 0 20rpx;

		.input {
			flex: 1;
			height: 50rpx;
			padding: 0 20rpx;
			border: 1rpx solid #EEEEEE;
			border-radius: 25rpx;
			font-size: 24rpx;
			color: #333333;
			margin-right: 15rpx;
			resize: none;
		}

		.submit-btn {
			padding: 0 30rpx;
			height: 50rpx;
			background-color: #f65486;
			border-radius: 25rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			text {
				font-size: 24rpx;
				font-weight: bold;
				color: #FFFFFF;
			}
		}
	}

	.share-button {
		background: none;
		/* 清除背景 */
		border: none;
		/* 清除边框 */
		padding: 0;
		/* 清除内边距 */
		margin: 0;
		/* 清除外边距 */
		line-height: normal;
		/* 重置行高 */
		display: flex;
		/* 保持 flex 布局（如需） */
		align-items: center;
		/* 垂直居中 */
		justify-content: center;
		/* 水平居中 */
	}

	/* 清除点击效果（伪元素） */
	.share-button::after {
		display: none;
		/* 或 border: none; box-shadow: none; */
	}
</style>