<template>
	<view class="content">
	
		<scroll-view class="scroll" scroll-y>
			<view class="scroll-content">
				<view class="introduce-section">
					<text class="title">{{detailData.articleTitle}}</text>
					<view class="introduce">
						<text>{{detailData.hospitalName}}</text>
						<!-- <text>105阅读</text> -->
						<text>{{detailData.publishTime}}</text>
					</view>
					
					<rich-text v-if="detailData.contentLayout != 4" style="white-space:pre-wrap;word-break:break-all;" :nodes="detailData.articleContent" ></rich-text>
					<view class="video-wrapper" v-else>
						<video 
							class="video"
							:src="imgUrl+detailData.articleContent" 
							controls
							objectFit="fill"
							:autoplay="false"
						></video>
					</view>
					
					<view class="actions">
						<!-- <view class="action-item">
							<text class="yticon icon-dianzan-ash"></text>
							<text>75</text>
						</view>
						<view class="action-item">
							<text class="yticon icon-dianzan-ash reverse"></text>
							<text>6</text>
						</view>
						<view class="action-item">
							<text class="yticon icon-fenxiang2"></text>
							<text>分享</text>
						</view> -->
						<view class="action-item" @click="favorite" style="cursor: pointer;"  v-if="loading === false">
							<text :class="detailData.favorite?'yticon icon-shoucang active':'yticon icon-shoucang '"></text>
							<text>收藏</text>
						</view>
					</view>
				</view>
				
				<view class="container padding-bottom" v-if="loading === false && newsList.length>0">
					<!-- 推荐 -->
					<view class="s-header">
						<text class="tit">相关推荐</text>
					</view>
					<view class="rec-section" v-for="item in newsList" :key="item.id" @click="viewDetail(item.id)">
						<view class="rec-item">
							<view class="left">
								<text class="title">{{item.articleTitle}}</text>
								<view class="bot">
									<text class="author author2" v-if="item.articleCovers.length > 0">{{item.hospitalName}}</text>
									<text class="author" v-else>{{item.hospitalName}}</text>
									<text class="time">{{ item.publishTime ? item.publishTime.split(" ")[0]:'' }}</text>
								</view>
							</view>
							<view class="right" v-if="item.articleCovers.length > 0">
								<image class="img" mode="aspectFit" :src="imgUrl+item.articleCovers[0]"></image>
							</view>
						</view>
					</view>
					
					<!-- 评论 -->
<!-- 					<view class="s-header">
						<text class="tit">网友评论</text>
					</view>
					<view class="evalution">
						<view  v-for="(item, index) in evaList" :key="index"
							class="eva-item"
						>
							<image :src="item.src" mode="aspectFill"></image>
							<view class="eva-right">
								<text>{{item.nickname}}</text>
								<text>{{item.time}}</text>
								<view class="zan-box">
									<text>{{item.zan}}</text>
									<text class="yticon icon-shoucang"></text>
								</view>
								<text class="content">{{item.content}}</text>
							</view>
						</view>
					</view> -->
				</view>
			</view>
		</scroll-view>
		
	<!-- 	<view class="bottom">
			<view class="input-box">
				<text class="yticon icon-huifu"></text>
				<input 
					class="input"
					type="text" 
					placeholder="点评一下把.." 
					placeholder-style="color:#adb1b9;"
				/>
			</view>
			<text class="confirm-btn">提交</text>
		</view> -->
	</view>
</template>

<script>
	import json from '@/json';
	import mixLoading from '@/components/mix-loading/mix-loading';
	export default {
		components: {
			mixLoading
		},
		data() {
			return {
				imgUrl:"http://139.9.163.126/gastric/",
				loading: false,
				detailData: {},
				newsList: [],
				evaList: []
			}
		},
		onLoad(options){
			uni.showLoading({
			    title: '加载中'
			});
			this.loading = true;
			this.getDetail(options.id)
		},
		methods: {
			favorite(){
				this.$http.post(`/gastric-cancer-data/favorite?articleId=${this.detailData.id}`).then(res => {
					if(res.data.code == 1){
						this.detailData.favorite = !this.detailData.favorite
					}
				})
			},
			viewDetail(id){
				uni.navigateTo({
					url: `/educationViews/details/index?id=${id}`
				})
				// let pages = getCurrentPages();
				// let page = pages[pages.length - 1];
				// page.onLoad()
				// this.getDetail(id)
			},
			formatRichText(html){
			  let newContent= html.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, 'width:100%;').replace(/width:[^;]+;/gi, 'width:100%;');
			      return match;
			  });
			  newContent = newContent.replace(/<br[^>]*\/>/gi, '');
			  newContent = newContent.replace(/\<img/gi, '<img style="width:100%;height:auto;display:block;margin:10px 0;"');
			  return newContent;
			},
			getDetail(id){
				this.$http.get(`/gastric-cancer-data/article/${id}`).then(res => {
					if(res.data.code == 1){
						this.detailData = res.data.object  
						this.detailData.articleContent = this.formatRichText(this.detailData.articleContent)
						setTimeout(function () {
						    uni.hideLoading();
						}, 200);
						this.loadNewsList();
					}
				})
			},
			
			//获取推荐列表
			loadNewsList(){
				const data = {"pageNum":1,"pageSize":10,'published': 1,tips:this.detailData.articleTips}
				this.$http.post(`/gastric-cancer-data/article/page`,data).then(res => {
					if(res.data.code == 1){
						let list = res.data.object.list
						this.newsList = list.filter(item => item.id != this.detailData.id )
						this.loading = false;
					}
				})
			},
			//获取评论列表
			async loadEvaList(){
				this.evaList = await json.evaList;
			}
		}
	}
