<template>
	<view class="wrapper">

		<view style="padding:30rpx;">
			<u-row customStyle="margin-bottom: 10px;">
				<u-col span="12">
					<view class="title">
						<h3>{{article.title}}</h3>
					</view>
				</u-col>
			</u-row>

			<u-row customStyle="margin-bottom: 10px">
				<u-col span="12">
					<view class="content" v-html="article.content"></view>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px" v-if="article.videoUrl">
				<u-col span="12">
					<video style="width: 100%;"
						:src="article.videoUrl.indexOf('http')!=-1?article.videoUrl:baseUrl1+article.videoUrl"
						controls></video>
				</u-col>
			</u-row>
		</view>
		<!-- 		<view id="pagebottom"></view>
		<u-divider></u-divider> -->

		<!-- 评论列表 -->
		<!-- 	<view class="comment-title">
			<text>评论</text>
		</view> -->


		<!-- 		<view class="comment-list">
			<view class="comment" v-for="(res, index) in commentList" :key="res.id">
				<view class="right">
					<view class="top">
						<view class="name">{{ res.fullName }}</view>
					</view>
					<view class="content">{{ res.content }}</view>
					<view class="bottom">
						<text>{{res.createTime.replace('T',' ').replace('.000+08:00',' ')}}</text>
						<text v-if="res.userId == user.userId" @click="userDelComment(res.id)">删除</text>
					</view>
					<u-divider></u-divider>
				</view>
			</view>
		</view> -->


		<!-- 底部按钮 -->
		<view class="bottom-button">
			<view class="button-content">
				<view class="enjoy-button" v-if="showBtn">
					<button class="button" size="mini" data-name="shareBtn" open-type="share">
						<image src="../../static/images/enjoy0.png"></image>
					</button>

				</view>
				<view v-else>
					<image class="btn-image" src="../../static/images/enjoy0.png" @click="toShare"></image>
				</view>
				<view class="like-button">
					<image src="@/static/images/vector.png" v-if="unFavorite" @click="userAddFavorite"
						style="width:40rpx;height:40rpx;"></image>
					<image src="@/static/images/vectored.png" v-else @click="userDelFavorite(favoriteId)"
						style="width:40rpx;height:40rpx;"></image>
				</view>

				<!-- 			<view class="comment-button" @click="commentClick">
					<image src="@/static/images/pinglun.png"></image>
					 <view class="box" style="position: relative; top: -5px; left: -10px;">
						<u-badge type="error" max="99" :value="commentList.length"></u-badge>
					</view> 	</view> -->

				<view class="like-button">
					<image src="@/static/images/dainzan.png" v-if="unLaud" @click="userAddLaud"
						style="width:44rpx;height:44rpx;"></image>
					<image src="@/static/images/dainzaned.png" v-else @click="userDelLaud(laudId)"
						style="width:44rpx;height:44rpx;"></image>
				</view>
			</view>
		</view>

		<!-- 评论区 -->
		<uni-popup type="bottom" ref="warrantPop" :animation="true" :maskClick="true">
			<view class="comment-popup" style="background: #fff;">
				<view class="head">
					<text @click="commentClose">取消</text>
					<text @click="userAddComment">发布</text>
				</view>
				<view class="comment-con">
					<view class="comment-input">
						<view class="con">

							<u--textarea v-model="content" placeholder="好观点会被优先展示" autoHeight count
								border="bottom"></u--textarea>

						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showBtn: this.$store.getters.token ? true : false,
				unLaud: true, //点赞前
				laudId: null,
				unFavorite: true, //收藏前
				favoriteId: null,
				articleId: '',
				inputVal: '',
				commentNull: false,
				contentId: '',
				commentList: [],
				article: {},
				user: {},
				content: '',
				baseUrl1: ""
			}
		},
		onLoad(option) {
			console.log(121, this.baseUrl);
			this.baseUrl1 = this.baseUrl;
			this.getArticleDetail(option.id)
		},
		methods: {
			toShare() {
				uni.navigateTo({
					url: '/loginPackage/pages/Login'
				})
			},
			// 文章详情
			async getArticleDetail(id) {
				const resUser = await this.$myRequest({
					url: '/getInfo'
				})
				this.user = resUser.data.user;

				const res = await this.$myRequest({
					url: '/system/article/' + id,
				})
				try {
					// res.data.data.content = this.replaceImg(res.data.data.content)
					res.data.data.content = res.data.data.content
				} catch (e) {
					res.data.data.content = res.data.data.content
				}

				this.article = res.data.data
				this.commentList = res.data.data.asdArticleCommentList
				console.log(this.commentList);


				const resFav = await this.$myRequest({
					url: '/system/favorite/list',
					data: {
						articleId: id,
						userId: resUser.data.user.userId
					}
				})

				if (resFav.data.rows.length > 0) {
					this.unFavorite = false;
					this.favoriteId = resFav.data.rows[0].id
				}

				const resLaud = await this.$myRequest({
					url: '/system/laud/list',
					data: {
						articleId: id,
						userId: resUser.data.user.userId
					}
				})

				if (resLaud.data.rows.length > 0) {
					this.unLaud = false;
					this.laudId = resLaud.data.rows[0].id
				}
			},
			// 点赞功能
			async userAddLaud() {
				if (this.$store.getters.token) {
					const res = await this.$myRequest({
						method: 'POST',
						url: '/system/laud/userAddLaud',
						data: {
							articleId: this.article.id
						}
					})
					if (res.data.code == 200) {
						this.unLaud = false
						this.getArticleDetail(this.article.id)
						uni.showToast({
							title: '点赞成功',
							icon: 'none'
						});
					}
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}

			},
			// 取消点赞功能
			async userDelLaud(laudId) {
				if (this.$store.getters.token) {
					const res = await this.$myRequest({
						method: 'DELETE',
						url: '/system/laud/userDelLaud/' + laudId,
					})
					if (res.data.code == 200) {
						this.unLaud = true
						this.getArticleDetail(this.article.id)
						uni.showToast({
							title: '点赞已取消',
							icon: 'none'
						});
					}
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}
			},
			// 收藏功能
			async userAddFavorite() {
				if (this.$store.getters.token) {
					const res = await this.$myRequest({
						method: 'POST',
						url: '/system/favorite/userAddFavorite',
						data: {
							articleId: this.article.id
						}
					})
					if (res.data.code == 200) {
						this.unFavorite = false
						this.getArticleDetail(this.article.id)
						uni.showToast({
							title: '收藏成功',
							icon: 'none'
						});
					}
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}

			},
			// 取消收藏功能
			async userDelFavorite(favoriteId) {
				if (this.$store.getters.token) {
					const res = await this.$myRequest({
						method: 'DELETE',
						url: '/system/favorite/userDelFavorite/' + favoriteId,
					})
					if (res.data.code == 200) {
						this.unFavorite = true
						this.getArticleDetail(this.article.id)
						uni.showToast({
							title: '收藏已取消',
							icon: 'none'
						});
					}
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}
			},
			// 评论按钮
			commentClick() {
				if (this.$store.getters.token) {
					this.$refs.warrantPop.open();
					this.content = '';
					this.commentNull = true;
					uni.pageScrollTo({
						duration: 300,
						selector: '#pagebottom' // string 选择器
					});
				} else {
					uni.navigateTo({
						url: '/loginPackage/pages/Login'
					})
				}
			},
			commentClose() {
				this.$refs.warrantPop.close();
			},
			async userAddComment() {
				const res = await this.$myRequest({
					method: 'POST',
					url: '/system/comment/userAddComment',
					data: {
						articleId: this.article.id,
						content: this.content
					}
				})
				if (res.data.code == 200) {
					uni.pageScrollTo({
						duration: 300,
						selector: '#pagebottom' // string 选择器
					});
					this.unFavorite = true
					this.commentClose();
					this.getArticleDetail(this.article.id)
					uni.showToast({
						title: '评论成功',
						icon: 'none'
					});

				}
			},
			async userDelComment(id) {
				uni.showModal({
					content: '将删除这条评论',
					success: (res) => {
						if (res.confirm) {
							const res = this.$myRequest({
								method: 'DELETE',
								url: '/system/comment/userDelComment/' + id,
							}).then((delres) => {
								if (delres.data.code == 200) {
									uni.pageScrollTo({
										duration: 300,
										selector: '#pagebottom' // string 选择器
									});
									this.getArticleDetail(this.article.id)
									uni.showToast({
										title: '删除成功',
										icon: 'none'
									});
								}
							})

						}
					}
				})

			},
			/**
			 * 替换图片链接 && 图片自适应
			 * @param {*} a 
			 * @returns 
			 */
			replaceImg(a) {
				// a 为富文本的字符串内容，为了测试，只写了img标签
				var b = /<img [^>]*src=['"]([^'"]+)[^>]*>/g; // img 标签取src里面内容的正则
				var s = a.match(b); // 取到所有img标签 放到数组 s里面
				if (s) {

					for (var i = 0; i < s.length; i++) {
						var srcImg = s[i].replace(b, '$1'); //取src面的内容
						console.log(346, srcImg);
						// srcImg = srcImg.slice(8)

						if (srcImg.slice(0, 4) == 'http' || srcImg.slice(0, 5) == 'https') {
							//若src前4位置或者前5位是http、https则不做任何修改
							//console.log('不做任何修改');
						} else {
							//修改富文本字符串内容 img标签src 相对路径改为绝对路径
							a = a.replace(/src=['"]([^'"]+)[^>]*>/gi, 'src="' + this.baseUrl + srcImg + '"' + '>');
							// console.log(baseUrl, a)
						}
					}
				}
				let newContent = a.replace(/<img[^>]*>/gi, function(match, capture) {
					match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
					match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
					match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
					return match;
				});
				newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
					match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi,
						'max-width:100%;');
					return match;
				});
				newContent = newContent.replace(/<br[^>]*\/>/gi, '');
				newContent = newContent.replace(/\<img/gi,
					'<img style="max-width:100%;height:auto;display:block;margin:10px 0;"');
				return newContent;

			}
		},

	}
