<template>
	<view class="dongtai">
		<view class="dongtai-hd">
			<view class="item">
				<view class="a">
					<view class="fl">
						<image :src="info.faceImage?info.faceImage:info.img"></image>
						<view class="info">
							<view class="name">{{info.nickname}}</view>
							<view class="desc">{{info.createTime}}</view>
						</view>
					</view>
					<view class="fr" v-if="info.userid == userInfo.id" @click="openPage(1)">
						<image class="del" src="@/static/icon-del.png"></image>
					</view>
					<view class="fr" v-else @click="openPage(2)">
						<image class="jubao" src="@/static/shenglue.png"></image>
					</view>
				</view>
				<view class="b">{{info.content}}</view>
				<view class="c">
					<image mode="aspectFill" v-for="(a,i) in info.imgurl" :key="i" @click="previewImage(i)" :src="a"></image>
				</view>
				<view class="d">
					<view class="fl">
						<view class="li">
							<image src="@/static/xiaoxi.png"></image>{{info.pinglun}}
						</view>
						<view class="li" @click="openPage(5)">
							<image v-if="info.isUserZan == 0" src="@/static/dianzan.png"></image>
							<image v-else src="@/static/zandin2.png"></image>{{info.zan}}
						</view>
						<view class="li" @click="openPage(3)">
							<image src="@/static/fenxiang.png"></image>分享
						</view>
					</view>
					<view class="fr" @click="openPage(4)" v-if="info.userid != userInfo.id">搭讪</view>
				</view>
			</view>
		</view>
		<view class="dongtai-bd" v-if="info.comments && info.comments.length> 0">
			<view class="title">全部评价（{{info.comments.length}}）</view>
			<view class="list">
				<view class="item" v-for="(a,i) in info.comments" :key="i" >
					<view class="a">
						<view class="pic">
							<image :src="a.faceImage"></image>
						</view>
						<view class="info">
							<view class="content">
								<view class="fl">
									<view class="name">{{a.nickname}}<text v-if="a.userid == info.userid">作者</text></view>
									<view class="desc">{{a.content}}</view>
								</view>
								<view class="zan">
									<view class="li" @click="openPage(9,a)">
										<image v-if="a.isUserZan == 0" src="@/static/dianzan.png"></image>
										<image v-else src="@/static/zandin2.png"></image>{{a.zan}}
									</view>
								</view>
							</view>
							<view class="hf">{{a.createTime}}<text @click="openPage(7,a)">回复</text></view>
							<view class="b" v-if="a.comments && a.comments.length > 0">
								<view class="cd" :style="{'height': a.isShow?'260rpx':'auto', 'overflow': 'hidden'}">
									<view class="li" v-for="(b,idx) in a.comments" :key="idx">
										<view class="a">
											<view class="pic">
												<image :src="b.faceImage?b.faceImage:b.img"></image>
											</view>
											<view class="info">
												<view class="content">
													<view class="fl">
														<view class="name">{{b.nickname}}<text v-if="b.userid == info.userid">作者</text></view>
														<view class="desc">回复<text>{{b.replyNickname}}：</text>{{b.content}}</view>
													</view>
													<view class="zan">
														<view class="li" @click="openPage(9,b)">
															<image v-if="b.isUserZan == 0" src="@/static/dianzan.png"></image>
															<image v-else src="@/static/zandin2.png"></image>{{b.zan}}
														</view>
													</view>
												</view>
												<view class="hf">{{b.createTime}}<text @click="openPage(7,a)">回复</text></view>
											</view>
										</view>
									</view>
								</view>
								<view class="more" @click="openPage(8,i)" v-if="a.comments.length >= 3">展开{{a.comments.length-2}}条回复</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="dongtai-fd" v-if="type=='app'">
			<view class="a">
				<image src="@/static/editor.png"></image>
				<view v-if="author" class="aite">@{{author}}</view>
				<input v-model="content" type="text" placeholder="留下你精彩的回复" />
			</view>
			<view class="b">
				<view class="fa" @click="openPage(6)">
					发送
				</view>
				<view class="li" @click="openPage(5)">
					<image v-if="info.isUserZan == 0" src="@/static/dianzan.png"></image>
					<image v-else src="@/static/zandin2.png"></image>
					{{info.zan}}
				</view>
			</view>
		</view>
		<dashan :isDashan="isDashan" :info="info"></dashan>
		<share :isShare="isShare" :info="info" />
		<unverified :isPopup="isPopup" />
		<insufficient :isCoins="isCoins" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isPopup: false,
				isDashan: false,
				isShare: false,
				isCoins: false,
				type: 'app',
				id: "",
				info: {},
				content: "",
				userInfo: {},
				replyId: -1,
				author: "",
				obj: {}
			};
		},
		onLoad(o) {
			const that = this;
			that.id = o.id;
			that.type = o.type?o.type:'app';
			that.userInfo = uni.getStorageSync("userInfo")
			that.getInfo();
			uni.$on("closeSharePopup",res=>{
				that.isShare = false;
				that.isPopup = false;
				that.isDashan=  false;
				that.isCoins=  false;
			})
			
			uni.$on("openCoinsPopup",res=>{
				that.isCoins = res
			})
		},
		methods: {
			openPage(index,obj){
				const that = this;
				if(this.type == 'web'){
					return ;
				}
				if(that.userInfo.realnameFlag == 0){
					that.isPopup = true
					return ;
				}
				if(index == 1){
					uni.showModal({
						title: "温馨提示",
						content: "确定删除该动态？",
						confirmColor: "#000000",
						success(res){
							if(res.confirm){
								that.toDel();
							}
						}
					})
				}else if(index == 2){
					uni.navigateTo({
						url: "/pages/users/feedback/index?type=1&id="+this.id+'&uid='+this.info.userid
					})
				}else if(index == 3){
					this.isShare = true
				}else if(index == 4){
					this.isDashan = true
				}else if(index == 5){
					this.toDianzan(1);
				}else if(index == 6){
					if(!this.content){
						uni.showToast({
							title: "评论内容不能为空",
							position: "bottom",
							icon: "none",
						})
						return ;
					}
					this.toPinglun()
				}else if(index == 7){
					this.replyId = obj.id;
					this.author = obj.nickname
				}else if(index == 8){
					this.info.comments[obj].isShow = false;
					this.$forceUpdate();
				}else if(index == 9){
					this.obj = obj
					this.toDianzan(-1);
				}
			},
			previewImage(i){
				uni.previewImage({
					urls: this.info.imgurl,
					count: i
				})
			},
			//点赞 取消点赞
			async toDianzan(type) {
				const { code, data, msg } = await this.$api.zan({
					articleid: type == 1?this.id:this.obj.id, 
					userId: uni.getStorageSync("userInfo").id,
					type,
					status: type == 1?(this.info.isUserZan == 0?1:2):(this.obj.isUserZan == 0?1:2),
				});
				if(code == 200){
					uni.showToast({
						title: type == 1?(this.info.isUserZan == 0?"点赞成功":"取消成功"):(this.obj.isUserZan == 0?"点赞成功":"取消成功")
					})
					this.obj = {}
					setTimeout(()=>{
						this.getInfo();
					},2000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})	
				}
			},
			//评论回复
			async toPinglun() {
				const { code, data, msg } = await this.$api.commentReply({
					articleid: this.id, 
					userId: uni.getStorageSync("userInfo").id,
					content: this.content,
					replyId: this.replyId,
				});
				if(code == 200){
					uni.showToast({
						title: this.replyId==-1?"评论成功":"回复成功"
					})
					this.author = ""
					this.replyId = -1
					this.content =""
					setTimeout(()=>{
						this.getInfo();
					},2000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})	
				}
			},
			//删除
			async toDel() {
				const { code, data, msg } = await this.$api.commentDelete({articleid: this.id, userId: uni.getStorageSync("userInfo").id});
				if(code == 200){
					uni.showToast({
						title: "删除成功"
					})
					setTimeout(()=>{
						uni.navigateBack({
							delta: 1
						})
					},1000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})	
				}
			},
			//获取详情
			async getInfo() {
				const {code, data} = await this.$api.articleDetail({articleid: this.id, userId: uni.getStorageSync("userInfo").id});
				if(code == 200){
					this.info = data
					this.info.href = this.$articleShareUrl+data.id;
					this.info.title = data.nickname;
					this.info.imageUrl = data.faceImage;
					this.info.summary = data.content
					if(data.imgurl){
						this.info.imgurl = data.imgurl.split(",")
						console.log(this.info.imgurl)
					}
					if(this.info.comments && this.info.comments.length>0){
						this.info.comments.map(a=>{
							if(a.comments && a.comments.length >=2){
								a.isShow = true
							}else{
								a.isShow = false
							}
						})
					}
				}
			},
			//评论
			async toCommentReply(replyId) {
				const {code, data, msg} = await this.$api.commentReply({articleid: this.id, userId: uni.getStorageSync("userInfo").id,content: this.content,replyId});
				if(code == 200){
					uni.showToast({
						title: "发送成功"
					})
					setTimeout(()=>{
						this.getInfo()
					},2000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})	
				}
			},
			//点按
			async toZan(type,status) {
				const {code, data} = await this.$api.zan({articleid: this.id, userId: uni.getStorageSync("userInfo").id,type, status});
				if(code == 200){
					uni.showToast({
						title: "点赞成功"
					})
					setTimeout(()=>{
						this.getInfo()
					},2000)
				}else{
					uni.showToast({
						title: msg,
						icon: "none",
						position: "bottom"
					})	
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.dongtai{
	padding-bottom: 120rpx;
	overflow: hidden;
	&-hd{
		padding: 0 25rpx 25rpx;
		overflow: hidden;
		.item{
			width: 100%;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			margin-top: 20rpx;
			.d{
				font-weight: 400;
				font-size: 24rpx;
				color: #333333;
				line-height: 42rpx;
				margin-top: 20rpx;
				text-align: left;
				display: flex;
				align-items: center;
				justify-content: space-between;
				.fl{
					flex: 1;
					display: flex;
					align-items: center;
					.li{
						display: flex;
						align-items: center;
						width: 120rpx;
						image{
							width: 32rpx;
							height: 32rpx;
							margin-right: 10rpx;
						}
					}
				}
				.fr{
					width: 90rpx;
					line-height: 44rpx;
					background: #000000;
					border-radius: 44rpx;
					text-align: center;
					font-weight: 500;
					font-size: 24rpx;
					color: #FFFFFF;
				}
			}
			.c{
				width: 110%;
				margin-top: 20rpx;
				overflow: hidden;
				image{
					width: 210rpx;
					height: 210rpx;
					border-radius: 10rpx;
					margin-right: 10rpx;
				}
			}
			.b{
				font-weight: 400;
				font-size: 28rpx;
				color: #222222;
				line-height: 42rpx;
				margin-top: 20rpx;
				text-align: left;
			    display: -webkit-box;
			    -webkit-box-orient: vertical;
			    -webkit-line-clamp: 2; /* 定义文本的行数 */
			    overflow: hidden;
			    text-overflow: ellipsis;
			}
			.a{
				display: flex;
				align-items: center;
				justify-content: space-between;
				.fl{
					flex: 1;
					display: flex;
					align-items: center;
					margin-right: 20rpx;
					image{
						width: 80rpx;
						height: 80rpx;
						background: #E6E9ED;
						border-radius: 50%;
						margin-right: 20rpx;
					}
					.info{
						flex: 1;
					}
					.name{
						font-weight: 400;
						font-size: 30rpx;
						color: #222222;
					}
					.desc{
						font-weight: 400;
						font-size: 22rpx;
						color: #B6BCC2;
						margin-top: 10rpx;
					}
				}
				.fr{
					width: 32rpx;
					.del{
						width: 42rpx;
						height: 42rpx;
					}
					.jubao{
						width: 32rpx;
						height: 32rpx;
					}
				}
			}
		}
	}
	&-bd{
		padding: 0 25rpx;
		overflow: hidden;
		margin-top: 30rpx;
		.title{
			font-weight: 500;
			font-size: 30rpx;
			color: #222222;
		}
		.list{
			margin-top: 20rpx;
			background-color: #FFFFFF;
			padding: 0 20rpx;
			overflow: hidden;
			border-radius: 20rpx;
			.item{
				padding: 30rpx 0;
				overflow: hidden;
				.more{
					margin-top: 20rpx;
					font-weight: 500;
					font-size: 24rpx;
					color: #3382EE;
					margin-left: 50rpx;
				}
				.hf{
					margin-top: 10rpx;
					font-weight: 500;
					font-size: 26rpx;
					color: #999999;
					text{
						margin-left: 20rpx;
						color: #000000;
					}
				}
				.a{
					width: 100%;
					display: flex;
					align-items: flex-start;
					.pic{
						width: 70rpx;
						height: 70rpx;
						margin-right: 20rpx;
						image{
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
					}
					.info{
						flex: 1;
						.b .li{
							margin-top: 20rpx;
							.pic{
								width: 40rpx;
								height: 40rpx;
								margin-right: 12rpx;
								image{
									width: 100%;
									height: 100%;
									border-radius: 50%;
								}
							}
						}
						.content{
							width: 100%;
							display: flex;
							align-items: flex-start;
							justify-content: space-between;
							.fl{
								flex: 1;
								max-width: 500rpx;
								.name{
									font-weight: 500;
									font-size: 28rpx;
									color: #222222;
									display: flex;
									align-items: center;
									text{
										width: 58rpx;
										height: 32rpx;
										background: #000000;
										border-radius: 5rpx;
										text-align: center;
										line-height: 32rpx;
										font-size: 22rpx;
										color: #FFFFFF;
										display: block;
										margin-left: 10rpx;
									}
								}
								.desc{
									text-align: left;
									font-weight: 500;
									font-size: 26rpx;
									color: #999999;
									line-height: 40rpx;
									margin-top: 10rpx;
									text{
										color: #000000;
										margin-left: 10rpx;
									}
								}
							}
							.zan{
								width: 80rpx;
								display: flex;
								align-items: center;
								justify-content: center;
								.li{
									width: 80rpx;
									text-align: center;
									font-weight: 500;
									font-size: 24rpx;
									color: #999999;
									image{
										width: 32rpx;
										height: 32rpx;
										display: block;
										margin: 0 auto;
									}
								}
							}
						}
					}
				}
				
			}
		}
	}
	&-fd{
		width: 100%;
		height: 100rpx;
		background: #FFFFFF;
		padding: 0 25rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 21;
		.a{
			width: 480rpx;
			height: 60rpx;
			background: #F3F4F5;
			border-radius: 60rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			image{
				width: 22rpx;
				height: 22rpx;
				margin-right: 10rpx;
			}
			.aite{
				font-size: 24rpx;
				color: #ff0000;
				margin-right: 10rpx;
			}
			input{
				flex: 1;
				height: 60rpx;
				line-height: 60rpx;
				font-weight: 500;
				font-size: 24rpx;
				color: #333333;
			}
		}
		.b{
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			.fa{
				font-size: 30rpx;
				color: #000000;
				width: 80rpx;
				text-align: left;
				margin-right: 30rpx;
			}
			.li{
				display: flex;
				align-items: center;
				width: 120rpx;
				image{
					width: 32rpx;
					height: 32rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
}
</style>
