<!-- 详情 -->
<template>
	<view>
		<page-container ref="pageContainer" title="详情" show-back :show-not-data="showNotData" :refresh="onRefresh">
			<view class="news-details">
				<view class="news-details__title">
					{{ title }}
				</view>
				<view class="news-details__time">
					<view class="news-details__time-left">
						<text>{{ publish_time }}</text>
					</view>
					<view class="news-details__time-right">
						<image :src="follow_image" mode="aspectFill" style="width:36rpx; height:36rpx;"
							@click="onFollow"></image>
					</view>
				</view>
				<view class="news-details__content">
					<rich-text :nodes="content"></rich-text>
				</view>
			</view>

			<view class="news-details__footer">
				<view class="news-details__footer-item">
					<view class="flex-row" v-if="prev_data" @click="goToNews(prev_data)">
						<image src="/static/app-plus/news/prev.png" mode="aspectFill"
							style="width:20px; height:20px;position: relative;top:5rpx;"></image>
						<text>上一篇</text>
					</view>
				</view>
				<view class="news-details__footer-item">
					<view class="flex-row" v-if="next_data" @click="goToNews(next_data)">
						<text>下一篇</text>
						<image src="/static/app-plus/news/next.png" mode="aspectFill"
							style="width:20px; height:20px;position: relative;top:8rpx;"></image>
					</view>
				</view>
			</view>
			<view class="news-details__recommend_news">
				<view class="news-details__recommend_news-title">
					推荐
				</view>
				<view class="news-details__recommend_news-list">
					<view class="news-details__recommend_news-item" v-for="item in recommend_news" :key="item.id"
						@click="goToNews(item)">
						<view class="news-details__recommend_news-item-image">
							<image :src="item.cover_image" mode="aspectFill"></image>
						</view>
						<view class="news-details__recommend_news-item-content">
							<text>{{ item.title }}</text>
						</view>
					</view>
				</view>
			</view>
		</page-container>
	</view>
</template>

<script>
	import {
		getNewsDetail,
		addFavorite,
		cancelFavorite
	} from '@/common/server/news';
	import {
		sleep
	} from '@/common/utils';
	export default {
		data() {
			return {
				title: "",
				content: "",
				publish_time: "",
				author_name: "",
				follow: false,
				showNotData: false,
				options: {},
				next_data: null,
				prev_data: null,
				recommend_news: []
			}
		},
		computed: {
			follow_image() {
				return this.follow ? "/static/app-plus/news/guanzhu.png" : "/static/app-plus/news/no-guanzhu.png";
			}
		},
		async onLoad(options) {
			this.options = options;
			this.title = "";
			this.getNewsDetail(options.id, options.current);

		},
		methods: {
			async onFollow() {
				if (!yds.userInfo.isAccountLogin()) {
					yds.showToast("请登录后在收藏！");
					return;
				}
				this.follow = !this.follow;
				if (this.follow) {
					await addFavorite(this.options.id);
				} else {
					await cancelFavorite(this.options.id);
				}
			},
			async onRefresh(paging) {
				console.log("onRefreshonRefreshonRefreshonRefresh");
				await this.getNewsDetail(this.options.id, this.options.current);
				await sleep(500);
				paging.complete();
			},
			async goToNews(item) {
				this.options.id = item.id;
				await this.getNewsDetail(item.id, this.options.current);
				this.$refs.pageContainer.getPaging().scrollToTop();
			},
			getTitle(title) {
				return title.length > 10 ? title.slice(0, 10) + "..." : title;
			},
			async getNewsDetail(id, current) {
				const res = await getNewsDetail(id, current);
				console.log("resresresres", res);
				if (res) {
					this.title = res.title;
					this.content = res.content;
					this.publish_time = res.publish_time;
					this.author_name = res.author_name;
					this.next_data = res.next_data;
					this.prev_data = res.prev_data;
					this.recommend_news = res.recommend_news;
					this.follow = res.user_follow;
				} else {
					this.showNotData = true;
				}

			}
		}
	}
</script>

<style lang="scss" scoped>
	.news-details {
		padding: 30rpx 10rpx;
		min-height: 70vh;

		&__title {
			text-align: center;
			font-size: 32rpx;
			font-weight: bold;
			color: #333;
		}

		&__time {
			margin-top: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 10rpx;

			&-left {
				font-size: 24rpx;
				color: #999;
			}

			&-right {
				display: flex;
				align-items: center;
				padding-right: 20rpx;
			}
		}

		&__content {
			padding: 30rpx 10rpx;

		}

		&__follow {
			width: 90%;
			text-align: right;
		}

		&__footer {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx 10rpx;

			&-item {
				padding-left: 20rpx;
				padding-right: 20rpx;
				height: 70rpx;
				display: flex;
				align-items: center;
				justify-content: center;

			}
		}

		&__recommend_news {
			margin-top: 30rpx;
			padding: 0 20rpx;

			&-title {
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 20rpx;
				color: #333;
			}

			&-list {
				display: flex;
				flex-direction: column;
				gap: 20rpx;
			}

			&-item {
				display: flex;
				flex-direction: row;
				align-items: center;
				padding: 16rpx;
				background-color: #f9f9f9;
				border-radius: 12rpx;

				&-image {
					width: 160rpx;
					height: 120rpx;
					margin-right: 20rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 8rpx;
					}
				}

				&-content {
					flex: 1;

					text {
						font-size: 28rpx;
						color: #333;
						line-height: 1.4;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						overflow: hidden;
					}
				}
			}
		}

		.flex-row {
			display: flex;
			flex-direction: row;
			align-items: center;
			gap: 8rpx;
		}

		//设置图片样式
		::v-deep rich-text img {
			max-width: 95% !important;
			border-radius: 10rpx;
			height: auto !important;
			display: block !important;
			margin: 0 auto;
		}

		//设置图片样式
		::v-deep rich-text p {
			margin: 20rpx 10rpx;
		}

		::v-deep rich-text div {
			margin: 20rpx 10rpx;
		}
	}
</style>