</script>

<style lang="less" scoped>
	.wrapper {
		// background-image: url("http://43.140.199.34:81/choujiangbeijing.png");
	}

	button {
		background-color: #e2ebf7;

		image {
			width: 40rpx;
			height: 40rpx;
			margin-top: 20rpx;
		}
	}

	button::after {
		border: none;

	}

	.btn-image {
		width: 40rpx;
		height: 40rpx;
		margin-top: 10rpx;
	}

	/deep/ .u-icon__icon .uicon-share-square {
		color: #e2ebf7;
	}

	/deep/.u-button--plain {
		background-color: #e2ebf7;
	}

	/deep/ img {
		width: 95%;
	}

	img {
		width: 95%;
	}

	.title {
		display: flex;
		justify-content: center;
	}

	.content {
		// text-indent: 2em;
	}

	.head {
		display: flex;
		justify-content: space-between;
		height: 100rpx;
		padding: 20rpx;
		color: #626262;
	}

	.con {
		min-height: 100rpx;
	}

	.bottom-button {
		display: flex;
		position: fixed;
		bottom: 0rpx;
		width: 100%;
		background: #e2ebf7;
		height: 100rpx;
		align-items: center;

		.button-content {
			display: flex;
			width: 70%;
			left: 15%;
			position: fixed;
			justify-content: space-between;
			align-items: center;

			.enjoy-button {
				display: flex;
				margin-right: -30rpx;
				background-image: url(../../static/images/enjoy0.png);
			}

			.comment-button {
				display: flex;
				color: #626262;
				font-size: 24rpx;

				image {
					width: 42rpx;
					height: 42rpx;
					margin-right: 10rpx;
				}
			}

			.like-button {
				display: flex;
				color: #626262;
				font-size: 24rpx;

				image {
					// width:44rpx;
					// height:44rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
</style>

<style lang="scss" scoped>
	.comment-title {
		padding: 30rpx;
		font-weight: bold;
	}

	.comment-list {
		padding-bottom: 35rpx;
	}

	.comment {
		display: flex;
		padding: 30rpx;

		.left {
			image {
				width: 64rpx;
				height: 64rpx;
				border-radius: 50%;
				background-color: #f2f2f2;
			}
		}

		.right {
			flex: 1;
			padding-left: 20rpx;
			font-size: 30rpx;

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

				.name {
					color: #5677fc;
				}

				.like {
					display: flex;
					align-items: center;
					color: #9a9a9a;
					font-size: 26rpx;

					.num {
						margin-right: 4rpx;
						color: #9a9a9a;
					}
				}

				.highlight {
					color: #5677fc;

					.num {
						color: #5677fc;
					}
				}
			}

			.content {
				margin-bottom: 10rpx;
			}

			.reply-box {
				background-color: rgb(242, 242, 242);
				border-radius: 12rpx;

				.item {
					padding: 20rpx;
					border-bottom: solid 2rpx $u-border-color;

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

				.all-reply {
					padding: 20rpx;
					display: flex;
					color: #5677fc;
					align-items: center;

					.more {
						margin-left: 6rpx;
					}
				}
			}

			.bottom {
				margin-top: 20rpx;
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				color: #9a9a9a;

				.reply {
					color: #5677fc;
					margin-left: 10rpx;
				}
			}
		}
	}
</style>