<template>
	<view class="content">
		<view class="post-list" v-if="is_show == 1">
			<view class="post-item" v-for="(post, index) in postList" :key="index">
				<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>
				</view>

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

				<view class="product-images">
					<image :src="img" mode="aspectFill" class="product-image" v-for="(img, imgIndex) in post.images"
						:key="imgIndex" @click="previewImage(post.images, imgIndex)"></image>
				</view>
				<view class="post-footer">
					<view class="comment-share">
						<view class="action" @click="comment(post)">
							<image src="../../../static/icon/pl.png" mode="widthFix" class="action-icon"></image>
							<text class="action-text">评论({{post.comment_count}})</text>
						</view>
						<view class="action" @click="share(post)">
						<button class="share-button" open-type="share">
							
								<image src="../../../static/icon/share.png" mode="widthFix" class="action-icon"></image>
								<text class="action-text">分享</text>
						</button>
						</view>
					</view>
				</view>
			</view>
		</view>
		<empty v-else></empty>
		<view class="publish-btn" @click="publish" v-if="userInfo.permission == 1 && is_show == 1">
			<image src="../../../static/icon/fabu.png" mode="widthFix" class="publish-icon"></image>
			<text class="publish-text">我要发布</text>
		</view>
	</view>
</template>

<script>
	import empty from '@/components/empty/empty.vue'
	import {
		mapState
	} from "vuex"
	import {
		getPostList
	} from '@/api/index.js'
	import {share} from '@/api/wx.js'
	export default {
		components: {
			empty
		},
		data() {
			return {
				is_show: 0,
				page: 1,
				pageSize: 10,
				loading: 'more',
				postList: [],
				sharePost: {}
			};
		},
		computed: {
			...mapState('user', ['userInfo', 'hasLogin']),
		},
		// #ifdef MP
		onShareAppMessage() {
			let shareObj = {
				title: this.sharePost.title, // 默认是小程序的名称(可以写slogan等)
				path: '/pages/user/forum/detail?id=' + this.sharePost.id, // 默认是当前页面，必须是以‘/’开头的完整路径
				imageUrl: this.sharePost.images[0],
				desc: this.sharePost.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
					if(data.is_show == 1) {
						uni.setNavigationBarTitle({
							title:'论坛'
						})
					}
				});
			},
			// 返回
			navBack() {
				uni.navigateBack();
			},
			// 评论
			comment(post) {
				uni.navigateTo({
					url:'/pages/user/forum/detail?id=' + post.id
				})
			},
			// 分享
			share(item) {
				this.sharePost = item
			},
			// 发布
			publish() {
				uni.navigateTo({
					url: '/pages/user/forum/edit'
				})
			},
			// 预览图片
			previewImage(images, index) {
				uni.previewImage({
					urls: images,
					current: index,
					indicator: 'number',
					success: function(res) {
						console.log('预览图片成功');
					},
					fail: function(res) {
						console.log('预览图片失败', res);
					}
				});
			},
			getPostList() {
				let that = this
				if (that.loading == 'loading' || that.loading == 'noMore') return;
				that.loading = 'loading'
				getPostList({
					page: that.page,
					pageSize: that.pageSize
				}).then(res => {
					let arr = res.data.list.map(item => {
						if(item.images && typeof(item.images) == 'string') {
							item.images = item.images.split(',')
						}
						return item
					})
					that.postList = that.postList.concat(arr)
					that.page++
					if (arr.length < that.pageSize) {
						that.loading = 'noMore'
					} else {
						that.loading = 'more'
					}
				})
			}
		},
		onReachBottom() {
			this.getPostList()
		},
		onLoad() {
			this.getPostList()
			this.getShow()
		},
		// 获取贴子列表

	};
</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;
		}

		.right {
			width: 40rpx;
		}
	}

	.post-list {
		margin-top: 10rpx;

		.post-item {
			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;
				}

				.user-info {
					flex: 1;

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

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

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

			.product-images {
				display: flex;
				flex-wrap: wrap;
				margin-bottom: 20rpx;

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

			.post-footer {
				border-top: 1rpx solid #EEEEEE;
				padding-top: 15rpx;

				.comment-share {
					display: flex;
					align-items: center;

					.action {
						display: flex;
						flex: 1;
						align-items: center;
						margin-right: 40rpx;
						text-align: center;
						display: flex;
						justify-content: center;

						.action-icon {
							width: 24rpx;
							height: 24rpx;
							margin-right: 8rpx;
						}

						.action-text {
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
			}
		}
	}

	.publish-btn {
		width: 185rpx;
		height: 64rpx;
		position: fixed;
		bottom: 160rpx;
		right: 0rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #f65486;
		border-radius: 32rpx 0 0 32rpx;

		.publish-icon {
			width: 40rpx;
			height: 40rpx;
			margin-bottom: 10rpx;
		}

		.publish-text {
			font-size: 24rpx;
			color: #FFFFFF;
			font-weight: bold;
		}
	}
	.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>