</script>

<style lang="scss">
	page{
		height: 100%;
	}
	.content{
		display: flex;
		flex-direction: column;
		height: 100%;
		background: #ffffff;
		.container{
			background: #ffffff;
		}
	}
	.video-wrapper{
		height: 422upx;
		
		.video{
			width: 100%;
			height: 100%;
		}
	}
	.scroll{
		flex: 1;
		position: relative;
		background-color: #f8f8f8;
		height: 0;
	}
	.scroll-content{
		display: flex;
		flex-direction: column;
	}
	/* 简介 */
	.introduce-section{
		display: flex;
		flex-direction: column;
		padding: 20upx 30upx;
		background: #fff;
		line-height: 1.5;
		
		.title{
			font-size: 36upx;
			color: #303133;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
			white-space:pre-wrap;
			word-break:break-all;
			margin-bottom: 16upx;
		}
		.introduce{
			display: flex;
			font-size: 26upx;
			margin-bottom: 20upx;
			color: #909399;
			text{
				margin-right: 16upx;
			}
		}
	}
	/* 点赞等操作 */
	.actions{
		display: flex;
		justify-content: space-around;
		align-items: center;
		line-height: 1.3;
		padding: 10upx 40upx 20upx;
	
		.action-item{
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 24upx;
			color: #999;
		}
		.yticon{
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 52upx;
			
			&.reverse{
				position: relative;
				top: 6upx;
				transform: scaleY(-1);
			}
			&.active{
				color: #ec706b;
			}
		}
		.icon-fenxiang2{
			font-weight: bold;
			font-size: 36upx;
		}
		.icon-shoucang{
			font-size: 44upx;
		}
	}

	.s-header{
		padding: 20upx 30upx;
		font-size: 30upx;
		color: #303133;
		background: #fff;
		margin-top: 16upx;
		
		&:before{
			content: '';
			width: 0;
			height: 40upx;
			margin-right: 24upx;
			border-left: 6upx solid #ec706b;
		}
	}
	/* 推荐列表 */
	.rec-section{
		background-color: #fff;
		.rec-item{
			display: flex;
			padding: 20upx 30upx;
			position: relative;
			&:after{
				content: '';
				position: absolute;
				left: 30upx;
				right: 0;
				bottom: 0;
				height: 0;
				border-bottom: 1px solid #eee;
				transform: scaleY(50%);
			}
		}
		.left{
			flex: 1;
			padding-right: 10upx;
			overflow: hidden;
			position: relative;
			padding-bottom: 52upx;
			.title{
				display: -webkit-box;
				font-size: 30upx;
				color: black;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 1;
				overflow: hidden;
				white-space:pre-wrap;
				word-break:break-all;
				line-height: 40upx;
			}
			.bot{
				position: absolute;
				left: 0;
				bottom: 4upx;
				font-size: 24upx;
				color: #909399;
				.author{
					font-size: 24upx;
					color: #aaa;
					max-width: 560upx;
					min-width: 560upx;
					overflow: hidden;
					text-overflow:ellipsis;
					white-space: nowrap;
					display: inline-block;
				}
				.author2{
					max-width: 350upx;
					min-width: 350upx;
				}
				.time{
					font-size: 24upx;
					color: #aaa;
					min-width: 270upx;
				}
			}
		}
		.right{
			width: 220upx;
			height: 140upx;
			flex-shrink: 0;
			position: relative;
			.img{
				width: 100%;
				height: 100%;
				background-size: cover;
			}
		}
	}
	/* 评论 */
	.evalution{
		display:flex;
		flex-direction:column;
		background: #fff;
		padding: 20upx 0;
	}
	
	.eva-item{
		display:flex;
		padding: 20upx 30upx;
		position: relative;
		image{
			width: 60upx;
			height: 60upx;
			border-radius: 50px;
			flex-shrink: 0;
			margin-right: 24upx;
		}
		&:after{
			content: '';
			position: absolute;
			left: 30upx;
			bottom: 0;
			right: 0;
			height: 0;
			border-bottom: 1px solid #eee;
			transform: translateY(50%);
		}
		&:last-child:after{
			border: 0;
		}
	}
	.eva-right{
		display:flex;
		flex-direction:column;
		flex: 1;
		font-size: 26upx;
		color: #909399;
		position:relative;
		.zan-box{
			display:flex;
			align-items:base-line;
			position:absolute;
			top: 10upx;
			right: 10upx;
			.yticon{
				margin-left: 8upx; 
			}
		}
		.content{
			font-size: 28upx;
			color: #333;
			padding-top:20upx;
		}
	}
	/* 底部 */
	.bottom{
		flex-shrink: 0;
		display: flex;
		align-items: center;
		height: 90upx;
		padding: 0 30upx;
		box-shadow: 0 -1px 3px rgba(0,0,0,.04); 
		position: relative;
		z-index: 1;
		
		.input-box{
			display: flex;
			align-items: center;
			flex: 1;
			height: 60upx;
			background: #f2f3f3;
			border-radius: 100px;
			padding-left: 30upx;
			
			.icon-huifu{
				font-size: 28upx;
				color: #909399;
			}
			.input{
				padding: 0 20upx;
				font-size: 28upx;
				color: #303133;
			}
		}
		.confirm-btn{
			font-size: 30upx;
			padding-left: 20upx;
			color: #0d9fff;
		}
	}
</style>
